Skip to content

Commit

Permalink
add forward_mut to Sequential derive
Browse files Browse the repository at this point in the history
  • Loading branch information
swfsql committed Nov 6, 2023
1 parent 20a958d commit c96e05d
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions dfdx-derives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,23 +489,44 @@ pub fn sequential(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
};

let impl_module = {
let src = match input.data {
let (src, src_mut) = match input.data {
Data::Struct(ref data) => match data.fields {
Fields::Named(ref fields) => {
let recurse = fields.named.iter().map(|f| {
let name = &f.ident;
quote_spanned! {f.span()=> self.#name.try_forward(x)? }
});
quote! { #(let x = #recurse;)* }
let (recurse, recurse_mut) = fields
.named
.iter()
.map(|f| {
let name = &f.ident;
(
quote_spanned! {f.span()=> self.#name.try_forward(x)? },
quote_spanned! {f.span()=> self.#name.try_forward_mut(x)? },
)
})
.unzip::<_, _, Vec<_>, Vec<_>>();
(
quote! { #(let x = #recurse;)* },
quote! { #(let x = #recurse_mut;)* },
)
}
Fields::Unnamed(ref fields) => {
let recurse = fields.unnamed.iter().enumerate().map(|(i, f)| {
let index = Index::from(i);
quote_spanned! {f.span()=> self.#index.try_forward(x)? }
});
quote! { #(let x = #recurse;)* }
let (recurse, recurse_mut) = fields

Check failure on line 512 in dfdx-derives/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-check

unused variable: `recurse_mut`

Check warning on line 512 in dfdx-derives/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-test-nightly

unused variable: `recurse_mut`

Check warning on line 512 in dfdx-derives/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-test-nightly

unused variable: `recurse_mut`

Check warning on line 512 in dfdx-derives/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-test-nightly

unused variable: `recurse_mut`

Check warning on line 512 in dfdx-derives/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-test-nightly

unused variable: `recurse_mut`
.unnamed
.iter()
.enumerate()
.map(|(i, f)| {
let index = Index::from(i);
(
quote_spanned! {f.span()=> self.#index.try_forward(x)? },
quote_spanned! {f.span()=> self.#index.try_forward_mut(x)? },
)
})
.unzip::<_, _, Vec<_>, Vec<_>>();
(
quote! { #(let x = #recurse;)* },
quote! { #(let x = #recurse;)* },
)
}
Fields::Unit => quote! { let x = x; },
Fields::Unit => (quote! { let x = x; }, quote! { let x = x; }),
},
_ => unreachable!(),
};
Expand All @@ -520,6 +541,10 @@ pub fn sequential(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
#src
Ok(x)
}
fn try_forward_mut(&mut self, x: Input) -> Result<Self::Output, Error> {
#src_mut
Ok(x)
}
}
}
};
Expand Down

0 comments on commit c96e05d

Please sign in to comment.