From 29f71397137b3b2506fd47ff2ead90dc980880a1 Mon Sep 17 00:00:00 2001 From: Daniel Franklin Date: Tue, 23 Mar 2021 20:41:54 +0000 Subject: [PATCH] Reduce untested code --- src/send-and-sync.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/send-and-sync.md b/src/send-and-sync.md index 87c8b6a..b4105bf 100644 --- a/src/send-and-sync.md +++ b/src/send-and-sync.md @@ -126,9 +126,11 @@ to access it. [`Box`][box-doc] implements [`Deref`][deref-doc] and [`DerefMut`][deref-mut-doc] so that you can access the inner value. Let's do that. -```rust,ignore +```rust use std::ops::{Deref, DerefMut}; +# struct Carton(std::ptr::NonNull); + impl Deref for Carton { type Target = T; @@ -165,7 +167,8 @@ safely be Send unless it shares mutable state with something else without enforcing exclusive access to it. Each `Carton` has a unique pointer, so we're good. -```rust,ignore +```rust +# struct Carton(std::ptr::NonNull); // Safety: No one besides us has the raw pointer, so we can safely transfer the // Carton to another thread if T can be safely transferred. unsafe impl Send for Carton where T: Send {} @@ -178,7 +181,8 @@ write to the pointer, and the borrow checker enforces that mutable references must be exclusive, there are no soundness issues making `Carton` sync either. -```rust,ignore +```rust +# struct Carton(std::ptr::NonNull); // Safety: Our implementation of DerefMut requires writers to mutably borrow the // Carton, so the borrow checker will only let us have references to the Carton // on multiple threads if no one has a mutable reference to the Carton. This