From 3508add83945c7ce6cf2fb3e9b746d022241d81c Mon Sep 17 00:00:00 2001 From: Ole Magnus Fon Johnsen Date: Tue, 23 Sep 2025 11:54:25 +0200 Subject: [PATCH] fix: rename len to __soa_len to avoid field name conflicts --- soavec_derive/src/soable.rs | 8 ++++---- soavec_derive/tests/integration.rs | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/soavec_derive/src/soable.rs b/soavec_derive/src/soable.rs index 7dc5c1f..0dcdb02 100644 --- a/soavec_derive/src/soable.rs +++ b/soavec_derive/src/soable.rs @@ -186,11 +186,11 @@ pub fn expand_derive_soable(input: DeriveInput) -> syn::Result { value: ::Pointers, len: u32, ) -> Self::Slice<'soa> { - let len = len as usize; + let __soa_len = len as usize; let (#(#field_names),*) = value; unsafe { #slice_struct_name { - #(#field_names: core::slice::from_raw_parts(#field_names.as_ptr(), len)),* + #(#field_names: core::slice::from_raw_parts(#field_names.as_ptr(), __soa_len)),* } } } @@ -200,11 +200,11 @@ pub fn expand_derive_soable(input: DeriveInput) -> syn::Result { value: ::Pointers, len: u32, ) -> Self::SliceMut<'soa> { - let len = len as usize; + let __soa_len = len as usize; let (#(#field_names),*) = value; unsafe { #slice_mut_struct_name { - #(#field_names: core::slice::from_raw_parts_mut(#field_names.as_ptr(), len)),* + #(#field_names: core::slice::from_raw_parts_mut(#field_names.as_ptr(), __soa_len)),* } } } diff --git a/soavec_derive/tests/integration.rs b/soavec_derive/tests/integration.rs index e674a6e..42354dd 100644 --- a/soavec_derive/tests/integration.rs +++ b/soavec_derive/tests/integration.rs @@ -22,6 +22,13 @@ struct GenericStruct { #[derive(SoAble)] struct TupleStruct(u32, f64, String); +#[derive(SoAble)] +struct StructWithLen { + a: usize, + // `len` should not overwrite in the macro + len: usize, +} + #[test] fn test_derive_compiles() { // If this compiles, the derive macro worked @@ -76,3 +83,16 @@ fn test_tuple_struct() { assert_eq!(back.1, 2.71); assert_eq!(back.2, "hello".to_string()); } + +#[test] +fn test_struct_with_len_field() { + use soavec::SoAble; + + let value = StructWithLen { a: 5, len: 8 }; + let tuple = SoAble::into_tuple(value); + assert_eq!(tuple, (5, 8)); + + let back = StructWithLen::from_tuple((9, 12)); + assert_eq!(back.a, 9); + assert_eq!(back.len, 12); +}