|
21 | 21 | struct MyPodValue {
|
22 | 22 | data: [u8; 32],
|
23 | 23 | }
|
24 |
| -impl SpleDiscriminates for MyPodValue { |
| 24 | +impl SplDiscriminate for MyPodValue { |
25 | 25 | // Give it a unique discriminator, can also be generated using a hash function
|
26 | 26 | const SPL_DISCRIMINATOR: ArrayDiscriminator = ArrayDiscriminator::new([1; ArrayDiscriminator::LENGTH]);
|
27 | 27 | }
|
@@ -56,22 +56,39 @@ let mut buffer = vec![0; account_size];
|
56 | 56 | let mut state = TlvStateMut::unpack(&mut buffer).unwrap();
|
57 | 57 |
|
58 | 58 | // 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(); |
60 | 63 | // Update it in-place
|
61 | 64 | value.data[0] = 1;
|
62 | 65 |
|
63 | 66 | // 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); |
66 | 70 | // Update it in-place
|
67 |
| -other_value.data = 2; |
| 71 | +other_value1.data = 2; |
68 | 72 |
|
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(); |
71 | 82 |
|
72 | 83 | // Or fetch it from an immutable buffer
|
73 | 84 | 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 | + |
75 | 92 | ```
|
76 | 93 |
|
77 | 94 | ## Motivation
|
@@ -155,11 +172,12 @@ let mut buffer = vec![0; account_size];
|
155 | 172 | let mut state = TlvStateMut::unpack(&mut buffer).unwrap();
|
156 | 173 |
|
157 | 174 | // 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(); |
159 | 177 | let my_variable_len = MyVariableLenType {
|
160 | 178 | data: initial_data.to_string()
|
161 | 179 | };
|
162 | 180 | 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(); |
164 | 182 | assert_eq!(deser, my_variable_len);
|
165 | 183 | ```
|
0 commit comments