Hi everyone, I am bad at rust so please bear with me, but trying to get comfortable with it,
I am writing a small TODO application in Rust with TUI, using the ratatui
crate,
I have defined a marker
trait that requires the implementation of StatefulWidget
use ratatui::widgets::{StatefulWidget, Widget};
use crate::widgets::TodoList;
pub struct ViewState<'a> {
todo_list: &'a TodoList,
}
pub trait View<'a>: StatefulWidget<State = ViewState<'a>> {}
note
StatefulWidget
doesn't accept any lifetime arguments
I want to implement the View
trait on the ListView
type, but I can't since
the struct doesn't take any lifetime arguments
use super::{View, ViewState};
use ratatui::prelude::*;
pub struct ListView;
impl<'a> StatefulWidget for ListView {
type State = ViewState<'a>;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {}
}
impl<'a> View<'a> for ListView {}
error message:
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
--> src/views/list.rs:6:6
|
6 | impl<'a> StatefulWidget for ListView {
| ^^ unconstrained lifetime parameter
you can see the State
type is only used in the function as argument type
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State)
if I understand correctly this function signature actually equivalent to
fn render(self, area: Rect, buf: &mut Buffer, state: &mut ViewState<'a>)
but to make the function signature actually correct, we should introduce the lifetime only
for that function right? like so
fn render<'a>(self, area: Rect, buf: &mut Buffer, state: &mut ViewState<'a>)
I have tried everything (GPT didn't help)
P.S: didn't try it yet, but I think I can replace the
todo_list
pointer withRc
right? although I prefer not to but if this is the only solution I might have no option