Created
December 16, 2021 02:05
-
-
Save espindola/c3f2cb19a9a9715652d55bd9d71cf63a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::ops::DerefMut; | |
use std::ops::Deref; | |
struct Foo { | |
a: i32, | |
b: bool, | |
} | |
fn foo(_: &mut i32, _: bool) {} | |
struct Bar { | |
f: Foo, | |
} | |
impl Deref for Bar { | |
type Target = Foo; | |
fn deref(&self) -> &Self::Target { | |
&self.f | |
} | |
} | |
impl DerefMut for Bar { | |
fn deref_mut(&mut self) -> &mut Self::Target { | |
&mut self.f | |
} | |
} | |
fn main() { | |
let f = Foo { a: 42, b: true }; | |
// let mut zed = Bar { f }; | |
let mut zed = std::boxed::Box::new(f); | |
let xyz = &mut zed.a; | |
foo(xyz, zed.b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment