Last active
July 26, 2019 14:45
-
-
Save yihuang/df12f84c85eba48efed55acc896a2cbc to your computer and use it in GitHub Desktop.
cyclic type reference in rust
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::fmt; | |
| trait Listener where Self: Sized + fmt::Debug { | |
| fn on_event(&self, obj: &TestObject<Self>, args: &str); | |
| } | |
| #[derive(Debug)] | |
| struct TestObject<F> where F:Listener { | |
| listener: F | |
| } | |
| #[derive(Debug)] | |
| struct TestListener; | |
| impl Listener for TestListener { | |
| fn on_event(&self, obj: &TestObject<Self>, args: &str) { | |
| println!("on_event {:?} {}", obj, args); | |
| } | |
| } | |
| fn main() { | |
| let listener = TestListener{}; | |
| let obj = TestObject{listener: listener}; | |
| obj.listener.on_event(&obj, "hello world"); | |
| } |
Author
The most recent version is fixed by using trait object.
Author
Tried with trait object again, it also works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.