tag | ef57090e8465c22336e88893e849964020ea7ca6 | |
---|---|---|
tagger | The Android Open Source Project <initial-contribution@android.com> | Thu Apr 27 16:29:28 2023 -0700 |
object | 3ecd884aa59f54921538441b71d9f55b48e9acd9 |
aml_tet_331511160 (9924753,com.google.android.tethering)
commit | 3ecd884aa59f54921538441b71d9f55b48e9acd9 | [log] [tgz] |
---|---|---|
author | Bob Badour <bbadour@google.com> | Thu Oct 06 11:54:53 2022 -0700 |
committer | Chih-Yu Huang <akahuang@google.com> | Mon Oct 17 16:51:08 2022 +0900 |
tree | a992347624b410ee7aca93aa12e53d36570a291b | |
parent | 8abb3c826cfeacbeb587a497d1ac221d79f44922 [diff] |
[LSC] Add LOCAL_LICENSE_KINDS to external/rust/crates/synstructure Added SPDX-license-identifier-MIT to: Android.bp Bug: 68860345 Bug: 151177513 Bug: 151953481 Bug: 251581018 Test: m all Merged-In: I39f82420361613606c022fc8ff1445079a289e76 Change-Id: Icb973b6f09d4205101023150be0b6b19943eb907 (cherry picked from commit effa69b0d2d119d97ae0dc2f4f430767f3660eed)
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.