【问题标题】:"does not live long enough" error when calling `roots` from rust-xcb从 rust-xcb 调用 `roots` 时出现“寿命不够长”错误
【发布时间】:2016-05-23 09:15:31
【问题描述】:

我正在尝试在 xcb 创建的窗口中使用 Cairo 表面。我有一个 C 示例以及 Rust XCB 和 Cairo 绑定。我快完成了,但这个错误对我来说仍然是个谜。

我的代码:

fn find_visual<'a>(conn: &'a xcb::Connection, visual: xcb_visualid_t) -> Option<Visualtype<'a>> {
    let setup: Setup<'a> = conn.get_setup();
    for screen in setup.roots() {
        let d_iter: DepthIterator = screen.allowed_depths();
        for depth in d_iter {
            for vis in depth.visuals() {
                if visual == vis.visual_id() {
                    println!("Found visual");
                    return Some(vis)
                }
            }
        }
    }
    None
}

我称之为:

let visual = find_visual(&conn, screen.root_visual()).unwrap();

并获得这样的错误:

src/main.rs:56:19: 56:24 error: `setup` does not live long enough
src/main.rs:56     for screen in setup.roots() {
                                 ^~~~~
src/main.rs:54:97: 68:2 note: reference must be valid for the lifetime 'a as defined on the block at 54:96...
src/main.rs:54 fn find_visual<'a>(conn: &'a xcb::Connection, visual: xcb_visualid_t) -> Option<Visualtype<'a>> {
src/main.rs:55     let setup: Setup<'a> = conn.get_setup();
src/main.rs:56     for screen in setup.roots() {
src/main.rs:57         let d_iter: DepthIterator = screen.allowed_depths();
src/main.rs:58         for depth in d_iter {
src/main.rs:59             for vis in depth.visuals() {
               ...
src/main.rs:55:45: 68:2 note: ...but borrowed value is only valid for the block suffix following statement 0 at 55:44
src/main.rs:55     let setup: Setup<'a> = conn.get_setup();
src/main.rs:56     for screen in setup.roots() {
src/main.rs:57         let d_iter: DepthIterator = screen.allowed_depths();
src/main.rs:58         for depth in d_iter {
src/main.rs:59             for vis in depth.visuals() {
src/main.rs:60                 if visual == vis.visual_id() {
               ...

screendepth 变量的错误相同。

谁能解释一下 - 为什么“setup 活得不够长”?据我了解,setup 将在函数return 选项时被销毁,并且可以在函数中无限制地使用。

get_setup()代码:

pub fn get_setup(&self) -> Setup {
    unsafe {

        let setup = xcb_get_setup(self.c);
        if setup.is_null() {
            panic!("NULL setup on connection")
        }
        mem::transmute(setup)
    }
}

【问题讨论】:

  • 奇怪,查看 rust-xcb 的源码我找不到任何生命周期注释;特别是,Setup 结构没有任何内容。
  • @SebastianRedl 正如我所见,生命周期存在rtbo.github.io/rust-xcb/src/xcb/xproto.rs.html#1516
  • 啊,我正在查看过时的 Aatch 存储库。
  • 我似乎返回的 vis 取决于以前绑定的变量。当我删除 Some(vis) 编译成功。

标签: rust lifetime lifetime-scoping


【解决方案1】:

看起来绑定的生命周期注释有问题。这里是roots()

impl<'a> Screen<'a> {
    pub fn roots(&self) -> ScreenIterator {
        unsafe {
            xcb_setup_roots_iterator(self.ptr)
        }
    }
}

注意,因为函数上没有注解,所以隐含注解为

impl<'a> Screen<'a> {
    pub fn <'b> roots(&'b self) -> ScreenIterator<'b> {
        unsafe {
            xcb_setup_roots_iterator(self.ptr)
        }
    }
}

这是错误的。返回的ScreenIterator 需要用生命周期'a 显式注释,这是底层连接的生命周期,XCB 似乎有这样的约定,即它发出的所有指针都由生命周期绑定到连接的生命周期(请参阅包装器类型 base::StructPtr 的注释。这意味着需要调整生成器脚本以解决此问题。您应该向 crate 提交问题。

请求者更新: @SebastianRedl 是对的。问题在于 xcb crate 中所有返回迭代器的函数的生命周期未设置。 rust-xcb crate 中的下一个生命周期更改允许成功编译代码:

impl<'a> Setup<'a> {
    pub fn roots(&self) -> ScreenIterator<'a> {
        unsafe {
             xcb_setup_roots_iterator(self.ptr)
        }
    }
}

impl<'a> Depth<'a> {
    pub fn visuals(&self) -> VisualtypeIterator<'a> {
        unsafe {
            xcb_depth_visuals_iterator(self.ptr)
        }
    }
}

impl<'a> Screen<'a> {
    pub fn allowed_depths(&self) -> DepthIterator<'a> {
        unsafe {
            xcb_screen_allowed_depths_iterator(self.ptr)
        }
    }
}

【讨论】:

  • 我已更改 xcb ffi 生成器并获得“错误:生命周期名称 'a 阴影已在范围内的生命周期名称”。似乎我们不能使用与 impl Screen 中相同的生命周期名称
  • 重点是你不应该声明一个新的生命周期,而应该使用现有的生命周期。所以它应该看起来像:“pub fn root(&'a self) -> ScreenIterator { ... }”,而不是“pub fn root(...)”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多