Sync

The Sync trait is automatically applied to any data type that can safely be borrowed between threads.

While our data types will have the Sync trait automatically if they qualify for it, occasionally we want to be sure that a data type does not have Sync, even if it looks to the compiler like it should.

We can achieve that by implementing !Sync for our data type:

pub enum NotSyncExample {
Good,
Bad,
}

impl !Sync for NotSyncExample {}

We don't actually need any functions inside of !Sync. All we're doing is telling the compiler that the Sync trait is inappropriate for this type.

As of Rust 1.29, implementing !Sync is still considered an unstable feature, and is not available in the stable build of the compiler. It can be enabled in the nightly build by placing #![feature(optin_builtin_traits)] at the top of the file.

A lot of data types have the Sync trait, but Rc, Cell, and RefCell are notable examples of types that do not. Arc, Mutex, and RwLock do, though.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.60.62