前提・実現したいこと
rust で AsRef<Path> の値を thread spawn で move したい。
発生している問題・エラーメッセージ
rust
1error[E0277]: `P` cannot be sent between threads safely 2 --> src/main.rs:56:5 3 | 456 | thread::spawn(move || { 5 | ^^^^^^^^^^^^^ `P` cannot be sent between threads safely 6 | 7 = help: within `[closure@src/main.rs:56:19: 65:6 path:P, tx:std::sync::mpsc::Sender<walkdir::DirEntry>]`, the trait `std::marker::Send` is not implemented for `P` 8 = help: consider adding a `where P: std::marker::Send` bound 9 = note: required because it appears within the type `[closure@src/main.rs:56:19: 65:6 path:P, tx:std::sync::mpsc::Sender<walkdir::DirEntry>]` 10 = note: required by `std::thread::spawn`
該当のソースコード
fn get_entries<P: AsRef<Path>>(path: P) -> mpsc::Receiver<DirEntry> { let (tx, rx) = mpsc::channel::<DirEntry>(); thread::spawn(move || { let _ = WalkDir::new(path) .into_iter() .map(|e| match e { Ok(e) => tx.send(e).unwrap(), Err(e) => eprintln!("{}", e), }) .collect::<Vec<_>>(); drop(tx); }); rx }
path なので、文字列ではなく、 AsRef<Path> として渡したいと思っています。
rust だとこういう場合どういう風にやるのがいいのでしょうか?
thread への渡し方、後は rsut 流だとこうする、みたいなのもあれば教えて欲しいです。
rust バージョン
2018
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。