I’m still new to Rust, and while I’m starting to have a grasp about the data model, the borrow checker and other base things, I haven’t written multi-threaded code yet. Well, until now.
So, I wanted to use http://aatxe.github.io/irc/irc/client/server/struct.IrcServer.html from multiple threads. The type documentation is rather slim. Arguably, there’s an issue there. In retrospect, I could have found the examples in the crate repository and spared myself some trouble.
Anyways, “thread-safe” doesn’t tell much, so I tried various things with things with Arc, Mutex, etc.
They all failed to compile with variants of:
src/main.rs:19:5: 19:10 error: the trait bound `std::cell::Cell<u32>: std::marker::Sync` is not satisfied [E0277]
src/main.rs:19 spawn(move || {
^~~~~
src/main.rs:19:5: 19:10 help: run `rustc --explain E0277` to see a detailed explanation
src/main.rs:19:5: 19:10 note: `std::cell::Cell<u32>` cannot be shared between threads safely
src/main.rs:19:5: 19:10 note: required because it appears within the type `irc::client::server::IrcServer`
src/main.rs:19:5: 19:10 note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Arc<irc::client::server::IrcServer>`
src/main.rs:19:5: 19:10 note: required because it appears within the type `[closure@src/main.rs:19:11: 21:6 server:std::sync::Arc<irc::client::server::IrcServer>]`
src/main.rs:19:5: 19:10 note: required by `std::thread::spawn`
… which is a pretty opaque failure from a user perspective.
As it turns out, I didn’t need to wrap the type at all, and just passing a IrcServer clone to the thread closure is how the type is supposed to be used in thread-safe manner.
Is there anything that could be done here to be more helpful?