Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created January 7, 2021 21:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/01023893293d237e6d42c4f778b2f70e to your computer and use it in GitHub Desktop.
Save rust-play/01023893293d237e6d42c4f778b2f70e to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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