Skip to content

Commit cbb89ed

Browse files
feat: add support for tuple types in space calculation (solana-foundation#3744)
* feat: add support for tuple types in space calculation This commit introduces the ability to handle tuple types in the space calculation logic. A new `TestTupleStruct` is added to validate various tuple configurations, including nested and option types, ensuring accurate space calculations through comprehensive unit tests. * clean up unnacessary comments * chore: fmt fix * fix: fixing test assert * fix: fixing test assert
1 parent 2b5b78e commit cbb89ed

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

lang/derive/space/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ fn len_from_type(ty: Type, attrs: &mut Option<VecDeque<TokenStream2>>) -> TokenS
151151
}
152152
}
153153
}
154+
Type::Tuple(ty_tuple) => {
155+
let recurse = ty_tuple
156+
.elems
157+
.iter()
158+
.map(|t| len_from_type(t.clone(), attrs));
159+
quote! {
160+
(0 #(+ #recurse)*)
161+
}
162+
}
154163
_ => panic!("Type {ty:?} is not supported"),
155164
}
156165
}

lang/tests/space.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,38 @@ pub struct TestUnnamedStruct(
109109
#[derive(InitSpace)]
110110
pub struct TestUnitStruct;
111111

112+
#[derive(InitSpace)]
113+
#[allow(clippy::type_complexity)]
114+
pub struct TestTupleStruct {
115+
pub test_tuple: (u8, u16, u32, u64, u128),
116+
pub mixed_tuple: (bool, f32, f64, i8, i16, i32, i64, i128),
117+
118+
pub nested_tuple: (u8, (u16, u32, u64, u128)),
119+
pub deeply_nested: (u8, (u16, (u32, (u64, u128)))),
120+
pub complex_nested: (bool, (u8, u16), (u32, (u64, u128))),
121+
122+
pub option_tuple: Option<(u8, u16, u32, u64, u128)>,
123+
pub tuple_with_option: (u8, Option<u16>, u32),
124+
pub nested_option_tuple: (u8, Option<(u16, u32)>, u64),
125+
126+
pub pubkey_tuple: (Pubkey, u64),
127+
pub tuple_with_pubkeys: (Pubkey, Pubkey, u8),
128+
129+
pub struct_tuple: (ChildStruct, u8),
130+
pub nested_struct_tuple: (u8, (ChildStruct, u16)),
131+
132+
pub single_tuple: (u64,),
133+
pub single_nested: ((u8,),),
134+
135+
pub empty_tuple: (),
136+
pub tuple_with_empty: (u8, (), u16),
137+
138+
pub array_tuple: ([u8; 4], u16),
139+
pub tuple_array_nested: (u8, ([u16; 2], u32)),
140+
141+
pub ultimate_complex: (u8, (bool, Option<(u16, u32)>, ChildStruct), Pubkey),
142+
}
143+
112144
#[test]
113145
fn test_empty_struct() {
114146
assert_eq!(TestEmptyAccount::INIT_SPACE, 0);
@@ -172,3 +204,80 @@ fn test_unnamed_struct() {
172204
fn test_unit_struct() {
173205
assert_eq!(TestUnitStruct::INIT_SPACE, 0)
174206
}
207+
208+
#[test]
209+
fn test_basic_tuple() {
210+
let basic_tuple_size = 1 + 2 + 4 + 8 + 16; // 31
211+
assert!(TestTupleStruct::INIT_SPACE >= basic_tuple_size);
212+
}
213+
214+
#[test]
215+
fn test_tuple_space_calculations() {
216+
let basic_tuple_size = 1 + 2 + 4 + 8 + 16; // 31
217+
218+
let mixed_tuple_size = 1 + 4 + 8 + 1 + 2 + 4 + 8 + 16; // 44
219+
220+
let nested_tuple_size = 1 + (2 + 4 + 8 + 16); // 31
221+
222+
let option_tuple_size = 1 + (1 + 2 + 4 + 8 + 16); // 32
223+
224+
let pubkey_tuple_size = 32 + 8; // 40
225+
226+
let single_tuple_size = 8;
227+
228+
let empty_tuple_size = 0;
229+
230+
let minimum_expected_size = basic_tuple_size
231+
+ mixed_tuple_size
232+
+ nested_tuple_size
233+
+ option_tuple_size
234+
+ pubkey_tuple_size
235+
+ single_tuple_size
236+
+ empty_tuple_size;
237+
238+
assert!(TestTupleStruct::INIT_SPACE >= minimum_expected_size);
239+
}
240+
241+
#[test]
242+
fn test_tuple_with_structs() {
243+
// Test that tuples containing other structs work correctly
244+
// struct_tuple: (ChildStruct, u8) = ChildStruct::INIT_SPACE + 1
245+
let expected_struct_tuple_contribution = ChildStruct::INIT_SPACE + 1;
246+
247+
assert!(TestTupleStruct::INIT_SPACE >= expected_struct_tuple_contribution);
248+
}
249+
250+
#[test]
251+
fn test_nested_tuple_complexity() {
252+
// Test deeply_nested: (u8, (u16, (u32, (u64, u128))))
253+
// = 1 + (2 + (4 + (8 + 16))) = 1 + (2 + (4 + 24)) = 1 + (2 + 28) = 1 + 30 = 31
254+
let deeply_nested_size = 1 + 2 + 4 + 8 + 16; // 31
255+
256+
// Test complex_nested: (bool, (u8, u16), (u32, (u64, u128)))
257+
// = 1 + (1 + 2) + (4 + (8 + 16)) = 1 + 3 + (4 + 24) = 1 + 3 + 28 = 32
258+
let complex_nested_size = 1 + (1 + 2) + (4 + (8 + 16)); // 32
259+
260+
assert!(TestTupleStruct::INIT_SPACE >= deeply_nested_size + complex_nested_size);
261+
}
262+
263+
#[test]
264+
fn test_tuple_with_options() {
265+
// tuple_with_option: (u8, Option<u16>, u32) = 1 + (1 + 2) + 4 = 8
266+
let tuple_with_option_size = 1 + (1 + 2) + 4; // 8
267+
268+
// nested_option_tuple: (u8, Option<(u16, u32)>, u64) = 1 + (1 + (2 + 4)) + 8 = 16
269+
let nested_option_tuple_size = 1 + (1 + (2 + 4)) + 8; // 16
270+
271+
assert!(TestTupleStruct::INIT_SPACE >= tuple_with_option_size + nested_option_tuple_size);
272+
}
273+
274+
#[test]
275+
fn test_tuple_with_arrays() {
276+
// array_tuple: ([u8; 4], u16) = (4 * 1) + 2 = 6
277+
let array_tuple_size = 4 + 2; // 6
278+
279+
// tuple_array_nested: (u8, ([u16; 2], u32)) = 1 + ((2 * 2) + 4) = 1 + (4 + 4) = 9
280+
let tuple_array_nested_size = 1 + ((2 * 2) + 4); // 9
281+
282+
assert!(TestTupleStruct::INIT_SPACE >= array_tuple_size + tuple_array_nested_size);
283+
}

0 commit comments

Comments
 (0)