I really wish there would be a WidgetMutRef trait for the following use case:
trait Screen: WidgetMutRef {}
struct Screen1 {
list_state: ListState,
}
impl Screen for Screen1 {}
impl WidgetMutRef for Screen1 {
fn render_ref(&mut self, area: Rect, buf: &mut Buffer) {
StatefulWidget::render(
List::new(["Item 1", "Item 2"]),
area,
buf,
// this would fail with `WidgetRef` or with any other attempt, because `Screen1` isn't mutable borrowed...
&mut self.list_state
);
}
}
struct App {
screens: Vec<Box<dyn Screen>>
}
impl App {
fn draw(&mut self, frame: &mut Frame) {
let screen: &mut Box<dyn Screen> = self.screens.last_mut().unwrap();
frame.render_widget(screen, frame.area());
}
}
the only solution for this which I’m using now, thanks to claude:
pub trait Screen {
fn render(&mut self, area: Rect, buf: &mut Buffer);
}
impl<'a> Widget for &'a mut (dyn Screen + 'static) {
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer)
where
Self: Sized,
{
Screen::render(self, area, buf);
}
}
howerve, I find this really ugly.
I think, introducing a WidgetMutRef would fix this issue in a more beautiful way:
trait WigdetMutRef {
fn render_mut_ref(&mut self, area: Rect, buf: &mut Buffer);
}