Skip to content

Instantly share code, notes, and snippets.

@seanmonstar
Created September 7, 2016 17:21
Show Gist options
  • Save seanmonstar/2fe0a7058b73edf389c43e1859cf09d1 to your computer and use it in GitHub Desktop.
Save seanmonstar/2fe0a7058b73edf389c43e1859cf09d1 to your computer and use it in GitHub Desktop.
struct Shared<T>(mpsc::Sender<T>);
impl Clone for Shared<T> {
fn clone(&self) -> Shared<T> {
Shared(self.0.clone())
}
}
impl<T> Shared<T> {
pub fn new(tx: mpsc::Sender<T>) -> Shared<T> {
// this guarantees the safety of the `Sync` imlementation for `Shared
tx.clone();
Shared(tx)
}
}
// safe because mpsc::Sender.clone() upgrades it to an internally thread-safe type
unsafe impl<T> Sync for Shared<T> {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment