mirror of https://github.com/rust-lang/nomicon
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.2 KiB
84 lines
2.2 KiB
5 years ago
|
# Final Code
|
||
|
|
||
|
Here's the final code, with some added comments and re-ordered imports:
|
||
4 years ago
|
|
||
5 years ago
|
```rust
|
||
|
use std::marker::PhantomData;
|
||
|
use std::ops::Deref;
|
||
|
use std::ptr::NonNull;
|
||
|
use std::sync::atomic::{self, AtomicUsize, Ordering};
|
||
|
|
||
|
pub struct Arc<T> {
|
||
|
ptr: NonNull<ArcInner<T>>,
|
||
5 years ago
|
phantom: PhantomData<ArcInner<T>>,
|
||
5 years ago
|
}
|
||
|
|
||
|
pub struct ArcInner<T> {
|
||
|
rc: AtomicUsize,
|
||
|
data: T,
|
||
|
}
|
||
|
|
||
|
impl<T> Arc<T> {
|
||
|
pub fn new(data: T) -> Arc<T> {
|
||
|
// We start the reference count at 1, as that first reference is the
|
||
|
// current pointer.
|
||
|
let boxed = Box::new(ArcInner {
|
||
|
rc: AtomicUsize::new(1),
|
||
|
data,
|
||
|
});
|
||
|
Arc {
|
||
|
// It is okay to call `.unwrap()` here as we get a pointer from
|
||
|
// `Box::into_raw` which is guaranteed to not be null.
|
||
|
ptr: NonNull::new(Box::into_raw(boxed)).unwrap(),
|
||
5 years ago
|
phantom: PhantomData,
|
||
5 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
unsafe impl<T: Sync + Send> Send for Arc<T> {}
|
||
|
unsafe impl<T: Sync + Send> Sync for Arc<T> {}
|
||
|
|
||
5 years ago
|
impl<T> Deref for Arc<T> {
|
||
|
type Target = T;
|
||
|
|
||
|
fn deref(&self) -> &T {
|
||
|
let inner = unsafe { self.ptr.as_ref() };
|
||
|
&inner.data
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
impl<T> Clone for Arc<T> {
|
||
|
fn clone(&self) -> Arc<T> {
|
||
5 years ago
|
let inner = unsafe { self.ptr.as_ref() };
|
||
5 years ago
|
// Using a relaxed ordering is alright here as we don't need any atomic
|
||
|
// synchronization here as we're not modifying or accessing the inner
|
||
|
// data.
|
||
5 years ago
|
let old_rc = inner.rc.fetch_add(1, Ordering::Relaxed);
|
||
5 years ago
|
|
||
5 years ago
|
if old_rc >= isize::MAX as usize {
|
||
5 years ago
|
std::process::abort();
|
||
|
}
|
||
|
|
||
5 years ago
|
Self {
|
||
|
ptr: self.ptr,
|
||
5 years ago
|
phantom: PhantomData,
|
||
5 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<T> Drop for Arc<T> {
|
||
|
fn drop(&mut self) {
|
||
5 years ago
|
let inner = unsafe { self.ptr.as_ref() };
|
||
|
if inner.rc.fetch_sub(1, Ordering::Release) != 1 {
|
||
5 years ago
|
return;
|
||
|
}
|
||
|
// This fence is needed to prevent reordering of the use and deletion
|
||
|
// of the data.
|
||
|
atomic::fence(Ordering::Acquire);
|
||
|
// This is safe as we know we have the last pointer to the `ArcInner`
|
||
|
// and that its pointer is valid.
|
||
|
unsafe { Box::from_raw(self.ptr.as_ptr()); }
|
||
|
}
|
||
|
}
|
||
|
```
|