arc.rs (17f671602cc6a15e65869c387492c5753c6f3cd5) arc.rs (92a655ae00a2f15fdd80eb96cd23526c0d8f0bfd)
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.

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

250/// }
251///
252/// let obj = Arc::try_new(Example)?;
253/// let cloned = do_something(obj.as_arc_borrow());
254///
255/// // Assert that both `obj` and `cloned` point to the same underlying object.
256/// assert!(core::ptr::eq(&*obj, &*cloned));
257/// ```
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.

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

250/// }
251///
252/// let obj = Arc::try_new(Example)?;
253/// let cloned = do_something(obj.as_arc_borrow());
254///
255/// // Assert that both `obj` and `cloned` point to the same underlying object.
256/// assert!(core::ptr::eq(&*obj, &*cloned));
257/// ```
258///
259/// Using `ArcBorrow<T>` as the type of `self`:
260///
261/// ```
262/// use crate::sync::{Arc, ArcBorrow};
263///
264/// struct Example {
265/// a: u32,
266/// b: u32,
267/// }
268///
269/// impl Example {
270/// fn use_reference(self: ArcBorrow<'_, Self>) {
271/// // ...
272/// }
273/// }
274///
275/// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
276/// obj.as_arc_borrow().use_reference();
277/// ```
258pub struct ArcBorrow<'a, T: ?Sized + 'a> {
259 inner: NonNull<ArcInner<T>>,
260 _p: PhantomData<&'a ()>,
261}
262
278pub struct ArcBorrow<'a, T: ?Sized + 'a> {
279 inner: NonNull<ArcInner<T>>,
280 _p: PhantomData<&'a ()>,
281}
282
283// This is to allow [`ArcBorrow`] (and variants) to be used as the type of `self`.
284impl<T: ?Sized> core::ops::Receiver for ArcBorrow<'_, T> {}
285
263impl<T: ?Sized> Clone for ArcBorrow<'_, T> {
264 fn clone(&self) -> Self {
265 *self
266 }
267}
268
269impl<T: ?Sized> Copy for ArcBorrow<'_, T> {}
270

--- 37 unchanged lines hidden ---
286impl<T: ?Sized> Clone for ArcBorrow<'_, T> {
287 fn clone(&self) -> Self {
288 *self
289 }
290}
291
292impl<T: ?Sized> Copy for ArcBorrow<'_, T> {}
293

--- 37 unchanged lines hidden ---