Skip to content

Commit 392d0c9

Browse files
committed
feat: soa_attr for Ref/RefMut's field
1 parent 59b1f0e commit 392d0c9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

soa-derive-internal/src/refs.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ pub fn derive(input: &Input) -> TokenStream {
3434
.map(|field| format!("A mutable reference to a `{0}` from a [`{1}`](struct.{1}.html)", field, vec_name))
3535
.collect::<Vec<_>>();
3636

37+
let field_attrs = input.field_attrs.iter()
38+
.map(|attr| &attr.ref_).collect::<Vec<_>>();
39+
40+
let mut_field_attrs = input.field_attrs.iter()
41+
.map(|attr| &attr.ref_mut).collect::<Vec<_>>();
42+
3743
quote! {
3844
/// A reference to a
3945
#[doc = #doc_url]
@@ -43,6 +49,7 @@ pub fn derive(input: &Input) -> TokenStream {
4349
#visibility struct #ref_name<'a> {
4450
#(
4551
#[doc = #fields_doc]
52+
#(#[#field_attrs])*
4653
pub #fields_names_1: &'a #fields_types,
4754
)*
4855
}
@@ -54,6 +61,7 @@ pub fn derive(input: &Input) -> TokenStream {
5461
#visibility struct #ref_mut_name<'a> {
5562
#(
5663
#[doc = #fields_mut_doc]
64+
#(#[#mut_field_attrs])*
5765
pub #fields_names_1: &'a mut #fields_types,
5866
)*
5967
}

tests/soa_attr.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ fn eq_test() {
3131
#[soa_derive(PartialEq, Debug, Serialize, Deserialize)]
3232
pub struct Point {
3333
#[soa_attr(Slice, deprecated)]
34+
#[soa_attr(RefMut, deprecated)]
3435
pub x: f32,
3536
#[soa_attr(SliceMut, deprecated)]
3637
pub y: f32,
3738
#[soa_attr(Vec, serde(skip))]
39+
#[soa_attr(Ref, deprecated)]
3840
pub meta: bool
3941
}
4042

@@ -53,16 +55,27 @@ fn serde_skip_test() -> Result<(), serde_json::Error> {
5355
y: vec![2.0, 4.0],
5456
meta: vec![]
5557
});
58+
5659
{
5760
let slice = soa.as_slice();
5861
let _ = slice.x[0]; // Should have a deprecate warning
5962
let _ = slice.y[0]; // Should not have a deprecate warning
63+
let _ = slice.meta[0]; // Should not have a deprecate warning
64+
65+
let ref_ = slice.get(1).unwrap();
66+
let _ = ref_.x; // Should not have a deprecate warning
67+
let _ = ref_.y; // Should not have a deprecate warning
68+
let _ = ref_.meta; // Should have a deprecate warning
6069
}
6170
{
62-
let slice = soa.as_mut_slice();
71+
let mut slice = soa.as_mut_slice();
6372
let _ = slice.y[0]; // Should have a deprecate warning
6473
let _ = slice.x[0]; // Should not have a deprecate warning
6574

75+
let ref_mut = slice.get_mut(1).unwrap();
76+
let _ = ref_mut.x; // Should have a deprecate warning
77+
let _ = ref_mut.y; // Should not have a deprecate warning
78+
let _ = ref_mut.meta; // Should not have a deprecate warning
6679
}
6780
Ok(())
6881
}

0 commit comments

Comments
 (0)