Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#[derive(Debug)]
- struct Foo {
- bar: Bar,
- }
- impl Foo {
- fn inc2(&mut self) -> i32 {
- dbg!("in Foo::inc2");
- self.v += 10;
- self.v
- }
- }
- impl std::ops::Deref for Foo {
- type Target = Bar;
- fn deref(&self) -> &Self::Target {
- &self.bar
- }
- }
- impl std::ops::DerefMut for Foo {
- fn deref_mut(&mut self) -> &mut Bar {
- &mut self.bar
- }
- }
- #[derive(Debug,Clone)]
- struct Bar {
- v: i32,
- }
- impl Bar {
- fn inc(&mut self) -> i32 {
- dbg!("in Bar::inc");
- self.v += 1;
- self.v
- }
- }
- fn main () {
- let mut x = Bar {v: 5};
- dbg!(&x);
- x.inc();
- dbg!(x.inc());
- dbg!(x.inc());
- dbg!(x.clone());
- let mut y = Foo { bar: x };
- //let y = &mut y;
- y.inc2();
- y.inc();
- dbg!(&y as &Bar);
- dbg!(y.clone());
- dbg!(&*y);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement