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

Commit 4fc2150

Browse files
author
Joe C
authored
[type-length-value]: update README (#5230)
1 parent b11e5df commit 4fc2150

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

libraries/type-length-value/README.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use {
2121
struct MyPodValue {
2222
data: [u8; 32],
2323
}
24-
impl SpleDiscriminates for MyPodValue {
24+
impl SplDiscriminate for MyPodValue {
2525
// Give it a unique discriminator, can also be generated using a hash function
2626
const SPL_DISCRIMINATOR: ArrayDiscriminator = ArrayDiscriminator::new([1; ArrayDiscriminator::LENGTH]);
2727
}
@@ -56,22 +56,39 @@ let mut buffer = vec![0; account_size];
5656
let mut state = TlvStateMut::unpack(&mut buffer).unwrap();
5757

5858
// Init and write default value
59-
let value = state.init_value::<MyPodValue>().unwrap();
59+
// Note: you'll need to provide a boolean whether or not to allow repeating
60+
// values with the same TLV discriminator.
61+
// If set to false, this function will error when an existing entry is detected.
62+
let value = state.init_value::<MyPodValue>(false).unwrap();
6063
// Update it in-place
6164
value.data[0] = 1;
6265

6366
// Init and write another default value
64-
let other_value = state.init_value::<MyOtherPodValue>().unwrap();
65-
assert_eq!(other_value.data, 10);
67+
// This time, we're going to allow repeating values.
68+
let other_value1 = state.init_value::<MyOtherPodValue>(true).unwrap();
69+
assert_eq!(other_value1.data, 10);
6670
// Update it in-place
67-
other_value.data = 2;
71+
other_value1.data = 2;
6872

69-
// Later on, to work with it again
70-
let value = state.get_value_mut::<MyPodValue>().unwrap();
73+
// Let's do it again, since we can now have repeating values!
74+
let other_value2 = state.init_value::<MyOtherPodValue>(true).unwrap();
75+
assert_eq!(other_value2.data, 10);
76+
// Update it in-place
77+
other_value1.data = 4;
78+
79+
// Later on, to work with it again, since we did _not_ allow repeating entries,
80+
// we can just get the first value we encounter.
81+
let value = state.get_first_value_mut::<MyPodValue>().unwrap();
7182

7283
// Or fetch it from an immutable buffer
7384
let state = TlvStateBorrowed::unpack(&buffer).unwrap();
74-
let value = state.get_value::<MyOtherPodValue>().unwrap();
85+
let value1 = state.get_first_value::<MyOtherPodValue>().unwrap();
86+
87+
// Since we used repeating entries for `MyOtherPodValue`, we can grab either one by
88+
// its entry number
89+
let value1 = state.get_value_with_repetition::<MyOtherPodValue>(1).unwrap();
90+
let value2 = state.get_value_with_repetition::<MyOtherPodValue>(2).unwrap();
91+
7592
```
7693

7794
## Motivation
@@ -155,11 +172,12 @@ let mut buffer = vec![0; account_size];
155172
let mut state = TlvStateMut::unpack(&mut buffer).unwrap();
156173

157174
// No need to hold onto the bytes since we'll serialize back into the right place
158-
let _ = state.allocate::<MyVariableLenType>(tlv_size).unwrap();
175+
// For this example, let's _not_ allow repeating entries.
176+
let _ = state.alloc::<MyVariableLenType>(tlv_size, false).unwrap();
159177
let my_variable_len = MyVariableLenType {
160178
data: initial_data.to_string()
161179
};
162180
state.pack_variable_len_value(&my_variable_len).unwrap();
163-
let deser = state.get_variable_len_value::<MyVariableLenType>().unwrap();
181+
let deser = state.get_first_variable_len_value::<MyVariableLenType>().unwrap();
164182
assert_eq!(deser, my_variable_len);
165183
```

0 commit comments

Comments
 (0)