私はRustのスレッドに新しいです。スレッド内のrustbox枠のRustBox
typeを渡すのに苦労しています。RustBoxの同期を実装する方法は?
IがI(1における別の+
シンボルを示し、それら2秒以内 Wキー、2を押しながら(1、1)Qキーを押すと、それがで2秒間+
記号を表示したいです)。
私は同じロジックのためのいくつかのコードを書いた:
extern crate rustbox;
use std::thread;
use std::time::Duration;
use rustbox::{Color, RustBox};
use rustbox::Key;
fn mark_at(x: usize, y: usize, rustbox: &RustBox) {
rustbox.print(x, y, rustbox::RB_BOLD, Color::Black, Color::White, "+");
thread::spawn(move || {
let delay = Duration::from_millis(2000);
thread::sleep(delay);
rustbox.print(x, y, rustbox::RB_BOLD, Color::Black, Color::White, " ");
});
}
fn main() {
let rustbox = match RustBox::init(Default::default()) {
Result::Ok(v) => v,
Result::Err(e) => panic!("{}", e),
};
rustbox.print(1, 1, rustbox::RB_BOLD, Color::Black, Color::White, " ");
rustbox.print(1, 2, rustbox::RB_BOLD, Color::Black, Color::White, " ");
loop {
rustbox.present();
match rustbox.poll_event(false) {
Ok(rustbox::Event::KeyEvent(key)) => {
match key {
Key::Char('q') => {
mark_at(1, 1, &rustbox);
}
Key::Char('w') => {
mark_at(1, 2, &rustbox);
}
Key::Esc => { break; }
_ => { }
}
},
Err(e) => panic!("{}", e),
_ => { }
}
}
}
それは私を与える:
error[E0277]: the trait bound `*mut(): std::marker::Sync` is not satisfied in `rustbox::RustBox`
--> src/main.rs:12:5
|
12 | thread::spawn(move || {
| ^^^^^^^^^^^^^ `*mut()` cannot be shared between threads safely
|
= help: within `rustbox::RustBox`, the trait `std::marker::Sync` is not implemented for `*mut()`
= note: required because it appears within the type `std::marker::PhantomData<*mut()>`
= note: required because it appears within the type `rustbox::RustBox`
= note: required because of the requirements on the impl of `std::marker::Send` for `&rustbox::RustBox`
= note: required because it appears within the type `[[email protected]/main.rs:12:19: 16:6 rustbox:&rustbox::RustBox, x:usize, y:usize]`
= note: required by `std::thread::spawn`
error: aborting due to previous error
上記のコードは、仕事ができるように、どのように私はRustBox
タイプにSync
を実装していますか?
あなたは(特性[ 'Send'と' Sync']を理解していますhttps://doc.rust-lang.org/book/ second-edition/ch16-04-extensible-concurrency-sync-and-send.html)?あなたが 'Sync'ではない型の意味を理解することが最善です。 1つは 'Sync'を実装するだけではありません。関連性の高い質問: https://stackoverflow.com/q/36649865/1233251 –