-
I don't know whether this is a Rust or Iced question. Apologies if I've guessed wrong. I'd like to implement I've modified the 0.4 branch's counter example to demonstrate the issue. I've also published this code as a gist and a diff, which I hope will make it easier to grok what's going on. Below are the error and the code:
I think I get what's happening: the lifetime of the result is inferred to be no longer than the lifetime of The Rust version of this question: have I analyzed the problem correctly? How do I express that The Iced version of this question: is there a common pattern among Iced experts that allows something with a "weird" lifetime ( Thanks. P.S. The reason I wanted to stick the |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Yeesh, I have a complicated solution. I'll make a simplified example shortly, but here's the idea:
This seems to satisfy Rust that the lifetime of the I'm sure someone will reply in this thread with something like "oh you just needed a lifetime specifier <'a> here and here," but I'm reasonably happy with this solution. |
Beta Was this translation helpful? Give feedback.
-
Here is the code (with a gist for syntax highlighting). I hope it helps someone else doing the same thing. I'm open to suggestions for other approaches.
|
Beta Was this translation helpful? Give feedback.
-
Ya the iced todos example implements |
Beta Was this translation helpful? Give feedback.
-
In 99% of cases, you absolutely shouldn't need interior mutability anywhere across the app. I highly recommend targeting Here is a relevant snippet for you (using iced from use iced::widget::{column, container};
use iced::{executor, Application, Command, Element, Length, Settings, Theme};
use self::thing::Thing;
fn main() {
App::run(Settings::default()).unwrap();
}
#[derive(Debug, Clone)]
enum Message {
Thing(usize, thing::Message),
}
struct App {
things: Vec<Thing>,
}
impl Application for App {
type Executor = executor::Default;
type Message = Message;
type Theme = Theme;
type Flags = ();
fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
(
App {
things: vec![Thing::default(); 10],
},
Command::none(),
)
}
fn title(&self) -> String {
"Example".into()
}
fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::Thing(index, message) => {
if let Some(thing) = self.things.get_mut(index) {
if let Some(event) = thing.update(message) {
match event {
thing::Event::Delete => {
self.things.remove(index);
}
}
}
}
Command::none()
}
}
}
fn view(&self) -> Element<Message> {
let things = column(
self.things
.iter()
.enumerate()
.map(|(index, thing)| {
thing
.view(index)
.map(move |message| Message::Thing(index, message))
})
.collect(),
)
.spacing(5);
container(things)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
}
}
mod thing {
use iced::widget::{button, container, row, text};
use iced::{theme, Alignment, Element, Length};
#[derive(Debug, Clone)]
pub enum Event {
Delete,
}
#[derive(Debug, Clone)]
pub enum Message {
Increment,
Decrement,
Delete,
}
#[derive(Debug, Default, Clone)]
pub struct Thing {
count: u64,
}
impl Thing {
pub fn update(&mut self, message: Message) -> Option<Event> {
match message {
Message::Increment => {
self.count += 1;
None
}
Message::Decrement => {
self.count = self.count.saturating_sub(1);
None
}
Message::Delete => Some(Event::Delete),
}
}
pub fn view(&self, index: usize) -> Element<Message> {
let count = self.count;
container(
row![
text(format!("Thing {index} - Count: {count}")).width(Length::Fill),
button(text("Increment")).on_press(Message::Increment),
button(text("Decrement")).on_press(Message::Decrement),
button(text("Delete"))
.on_press(Message::Delete)
.style(theme::Button::Destructive)
]
.spacing(5)
.align_items(Alignment::Center),
)
.max_width(400)
.into()
}
}
} |
Beta Was this translation helpful? Give feedback.
In 99% of cases, you absolutely shouldn't need interior mutability anywhere across the app. I highly recommend targeting
master
instead of0.4
and using the newpure
API. This will make things a lot more simple and it's more natural asview(&self)
is now immutable.Here is a relevant snippet for you (using iced from
master
/ pure), where you can hold an array of some state for an iterable component of your app: