Closed nielsle closed 2 months ago
Here is another idea that might be useful. It could be nice to have a generic macro to get a field without specifying the field name
struct A; struct B; #[derive(StructGetMutField)] struct Foo { a:A, b:B, } fn main() { let foo= Foo {a:A, b:B}; let _a: &A = foo.get_mut_field(); let _b: &B = foo.get_mut_field(); }
The following could be derived behind the scenes
trait GetMutField<T> { fn get_mut_field(&mut self) ->&mut T; } impl GetMutField<A> for Foo { fn get_mut_field(&mut self) ->&mut A {&mut self.a} } impl GetMutField<B> for Foo { fn get_mut_field(&mut self) ->&mut B {&mut self.b} }
This macro would allow me to do the following with minimal boiler plate code;
#[derive(StructGetMutField)] struct Foo { as:Vec<A>, bs:Vec<B>, } let mut foo = Foo {as:Vec::new(), bs:Vec::new(), }; let a = A; foo.get_mut_field().push(a);
Similarly it would allow me to create a poor mans TypeMap without using the Any-trait.
#[derive(StructGetMutField)] struct Foo { a_opt:Option<A>, b_opt:Option<B>, } let mut foo = Foo {a_opt: Option::new(), b_opt: Option::new(), }; foo.get_mut_field() = Some(A);;
Here is another idea that might be useful. It could be nice to have a generic macro to get a field without specifying the field name
The following could be derived behind the scenes
This macro would allow me to do the following with minimal boiler plate code;
Similarly it would allow me to create a poor mans TypeMap without using the Any-trait.