pub struct ArcBorrow<'a, T: ?Sized + 'a> { /* private fields */ }
Expand description
A borrowed reference to an Arc
instance.
For cases when one doesn’t ever need to increment the refcount on the allocation, it is simpler
to use just &T
, which we can trivially get from an Arc<T>
instance.
However, when one may need to increment the refcount, it is preferable to use an ArcBorrow<T>
over &Arc<T>
because the latter results in a double-indirection: a pointer (shared reference)
to a pointer (Arc<T>
) to the object (T
). An ArcBorrow
eliminates this double
indirection while still allowing one to increment the refcount and getting an Arc<T>
when/if
needed.
§Invariants
There are no mutable references to the underlying Arc
, and it remains valid for the
lifetime of the ArcBorrow
instance.
§Examples
use kernel::sync::{Arc, ArcBorrow};
struct Example;
fn do_something(e: ArcBorrow<'_, Example>) -> Arc<Example> {
e.into()
}
let obj = Arc::new(Example, GFP_KERNEL)?;
let cloned = do_something(obj.as_arc_borrow());
// Assert that both `obj` and `cloned` point to the same underlying object.
assert!(core::ptr::eq(&*obj, &*cloned));
Using ArcBorrow<T>
as the type of self
:
use kernel::sync::{Arc, ArcBorrow};
struct Example {
a: u32,
b: u32,
}
impl Example {
fn use_reference(self: ArcBorrow<'_, Self>) {
// ...
}
}
let obj = Arc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?;
obj.as_arc_borrow().use_reference();
Implementations§
Source§impl<T: ?Sized> ArcBorrow<'_, T>
impl<T: ?Sized> ArcBorrow<'_, T>
Sourcepub unsafe fn from_raw(ptr: *const T) -> Self
pub unsafe fn from_raw(ptr: *const T) -> Self
Creates an ArcBorrow
to an Arc
that has previously been deconstructed with
Arc::into_raw
or Arc::as_ptr
.
§Safety
- The provided pointer must originate from a call to
Arc::into_raw
orArc::as_ptr
. - For the duration of the lifetime annotated on this
ArcBorrow
, the reference count must not hit zero. - For the duration of the lifetime annotated on this
ArcBorrow
, there must not be aUniqueArc
reference to this value.
Trait Implementations§
impl<'a, T: ?Sized + 'a + Unsize<__S>, __S: ?Sized + 'a> CoerceUnsized<ArcBorrow<'a, __S>> for ArcBorrow<'a, T>
impl<T: ?Sized> Copy for ArcBorrow<'_, T>
impl<'a, T: ?Sized + 'a + Unsize<__S>, __S: ?Sized + 'a> DispatchFromDyn<ArcBorrow<'a, __S>> for ArcBorrow<'a, T>
Auto Trait Implementations§
impl<'a, T> Freeze for ArcBorrow<'a, T>where
T: ?Sized,
impl<'a, T> !RefUnwindSafe for ArcBorrow<'a, T>
impl<'a, T> !Send for ArcBorrow<'a, T>
impl<'a, T> !Sync for ArcBorrow<'a, T>
impl<'a, T> Unpin for ArcBorrow<'a, T>where
T: ?Sized,
impl<'a, T> !UnwindSafe for ArcBorrow<'a, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> PinInit<T> for T
impl<T> PinInit<T> for T
Source§unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>
unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>
slot
. Read more