简体   繁体   English

由于需求冲突,无法推断出合适的生命周期

[英]Cannot infer an appropriate lifetime due to conflicting requirements

I am new to rust and I am building a TUI app using rust-tui to practice and understand the concepts of rust.我是 Rust 的新手,我正在使用 rust-tui 构建一个 TUI 应用程序来练习和理解 Rust 的概念。 I have this code:我有这个代码:

// the widgets that can be renderd on the screen
#[derive(Clone)]
pub enum Widgets<'a> {
    ResList(ResList<'a>),
    ListResults(ListResults<'a>),
    Input(Input),
}

pub struct Screen<'a> {
    renders_done: u32,
    tx: Sender<Result<Event, crossterm::ErrorKind>>,
    rx: Receiver<Result<Event, crossterm::ErrorKind>>,
    main_screen: Widgets<'a>,
}


impl Screen<'_> {

    pub async fn handle_events(&mut self) {
        let event = self
            .rx
            .recv()
            .expect("Err while recievent the events in the reciever")
            .unwrap();

        let new_screen: Option<Widgets> = match &mut self.main_screen {
            Widgets::ResList(res_list) => {
                match event {
                    Event::Key(event) => match event.code {
                        KeyCode::Esc => {
                            Screen::exit_app();
                            None
                        }
                    _ => None,
                }
            }
            Widgets::Input(input) => input.handle_events(event).await,  <-- the problem comes when I add this
            _ => None,
        };
        match new_screen {
            Some(screen) => self.main_screen = screen,
            None => {}
        }
    }

}
impl Input {
    async fn handle_events(&mut self, event: Event) -> Option<Widgets> {
       None
    }
}

The idea is that if a sub-module returns a widget the main screen should be changed to that new widget.这个想法是,如果子模块返回一个小部件,主屏幕应该更改为该新小部件。 For texting purposes for now I never return a widget.现在出于发短信的目的,我从不返回小部件。 But when I try and build the code the compiler complains:但是当我尝试构建代码时,编译器会抱怨:

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
   --> src/model/tui/screen.rs:84:32
    |
84  |     pub async fn handle_events(&mut self) {
    |                                ^^^^^^^^^
    |
note: first, the lifetime cannot outlive the lifetime `'_` as defined on the method body at 84:32...
   --> src/model/tui/screen.rs:84:32
    |
84  |     pub async fn handle_events(&mut self) {
    |                                ^
note: ...so that the expression is assignable
   --> src/model/tui/screen.rs:84:32
    |
84  |     pub async fn handle_events(&mut self) {
    |                                ^^^^^^^^^
    = note: expected `&mut Screen<'_>`
               found `&mut Screen<'_>`
note: but, the lifetime must be valid for the lifetime `'_` as defined on the impl at 45:13...
   --> src/model/tui/screen.rs:45:13
    |
45  | impl Screen<'_> {
    |             ^^
note: ...so that the expression is assignable
   --> src/model/tui/screen.rs:126:52
    |
126 |             Some(mut screen) => self.main_screen = screen,
    |                                                    ^^^^^^
    = note: expected `Widgets<'_>`
               found `Widgets<'_>`

error: aborting due to previous error; 8 warnings emitted

For more information about this error, try `rustc --explain E0495`.

From what I understand the lifetimes are not living enough to be saved in the struct but I am not using references anywhere they are all owned values.据我所知,生命周期不足以保存在结构中,但我没有在任何地方使用引用,它们都是拥有的值。 Can someone help me understand what am I missing?有人可以帮助我了解我错过了什么吗?

Specify the lifetimes explicitly on the widget returned and create an explicit lifetime of the object:在返回的小部件上明确指定生命周期并创建对象的明确生命周期:

impl<'screen> Screen<'screen> {

    pub async fn handle_events<'c>(&'cmut self) {
        let event = self
            .rx
            .recv()
            .expect("Err while recievent the events in the reciever")
            .unwrap();

        let new_screen: Option<Widgets<'screen>> = match &mut self.main_screen {
            Widgets::ResList(res_list) => {
                match event {
                    Event::Key(event) => match event.code {
                        KeyCode::Esc => {
                            Screen::exit_app();
                            None
                        }
                    _ => None,
                }
            }
            Widgets::Input(input) => input.handle_events(event).await,
            _ => None,
        };
        match new_screen {
            Some(screen) => self.main_screen = screen,
            None => {}
        }
    }

}

Also add the lifetime explicitly when returning a Widget from inside a function从函数内部返回 Widget 时,还要显式添加生命周期

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试实现迭代器:由于需求冲突而无法推断出适当的生存期 - Trying to implement an iterator: cannot infer an appropriate lifetime due to conflicting requirements 由于需求冲突,无法推断出自动强制的适当寿命 - cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements 由于需求冲突,无法为借用表达式推断出适当的生命周期 - cannot infer an appropriate lifetime for borrow expression due to conflicting requirements 由于递归结构中的冲突要求,无法推断出适当的生命周期 - Cannot infer an appropriate lifetime due to conflicting requirements in a recursive struct 由于需求冲突,无法为 autoref 推断适当的生命周期 - Cannot infer an appropriate lifetime for autoref due to conflicting requirements 作为函数参数的闭包“由于需求冲突而无法推断出适当的寿命” - Closure as function parameter “cannot infer an appropriate lifetime due to conflicting requirements” Rust:由于要求冲突,无法推断 autoref 的适当生命周期 - Rust: cannot infer an appropriate lifetime for autoref due to conflicting requirements 匿名函数 - 由于需求冲突,无法推断出合适的生命周期 - Anonymous function - cannot infer an appropriate lifetime due to conflicting requirements 将结构转换为具有生存期的特征得到“由于需求冲突,无法为生存期参数&#39;a&#39;推断适当的生存期” - casting struct to trait with lifetime got “cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements” 由于对由具有 Serde Deserialize 的枚举组成的结构的要求相互冲突,因此无法为生命周期参数“de”推断合适的生命周期 - cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements for struct made of enums with Serde Deserialize
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM