Skip to content

Instantly share code, notes, and snippets.

@espindola
Created December 16, 2021 02:05
Show Gist options
  • Save espindola/c3f2cb19a9a9715652d55bd9d71cf63a to your computer and use it in GitHub Desktop.
Save espindola/c3f2cb19a9a9715652d55bd9d71cf63a to your computer and use it in GitHub Desktop.
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