You can also mark types as thread-safe, effectively promising the compiler that you've arranged all the data races in such a way as to make them safe. This is relatively rare in practice but it's worth seeing what it takes to make a type thread-safe in Rust before we start building on top of safe primitives. First, let's take a look at code that's intentionally sideways:
use std::{mem, thread}; use std::ops::{Deref, DerefMut}; unsafe impl Send for Ring {} unsafe impl Sync for Ring {} struct InnerRing { capacity: isize, size: usize, data: *mut Option<u32>, } #[derive(Clone)] struct Ring { inner: *mut InnerRing, }
What we have here is a ring, or a circular buffer, of u32. InnerRing holds a raw mutable pointer and is not automatically ...