@@ -49,50 +49,56 @@ let account_size = TlvStateMut::get_base_len()
49
49
+ TlvStateMut :: get_base_len ()
50
50
+ std :: mem :: size_of :: <MyOtherPodValue >()
51
51
+ TlvStateMut :: get_base_len ()
52
- + std :: mem :: size_of :: <MyOtherPodValue >()
52
+ + std :: mem :: size_of :: <MyOtherPodValue >();
53
53
54
54
// Buffer likely comes from a Solana `solana_program::account_info::AccountInfo`,
55
55
// but this example just uses a vector.
56
56
let mut buffer = vec! [0 ; account_size ];
57
57
58
- {
59
- // Unpack the base buffer as a TLV structure
60
- let mut state = TlvStateMut :: unpack (& mut buffer ). unwrap ();
61
-
62
- // Init and write default value
63
- // Note: you'll need to provide a boolean whether or not to allow repeating
64
- // values with the same TLV discriminator.
65
- // If set to false, this function will error when an existing entry is detected.
66
- let (value , _ ) = state . init_value :: <MyPodValue >(false ). unwrap ();
67
- // Update it in-place
68
- value . data[0 ] = 1 ;
69
-
70
- // Init and write another default value
71
- // This time, we're going to allow repeating values.
72
- let (other_value1 , _ ) = state . init_value :: <MyOtherPodValue >(true ). unwrap ();
73
- assert_eq! (other_value1 . data, 10 );
74
- // Update it in-place
75
- other_value1 . data = 2 ;
76
-
77
- // Let's do it again, since we can now have repeating values!
78
- let (other_value2 , _ ) = state . init_value :: <MyOtherPodValue >(true ). unwrap ();
79
- assert_eq! (other_value2 . data, 10 );
80
- // Update it in-place
81
- other_value2 . data = 4 ;
82
-
83
- // Later on, to work with it again, we can just get the first value we
84
- // encounter, because we did _not_ allow repeating entries for `MyPodValue`.
85
- let value = state . get_first_value_mut :: <MyPodValue >(). unwrap ();
86
- }
58
+ // Unpack the base buffer as a TLV structure
59
+ let mut state = TlvStateMut :: unpack (& mut buffer ). unwrap ();
60
+
61
+ // Init and write default value
62
+ // Note: you'll need to provide a boolean whether or not to allow repeating
63
+ // values with the same TLV discriminator.
64
+ // If set to false, this function will error when an existing entry is detected.
65
+ // Note the function also returns the repetition number, which can be used to
66
+ // fetch the value again.
67
+ let (value , _repetition_number ) = state . init_value :: <MyPodValue >(false ). unwrap ();
68
+ // Update it in-place
69
+ value . data[0 ] = 1 ;
70
+
71
+ // Init and write another default value
72
+ // This time, we're going to allow repeating values.
73
+ let (other_value1 , other_value1_repetition_number ) =
74
+ state . init_value :: <MyOtherPodValue >(true ). unwrap ();
75
+ assert_eq! (other_value1 . data, 10 );
76
+ // Update it in-place
77
+ other_value1 . data = 2 ;
78
+
79
+ // Let's do it again, since we can now have repeating values!
80
+ let (other_value2 , other_value2_repetition_number ) =
81
+ state . init_value :: <MyOtherPodValue >(true ). unwrap ();
82
+ assert_eq! (other_value2 . data, 10 );
83
+ // Update it in-place
84
+ other_value2 . data = 4 ;
85
+
86
+ // Later on, to work with it again, we can just get the first value we
87
+ // encounter, because we did _not_ allow repeating entries for `MyPodValue`.
88
+ let value = state . get_first_value_mut :: <MyPodValue >(). unwrap ();
87
89
88
90
// Or fetch it from an immutable buffer
89
91
let state = TlvStateBorrowed :: unpack (& buffer ). unwrap ();
90
92
let value1 = state . get_first_value :: <MyOtherPodValue >(). unwrap ();
91
93
92
94
// Since we used repeating entries for `MyOtherPodValue`, we can grab either one by
93
- // its entry number
94
- let value1 = state . get_value_with_repetition :: <MyOtherPodValue >(0 ). unwrap ();
95
- let value2 = state . get_value_with_repetition :: <MyOtherPodValue >(1 ). unwrap ();
95
+ // its repetition number
96
+ let value1 = state
97
+ . get_value_with_repetition :: <MyOtherPodValue >(other_value1_repetition_number )
98
+ . unwrap ();
99
+ let value2 = state
100
+ . get_value_with_repetition :: <MyOtherPodValue >(other_value2_repetition_number )
101
+ . unwrap ();
96
102
97
103
```
98
104
0 commit comments