arc.rs (f8110cd157833e721f50f779dc70f8ae5b429832) arc.rs (d701e061cb14f589f8c4f48fd7fbe81c0e34b7e7)
1// SPDX-License-Identifier: GPL-2.0
2
3//! A reference-counted pointer.
4//!
5//! This module implements a way for users to create reference-counted objects and pointers to
6//! them. Such a pointer automatically increments and decrements the count, and drops the
7//! underlying object when it reaches zero. It is also safe to use concurrently from multiple
8//! threads.

--- 136 unchanged lines hidden (view full) ---

145impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<Arc<U>> for Arc<T> {}
146
147// SAFETY: It is safe to send `Arc<T>` to another thread when the underlying `T` is `Sync` because
148// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
149// `T` to be `Send` because any thread that has an `Arc<T>` may ultimately access `T` using a
150// mutable reference when the reference count reaches zero and `T` is dropped.
151unsafe impl<T: ?Sized + Sync + Send> Send for Arc<T> {}
152
1// SPDX-License-Identifier: GPL-2.0
2
3//! A reference-counted pointer.
4//!
5//! This module implements a way for users to create reference-counted objects and pointers to
6//! them. Such a pointer automatically increments and decrements the count, and drops the
7//! underlying object when it reaches zero. It is also safe to use concurrently from multiple
8//! threads.

--- 136 unchanged lines hidden (view full) ---

145impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<Arc<U>> for Arc<T> {}
146
147// SAFETY: It is safe to send `Arc<T>` to another thread when the underlying `T` is `Sync` because
148// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
149// `T` to be `Send` because any thread that has an `Arc<T>` may ultimately access `T` using a
150// mutable reference when the reference count reaches zero and `T` is dropped.
151unsafe impl<T: ?Sized + Sync + Send> Send for Arc<T> {}
152
153// SAFETY: It is safe to send `&Arc<T>` to another thread when the underlying `T` is `Sync` for the
154// same reason as above. `T` needs to be `Send` as well because a thread can clone an `&Arc<T>`
155// into an `Arc<T>`, which may lead to `T` being accessed by the same reasoning as above.
153// SAFETY: It is safe to send `&Arc<T>` to another thread when the underlying `T` is `Sync`
154// because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
155// it needs `T` to be `Send` because any thread that has a `&Arc<T>` may clone it and get an
156// `Arc<T>` on that thread, so the thread may ultimately access `T` using a mutable reference when
157// the reference count reaches zero and `T` is dropped.
156unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}
157
158impl<T> Arc<T> {
159 /// Constructs a new reference counted instance of `T`.
160 pub fn try_new(contents: T) -> Result<Self, AllocError> {
161 // INVARIANT: The refcount is initialised to a non-zero value.
162 let value = ArcInner {
163 // SAFETY: There are no safety requirements for this FFI call.

--- 468 unchanged lines hidden ---
158unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}
159
160impl<T> Arc<T> {
161 /// Constructs a new reference counted instance of `T`.
162 pub fn try_new(contents: T) -> Result<Self, AllocError> {
163 // INVARIANT: The refcount is initialised to a non-zero value.
164 let value = ArcInner {
165 // SAFETY: There are no safety requirements for this FFI call.

--- 468 unchanged lines hidden ---