tag | 5e43f5940cb4674d334078b36f24ca734c1fabb8 | |
---|---|---|
tagger | The Android Open Source Project <initial-contribution@android.com> | Wed Nov 06 13:24:32 2024 -0800 |
object | 9a9e8dc8adf318727a46d4ac48fa22a2828ed686 |
Android 15.0.0 release 5
commit | 9a9e8dc8adf318727a46d4ac48fa22a2828ed686 | [log] [tgz] |
---|---|---|
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | Thu May 23 23:15:20 2024 +0000 |
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | Thu May 23 23:15:20 2024 +0000 |
tree | 7b020269a83863a5d7de6e369fb9caf47ac78605 | |
parent | 275569d7d4d5c2d9fb2ffa0db98c8e782a175613 [diff] | |
parent | 6fa5b7e9ae17f05a85cb761a65cd8a9e634dcd13 [diff] |
Snap for 11881322 from 6fa5b7e9ae17f05a85cb761a65cd8a9e634dcd13 to 24Q3-release Change-Id: I8e12c720ec58eb390e19306f4537839567eb4f79
NOTE: What follows is an exerpt from the module level documentation. For full details read the docs on docs.rs
This crate provides helper types for matching against enum variants, and extracting bindings to each of the fields in the deriving Struct or Enum in a generic way.
If you are writing a #[derive]
which needs to perform some operation on every field, then you have come to the right place!
WalkFields
pub trait WalkFields: std::any::Any { fn walk_fields(&self, walk: &mut FnMut(&WalkFields)); } impl WalkFields for i32 { fn walk_fields(&self, _walk: &mut FnMut(&WalkFields)) {} }
#[macro_use] extern crate synstructure; #[macro_use] extern crate quote; extern crate proc_macro2; fn walkfields_derive(s: synstructure::Structure) -> proc_macro2::TokenStream { let body = s.each(|bi| quote!{ walk(#bi) }); s.bound_impl(quote!(example_traits::WalkFields), quote!{ fn walk_fields(&self, walk: &mut FnMut(&example_traits::WalkFields)) { match *self { #body } } }) } decl_derive!([WalkFields] => walkfields_derive); /* * Test Case */ fn main() { test_derive! { walkfields_derive { enum A<T> { B(i32, T), C(i32), } } expands to { #[allow(non_upper_case_globals)] const _DERIVE_example_traits_WalkFields_FOR_A: () = { extern crate example_traits; impl<T> example_traits::WalkFields for A<T> where T: example_traits::WalkFields { fn walk_fields(&self, walk: &mut FnMut(&example_traits::WalkFields)) { match *self { A::B(ref __binding_0, ref __binding_1,) => { { walk(__binding_0) } { walk(__binding_1) } } A::C(ref __binding_0,) => { { walk(__binding_0) } } } } } }; } } }
Interest
pub trait Interest { fn interesting(&self) -> bool; } impl Interest for i32 { fn interesting(&self) -> bool { *self > 0 } }
#[macro_use] extern crate synstructure; #[macro_use] extern crate quote; extern crate proc_macro2; fn interest_derive(mut s: synstructure::Structure) -> proc_macro2::TokenStream { let body = s.fold(false, |acc, bi| quote!{ #acc || example_traits::Interest::interesting(#bi) }); s.bound_impl(quote!(example_traits::Interest), quote!{ fn interesting(&self) -> bool { match *self { #body } } }) } decl_derive!([Interest] => interest_derive); /* * Test Case */ fn main() { test_derive!{ interest_derive { enum A<T> { B(i32, T), C(i32), } } expands to { #[allow(non_upper_case_globals)] const _DERIVE_example_traits_Interest_FOR_A: () = { extern crate example_traits; impl<T> example_traits::Interest for A<T> where T: example_traits::Interest { fn interesting(&self) -> bool { match *self { A::B(ref __binding_0, ref __binding_1,) => { false || example_traits::Interest::interesting(__binding_0) || example_traits::Interest::interesting(__binding_1) } A::C(ref __binding_0,) => { false || example_traits::Interest::interesting(__binding_0) } } } } }; } } }
For more example usage, consider investigating the abomonation_derive
crate, which makes use of this crate, and is fairly simple.