rstd 0.1.0
Loading...
Searching...
No Matches
alloc::sync::Arc< T > Class Template Referenceexport
module rstd.alloc

A thread-safe reference-counting pointer, analogous to Rust's Arc<T>. More...

Public Member Functions

void reset ()
 Drops the current allocation, decrementing the strong count.
 
auto assume_init ()
 Converts an Arc<MaybeUninit<T>> into an Arc<T> after the value has been initialized.
 
ref< T > operator* () noexcept
 Dereferences the Arc, returning a mutable reference to the inner value.
 
ref< const T > operator* () const noexcept
 Dereferences the Arc, returning a const reference to the inner value.
 
T * operator-> () noexcept
 Provides pointer-like mutable access to the inner value.
 
T * operator-> () const noexcept
 Provides pointer-like const access to the inner value.
 
 operator bool () const noexcept
 Returns true if this Arc is non-empty.
 
usize strong_count () const noexcept
 Returns the number of strong (Arc) pointers to the same allocation.
 
usize weak_count () const noexcept
 Returns the number of Weak pointers to the same allocation.
 
Weak< T > downgrade () const noexcept
 Creates a Weak pointer to the same allocation without incrementing the strong count.
 
auto as_ptr () const noexcept
 Returns a raw pointer to the managed value.
 
auto get_mut () const noexcept -> Option< mut_ref< T > >
 Returns a mutable reference to the inner value if there are no other Arc or Weak pointers to the same allocation.
 
auto try_unwrap () -> Result< T, Arc >
 Attempts to unwrap the Arc, returning the inner value if this is the only strong reference.
 
auto into_raw () noexcept
 Consumes the Arc without decrementing the reference count, returning an ArcRaw.
 

Static Public Member Functions

template<typename... Args>
static auto make (Args &&... args) -> Arc
 Constructs a new Arc<T>.
 
static auto make_uninit () -> Arc< MaybeUninit< T > >
 Creates a new Arc containing an uninitialized value.
 
static auto pin (rstd::param_t< T > value) -> Pin< Arc< T > >
 Creates a new Pin<Arc<T>>.
 
static Arc from_raw (ArcRaw< T > r) noexcept
 Reconstructs an Arc from an ArcRaw previously obtained via into_raw.
 
static bool ptr_eq (const Arc &a, const Arc &b) noexcept
 Returns true if the two Arcs point to the same allocation (not just values that compare as equal).
 
static bool is_unique (const Arc &arc) noexcept
 Returns true if this is the only Arc or Weak pointer to the allocation.
 

Friends

template<typename , typename >
struct rstd::Impl
 

Detailed Description

template<typename T>
class alloc::sync::Arc< T >

A thread-safe reference-counting pointer, analogous to Rust's Arc<T>.

Template Parameters
TThe type of the value managed by atomic reference counting.

Member Function Documentation

◆ assume_init()

template<typename T >
auto alloc::sync::Arc< T >::assume_init ( )
inline

Converts an Arc<MaybeUninit<T>> into an Arc<T> after the value has been initialized.

Returns
An Arc<T> over the now-initialized value.

◆ downgrade()

template<class T >
auto alloc::sync::Arc< T >::downgrade ( ) const
noexcept

Creates a Weak pointer to the same allocation without incrementing the strong count.

Returns
A Weak<T> pointer.

◆ from_raw()

template<typename T >
static Arc alloc::sync::Arc< T >::from_raw ( ArcRaw< T > r)
inlinestaticnoexcept

Reconstructs an Arc from an ArcRaw previously obtained via into_raw.

Parameters
rThe raw Arc representation.
Returns
An Arc that takes back ownership.

◆ into_raw()

template<typename T >
auto alloc::sync::Arc< T >::into_raw ( )
inlinenoexcept

Consumes the Arc without decrementing the reference count, returning an ArcRaw.

Returns
An ArcRaw<T> for low-level interop; must be reconverted with from_raw.

◆ make_uninit()

template<typename T >
static auto alloc::sync::Arc< T >::make_uninit ( ) -> Arc<MaybeUninit<T>>
inlinestatic

Creates a new Arc containing an uninitialized value.

The returned Arc contains a MaybeUninit<T> that must be initialized before calling assume_init() to convert to Arc<T>.

Example

auto arc = Arc<int>::make_uninit();
// Initialize the value
Arc::get_mut(arc).unwrap().write(42);
auto initialized = arc.assume_init();
static auto make_uninit() -> Arc< MaybeUninit< T > >
Creates a new Arc containing an uninitialized value.
Definition sync.cppm:287
auto get_mut() const noexcept -> Option< mut_ref< T > >
Returns a mutable reference to the inner value if there are no other Arc or Weak pointers to the same...
Definition sync.cppm:372

◆ pin()

template<typename T >
static auto alloc::sync::Arc< T >::pin ( rstd::param_t< T > value) -> Pin<Arc<T>>
inlinestatic

Creates a new Pin<Arc<T>>.

If T does not implement Unpin, then the value will be pinned in memory and unable to be moved.

This is useful for types that must not be moved after creation, such as self-referential structures.

◆ strong_count()

template<typename T >
usize alloc::sync::Arc< T >::strong_count ( ) const
inlinenoexcept

Returns the number of strong (Arc) pointers to the same allocation.

Returns
The current strong reference count.

◆ try_unwrap()

template<typename T >
auto alloc::sync::Arc< T >::try_unwrap ( ) -> Result<T, Arc>
inline

Attempts to unwrap the Arc, returning the inner value if this is the only strong reference.

Returns
Ok(T) if successful, or Err(Arc) if other strong references exist.

◆ weak_count()

template<typename T >
usize alloc::sync::Arc< T >::weak_count ( ) const
inlinenoexcept

Returns the number of Weak pointers to the same allocation.

Returns
The current weak reference count (excluding the implicit weak held by strong pointers).

The documentation for this class was generated from the following file: