Created
February 14, 2023 04:56
-
-
Save mxgrey/ce991a208e367d5a5f9c8617f7463463 to your computer and use it in GitHub Desktop.
Automatic Inheritance by Deref
This file contains hidden or 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::{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