我正在尝试在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() {
...
screen
和depth
变量的错误相同。
有人可以解释 - 为什么“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)
}
}
答案 0 :(得分:2)
看起来绑定的生命周期注释是错误的。这是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
的注释。这意味着需要调整生成器脚本以解决此问题。您应该对包装箱提出问题。
更新: @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)
}
}
}