gpt4 book ai didi

rust - 如何注释此Rust/Calloop回调代码的生存期?

转载 作者:行者123 更新时间:2023-12-03 11:28:26 28 4
gpt4 key购买 nike

我正在尝试编写一个将回调函数添加到Calloop's事件循环的函数。如果将代码简单地粘贴到其中函数参数都在作用域和堆栈中的函数中,则该代码可以正常工作。将其拉出功能会导致生命周期问题。
这是我在进入Calloop的内部结构之前所能简化的代码(作为初学者,这超出了我一点):

use calloop::{Interest, Mode, generic::Generic, LoopHandle};
use std::os::unix::io::RawFd;

fn check_function<F, T>(
handle: &mut LoopHandle<T>,
fd: RawFd,
callback: F,
)
where
F: FnMut(calloop::Readiness, &mut calloop::generic::Fd, &mut T) -> Result<(), std::io::Error>
{
let event_source = Generic::from_fd(fd, Interest::READ, Mode::Edge);

handle.insert_source(
event_source,
callback,
).ok();
}
编译结果为:
error[E0311]: the parameter type `F` may not live long enough
--> src/test.rs:14:12
|
4 | fn check_function<F, T>(
| - help: consider adding an explicit lifetime bound...: `F: 'a`
...
14 | handle.insert_source(
| ^^^^^^^^^^^^^
|
note: the parameter type `F` must be valid for the anonymous lifetime #2 defined on the function body at 4:1...
--> src/test.rs:4:1
|
4 | / fn check_function<F, T>(
5 | | handle: &mut LoopHandle<T>,
6 | | fd: RawFd,
7 | | callback: F,
8 | | )
9 | | where
10 | | F: FnMut(calloop::Readiness, &mut calloop::generic::Fd, &mut T) -> Result<(), std::io::Error>
| |_________________________________________________________________________________________________^
note: ...so that the type `F` will meet its required lifetime bounds
--> src/test.rs:14:12
|
14 | handle.insert_source(
| ^^^^^^^^^^^^^
由于它在所有东西都在堆栈上时可以工作,因此我想告诉它所有东西都具有相同(或兼容)的生存期:
fn check_function<'a, F, T>(
handle: &mut LoopHandle<T>,
fd: RawFd,
callback: F,
)
where
T: 'a,
F: FnMut(calloop::Readiness, &mut calloop::generic::Fd, &mut T) -> Result<(), std::io::Error> + 'a
但是rustc告诉我增加另一个生命周期:
error[E0311]: the parameter type `F` may not live long enough
--> src/test.rs:15:12
|
4 | fn check_function<'a, F, T>(
| - help: consider adding an explicit lifetime bound...: `F: 'b`
...
15 | handle.insert_source(
| ^^^^^^^^^^^^^
|
note: the parameter type `F` must be valid for the anonymous lifetime #2 defined on the function body at 4:1...
--> src/test.rs:4:1
|
4 | / fn check_function<'a, F, T>(
5 | | handle: &mut LoopHandle<T>,
6 | | fd: RawFd,
7 | | callback: F,
... |
10 | | T: 'a,
11 | | F: FnMut(calloop::Readiness, &mut calloop::generic::Fd, &mut T) -> Result<(), std::io::Error> + 'a
| |______________________________________________________________________________________________________^
note: ...so that the type `F` will meet its required lifetime bounds
--> src/test.rs:15:12
|
15 | handle.insert_source(
| ^^^^^^^^^^^^^
这似乎是学习字母的相当慢的方法。我真的想了解我如何告诉rustc这些生命周期都是兼容的,而不是仅仅扔掉生命周期注释直到它起作用。次要的:
  • 什么是“匿名生命周期2”?
  • check_function<'a, F: 'a, ...>等于check_function<'a, F, ...> where F: ... + 'a吗?
  • 最佳答案

    Rustc对此并不是很有帮助,但是我发现LoopHandle的参数是有生命的,并且由于句柄获得了传入的FnMut的所有权,因此FnMut的生命周期必须超过LoopHandle。一旦弄清楚了,注释正确的生命周期就很容易了:

    use calloop::{
    generic::{Fd, Generic},
    Interest, LoopHandle, Mode,
    };
    use std::io::Result;
    use std::os::unix::io::RawFd;
    fn check_function<'a: 'b, 'b, F, T>(handle: &mut LoopHandle<'b, T>, fd: RawFd, callback: F)
    where
    F: 'a + FnMut(calloop::Readiness, &mut Fd, &mut T) -> Result<()>,
    {
    let event_source = Generic::from_fd(fd, Interest::READ, Mode::Edge);

    handle.insert_source(event_source, callback).ok();
    }

    关于rust - 如何注释此Rust/Calloop回调代码的生存期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66048033/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com