Skip to content

Instantly share code, notes, and snippets.

@mxgrey
Created February 14, 2023 04:56
Show Gist options
  • Select an option

  • Save mxgrey/ce991a208e367d5a5f9c8617f7463463 to your computer and use it in GitHub Desktop.

Select an option

Save mxgrey/ce991a208e367d5a5f9c8617f7463463 to your computer and use it in GitHub Desktop.
Automatic Inheritance by Deref
use std::{sync::Arc, ops::Deref};
fn main() {
listen(Arc::new(Foo));
}
fn listen<N: Noisy>(n: N) {
n.noise();
}
trait Noisy {
fn noise(&self);
}
impl<T> Noisy for T
where
T: Deref,
T::Target: Noisy,
{
fn noise(&self) {
self.deref().noise()
}
}
struct Foo;
impl Noisy for Foo {
fn noise(&self) {
println!("foo");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment