So, according to Rust’s lifetime elision rules, &selfand &Self::State are assigned two different lifetimes (e.g., 'a and 'b, respectively). The resulting Elementcan only contain data from &'a self, not from &'b Self::State.
Is this intentional? The resulting widgets can only reference data from self, not from Self::State?
If yes, how can I circumvent this limitation and use iced::Component correctly?
I haven’t tried the Component trait myself but you’re right, all the examples I’ve found (which are not that many by the way, just the one included in Iced and a couple more in Github) either don’t have an internal state or they use it during view but don’t need to return references to parts of it.
What exactly is your use case? What would be stored in your component’s internal state that you would need to reference directly from the final widget?
My component contains a canvas and a few buttons. The geometry drawn by my program on the canvas is cached. I am trying to make this cache part of the component’s local state because I do not need it to be part of the global application state.
Hmm yes, this sounds like a perfectly valid use case. I don’t know what’s the proper way to fix this, or if maybe Iced hasn’t considered this scenario. It sounds like your options at the moment are: make the geometry part of the global app state, or clone the geometry from the internal state on every view.
I just wanted to bump this thread as I faced the same issue myself when implementing a widget for showing some live logs. In my case however I’m uncertain if the log messages should be stored in the “outer” or “inner” state. I would prefer the latter to have it fully encapsulated, but that would require cloning all messages on every call to view.
As is mentioned, all examples I have seen don’t use the state or don’t need to borrow from it. This leads me to belive that this might be an intentional API to ensure that such state should be “global”. However I suppose it might also just be an oversight in the API. Hopefully someone more involved could give us some clarity!