gpt4 book ai didi

带有 thread_local 的结构中的 Rust 生命周期

转载 作者:行者123 更新时间:2023-11-29 08:30:25 27 4
gpt4 key购买 nike

<分区>

我正在尝试使用 Windows API 编写 Windows GUI 程序,使用 thread_local 保存所有创建的窗口或按钮。

use std::thread::Thread;
use std::rc::Rc;
use std::cell::RefCell;
use std::collections::HashMap;

// button window checkbox need impl this trait.
trait Wnd{
fn CreateWindow(&self)->int
{
// do some thing
0
}
}

pub struct Dust{
window_counter:int,
hInstance:int,
hookId: int,
widgets:HashMap<int, Rc<RefCell<Wnd>>>,
}

fn dust()->RefCell<Dust>{
unsafe{
let mut d = Dust{
window_counter:0,
hInstance:0,
hookId:0,
widgets: HashMap::new(),
};

return RefCell::new(d)
}
}

pub thread_local!(static TLS_DUST: RefCell<Dust> = dust());


fn main()
{

TLS_DUST.with( | d | {
let mut dust = d.borrow_mut();
dust.window_counter-=1;
});

}

当我构建时出现错误:

$ cargo build --verbose
Compiling dust v0.0.1 (file:///D:/msys64/home/BYWAYBOY/dust/examples)
Running `rustc D:\msys64\home\BYWAYBOY\dust\src\lib.rs --crate-name dust --crate-type lib -g -C metadata=3fbf7f518595f4d2 -C extra-filename=-3fbf7f518595f4d2 --out-dir D:\msys64\home\BYWAYBOY\dust\examples\target\deps --emit=dep-info,link -L D:\msys64\home\BYWAYBOY\dust\examples\target\deps -L D:\msys64\home\BYWAYBOY\dust\examples\target\deps`
D:\msys64\home\BYWAYBOY\dust\src\lib.rs:52:36: 52:39 error: explicit lifetime bound required
D:\msys64\home\BYWAYBOY\dust\src\lib.rs:52 widgets:HashMap<HWND, Rc<RefCell<Wnd>>>,
^~~
error: aborting due to previous error
Could not compile `dust`.

Caused by:
Process didn't exit successfully: `rustc D:\msys64\home\BYWAYBOY\dust\src\lib.rs --crate-name dust --crate-type lib -g -C metadata=3fbf7f518595f4d2 -C extra-filename=-3fbf7f518595f4d2 --out-dir D:\msys64\home\BYWAYBOY\dust\examples\target\deps --emit=dep-info,link -L D:\msys64\home\BYWAYBOY\dust\examples\target\deps -L D:\msys64\home\BYWAYBOY\dust\examples\target\deps` (status=101)

我只想使用 HashMap 将所有窗口和小部件存储在 thread_local 中。

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