Skip to content

Instantly share code, notes, and snippets.

@justinmoon
Created May 29, 2020 09:46
Show Gist options
  • Save justinmoon/2395bce7f5b7601504113b260a07c83d to your computer and use it in GitHub Desktop.
Save justinmoon/2395bce7f5b7601504113b260a07c83d to your computer and use it in GitHub Desktop.
use iced::{button, Align, Button, Column, Element, Sandbox, Settings, Text};
pub fn main() {
App::run(Settings::default())
}
#[derive(Default)]
struct First<'value> {
value: &'value str,
}
#[derive()]
struct Second<'value> {
value: &'value str,
}
enum Step<'value> {
First(First<'value>),
Second(Second<'value>),
}
struct App<'value> {
value: String,
step: Step<'value>,
toggle_button: button::State,
}
#[derive(Debug, Clone, Copy)]
enum Message {
Toggle,
}
impl<'value> Sandbox for App<'_> {
type Message = Message;
fn new() -> Self {
let value = String::from("Hello");
Self {
value,
step: Step::First(First { value: &value }),
toggle_button: button::State::new(),
}
}
fn title(&self) -> String {
String::from("Lifetimes")
}
fn update(&mut self, message: Message) {
match message {
Message::Toggle => match self.step {
Step::First(_) => {
let second = Second { value: &self.value };
self.step = Step::Second(second);
}
Step::Second(_) => {
let first = First { value: &self.value };
self.step = Step::First(first);
}
},
}
}
fn view(&mut self) -> Element<Message> {
Column::new()
.padding(20)
.align_items(Align::Center)
.push(
Button::new(&mut self.toggle_button, Text::new("Toggle")).on_press(Message::Toggle),
)
.push(match self.step {
Step::First(first) => Text::new(format!("first ... value={}", first.value)),
Step::Second(second) => Text::new(format!("second ... value={}", second.value)),
})
.into()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment