-
-
Save rust-play/01023893293d237e6d42c4f778b2f70e to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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
trait Widget { | |
fn base_widget(&mut self) -> &mut dyn Widget; | |
fn render(&mut self) { | |
self.base_widget().render(); | |
} | |
} | |
struct BaseWidget; | |
impl Widget for BaseWidget { | |
fn base_widget(&mut self) -> &mut dyn Widget { | |
unreachable!("Attempt to base on self") | |
} | |
fn render(&mut self) { | |
println!("BaseWidget Render"); | |
} | |
} | |
struct Button { | |
base_widget: BaseWidget | |
} | |
impl Widget for Button { | |
fn base_widget(&mut self) -> &mut dyn Widget { | |
&mut self.base_widget | |
} | |
} | |
fn main() { | |
let mut widget = Button { | |
base_widget: BaseWidget | |
}; | |
widget.render(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment