-
-
Save rust-play/8667abe111edeb90bca9a44687ecdca4 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
#[derive(Debug)] | |
struct UiState { | |
parent: u64, | |
id: u64, | |
} | |
trait Ui { | |
fn state(&mut self) -> &mut UiState; | |
#[must_use] | |
fn horizontal(&mut self) -> UiContext { | |
let state = self.state(); | |
let child = UiState{parent: state.id, id: state.id + 1}; | |
let mut ctx = UiContext{ui: child, parent: state, result: Result{rect: 0}}; | |
ctx.enter(); | |
ctx | |
} | |
} | |
#[derive(Debug)] | |
struct Result { | |
rect: u64, | |
} | |
#[derive(Debug)] | |
struct UiContext<'a> { | |
ui: UiState, | |
parent: &'a UiState, | |
result: Result, | |
} | |
impl Ui for UiState { | |
fn state(&mut self) -> &mut UiState { self } | |
} | |
impl Ui for UiContext<'_> { | |
fn state(&mut self) -> &mut UiState { &mut self.ui } | |
} | |
impl UiContext<'_> { | |
fn enter(&mut self) { | |
println!("enter context {:?}", self); | |
} | |
fn exit(&mut self) { | |
println!("exit context {:?}", self); | |
} | |
} | |
impl Drop for UiContext<'_> { | |
fn drop(&mut self) { | |
println!("dropping context {:?}", self); | |
self.exit(); | |
} | |
} | |
fn main() { | |
let mut root = UiState{parent: 0, id: 0}; | |
println!("root {:?}", root); | |
{ | |
let mut ui = root.horizontal(); | |
println!("ui 1 {:?}", ui); | |
{ | |
let mut ui = ui.horizontal(); | |
println!("ui 2 {:?}", ui); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment