File tree Expand file tree Collapse file tree 3 files changed +14
-14
lines changed Expand file tree Collapse file tree 3 files changed +14
-14
lines changed Original file line number Diff line number Diff line change @@ -25,7 +25,8 @@ impl<T> Arc<T> {
25
25
data,
26
26
});
27
27
Arc {
28
- // It is okay to call `.unwrap()` here as we get a pointer from `Box::into_raw` which is guaranteed to not be null.
28
+ // It is okay to call `.unwrap()` here as we get a pointer from
29
+ // `Box::into_raw` which is guaranteed to not be null.
29
30
ptr: NonNull::new(Box::into_raw(boxed)).unwrap(),
30
31
_marker: PhantomData,
31
32
}
@@ -84,8 +85,8 @@ impl<T> Arc<T> {
84
85
data,
85
86
});
86
87
Arc {
87
- // SOUNDNESS: It is sound to call `.unwrap()` here as we get a
88
- // pointer from `Box::into_raw` which is guaranteed to not be null.
88
+ // It is okay to call `.unwrap()` here as we get a pointer from
89
+ // `Box::into_raw` which is guaranteed to not be null.
89
90
ptr: NonNull::new(Box::into_raw(boxed)).unwrap(),
90
91
_marker: PhantomData,
91
92
}
Original file line number Diff line number Diff line change @@ -63,14 +63,12 @@ We'll need to import `std::sync::atomic` itself:
63
63
use std::sync::atomic;
64
64
```
65
65
66
- Finally, we can drop the data itself. We use ` std::ptr::drop_in_place ` as it is
67
- better than using ` std::ptr::read ` then dropping that object (see [ its
68
- documentation] ( https://doc.rust-lang.org/stable/std/ptr/fn.drop_in_place.html ) ).
69
- This takes a ` *mut T ` and not a ` NonNull<T> ` , so we must convert using
70
- ` NonNull::as_ptr ` .
66
+ Finally, we can drop the data itself. We use ` Box::from_raw ` to drop the boxed
67
+ ` ArcInner<T> ` and its data. This takes a ` *mut T ` and not a ` NonNull<T> ` , so we
68
+ must convert using ` NonNull::as_ptr ` .
71
69
72
70
``` rust,ignore
73
- unsafe { std::ptr::drop_in_place (self.ptr.as_ptr()) }
71
+ unsafe { stdBox::from_raw (self.ptr.as_ptr()) }
74
72
```
75
73
76
74
This is safe as we know we have the last pointer to the ` ArcInner ` and that its
@@ -88,7 +86,7 @@ impl<T> Drop for Arc<T> {
88
86
atomic::fence(Ordering::Acquire);
89
87
// This is safe as we know we have the last pointer to the `ArcInner`
90
88
// and that its pointer is valid.
91
- unsafe { std::ptr::drop_in_place (self.ptr.as_ptr()) }
89
+ unsafe { Box::from_raw (self.ptr.as_ptr()) }
92
90
}
93
91
}
94
92
```
Original file line number Diff line number Diff line change @@ -26,16 +26,17 @@ impl<T> Arc<T> {
26
26
data ,
27
27
});
28
28
Arc {
29
- // SOUNDNESS: It is sound to call `.unwrap()` here as we get a
30
- // pointer from `Box::into_raw` which is guaranteed to not be null.
29
+ // It is okay to call `.unwrap()` here as we get a pointer from
30
+ // `Box::into_raw` which is guaranteed to not be null.
31
31
ptr : NonNull :: new (Box :: into_raw (boxed )). unwrap (),
32
32
_marker : PhantomData ,
33
33
}
34
34
}
35
35
36
36
fn inner (& self ) -> & ArcInner <T > {
37
37
// This unsafety is okay because while this Arc is alive, we're
38
- // guaranteed that the inner pointer is valid.
38
+ // guaranteed that the inner pointer is valid. Also, ArcInner<T> is
39
+ // Sync if T is Sync.
39
40
unsafe { self . ptr. as_ref () }
40
41
}
41
42
}
@@ -65,7 +66,7 @@ impl<T> Drop for Arc<T> {
65
66
atomic :: fence (Ordering :: Acquire );
66
67
// This is safe as we know we have the last pointer to the `ArcInner`
67
68
// and that its pointer is valid.
68
- unsafe { std :: ptr :: drop_in_place (self . ptr. as_ptr ()) }
69
+ unsafe { Box :: from_raw (self . ptr. as_ptr ()) }
69
70
}
70
71
}
71
72
You can’t perform that action at this time.
0 commit comments