use auto_delegate::{delegate, Delegate}; #[delegate] trait Accessible { fn read(&self) -> &T; fn read_mut(&mut self) -> &mut T; fn write(&mut self, value: T); } struct Child { value: T, } impl Default for Child { fn default() -> Self { Self { value: T::default(), } } } impl Accessible for Child { fn read(&self) -> &T { &self.value } fn read_mut(&mut self) -> &mut T { &mut self.value } fn write(&mut self, value: T) { self.value = value; } } #[derive(Delegate)] struct Parent { #[to(Accessible)] child: Child, } impl Default for Parent { fn default() -> Parent { Self { child: Child::default(), } } } fn main() { let mut parent = Parent::::default(); assert_eq!(parent.read(), ""); parent.write(String::from("Hello")); assert_eq!(parent.read_mut(), "Hello"); }