I'm trying to use the Windows API to write a Windows GUI program, using thread_local
to save all the created windows or buttons.
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;
});
}
when I build I got the error:
$ 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)
I just want use the HashMap to store all windows and widgets in a thread_local
.