我正在尝试在 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)
}
}