Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 0161d35

Browse files
author
Joe C
authored
TLV: README: use repetition numbers (#7114)
1 parent 4a7faf8 commit 0161d35

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

libraries/type-length-value/README.md

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,50 +49,56 @@ let account_size = TlvStateMut::get_base_len()
4949
+ TlvStateMut::get_base_len()
5050
+ std::mem::size_of::<MyOtherPodValue>()
5151
+ TlvStateMut::get_base_len()
52-
+ std::mem::size_of::<MyOtherPodValue>()
52+
+ std::mem::size_of::<MyOtherPodValue>();
5353

5454
// Buffer likely comes from a Solana `solana_program::account_info::AccountInfo`,
5555
// but this example just uses a vector.
5656
let mut buffer = vec![0; account_size];
5757

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();
8789

8890
// Or fetch it from an immutable buffer
8991
let state = TlvStateBorrowed::unpack(&buffer).unwrap();
9092
let value1 = state.get_first_value::<MyOtherPodValue>().unwrap();
9193

9294
// 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();
96102

97103
```
98104

0 commit comments

Comments
 (0)