3 ms·
Any links or ideas on how to get started with some of these? Understanding traits and knowing common ones is as trivial as reading the Rust Book, but ideas like
by JasonSage 9y ago
Any links or ideas on how to get started with some of these? Understanding traits and knowing common ones is as trivial as reading the Rust Book, but ideas like "write blanket implementations" and "encapsulate ownership details" are less easy to find.
- allengeorge 9y agoI really started looking into blanket implementations after I saw this example [1] in Tokio. Basically it says "implement the `AsyncRead` trait for any boxed type that implements `AsyncRead` or any `AsyncRead` trait object. The Error trait contains more examples [2]. As for "encapsulate ownership details". Hard to describe, but...let me point to Tokio [3] again. In Java if you wanted to share an IO reader/writer with two different objects you'd use the same type and pass the same reference to both owners. In Rust, the same idiom would require the wordy use of `Arc<Mutex<Type>>` or `Rc<RefCell<Type>>`. I hope we can both agree that that's frustrating. An alternative (as demonstrated by Tokio's `split` method) is to separate your base type into two other types - reader and writer - that can be owned independently. No more externally-visible ref counting. Of course, this isn't possible in all scenarios: there absolutely are times when you don't have a clean separation of types and have to ref count; but, avoid it if you can. I learned all of this by poking into libs, asking a ton on the IRC channels (everyone is super friendly) and failing a lot. Any errors are my own. Hope that helps! [1] https://github.com/tokio-rs/tokio-io/blob/master/src/lib.rs#L192 https://github.com/tokio-rs/tokio-io/blob/master/src/lib.rs#... [2] https://doc.rust-lang.org/src/std/error.rs.html#281 https://doc.rust-lang.org/src/std/error.rs.html#281 [3] https://github.com/tokio-rs/tokio-io/blob/master/src/lib.rs#L186 https://github.com/tokio-rs/tokio-io/blob/master/src/lib.rs#...