Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions soavec_derive/src/soable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ pub fn expand_derive_soable(input: DeriveInput) -> syn::Result<TokenStream> {
value: <Self::TupleRepr as soavec::SoATuple>::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)),*
}
}
}
Expand All @@ -200,11 +200,11 @@ pub fn expand_derive_soable(input: DeriveInput) -> syn::Result<TokenStream> {
value: <Self::TupleRepr as soavec::SoATuple>::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)),*
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions soavec_derive/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ struct GenericStruct<T, U> {
#[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
Expand Down Expand Up @@ -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);
}