Struct ArcInner

Source
#[repr(C)]
pub struct ArcInner<T> { /* private fields */ }
Expand description

A type with the same layout as the allocated value of Arc that it’s initialized once you convert a mutable reference to it.

This type consist of the strong,weak count of the Arc and a MaybeUninit holding the data.The strong it’s set to zero until you cast to the first Arc with to_arc or to_arc_with thus upgrading the count.

§Examples

The first use-case of this type is lazy-init an mutable static across threads and drop it when all ceases to use it.

use stackarc::ArcInner;
use std::thread::{sleep, spawn};
use std::time::Duration;
 
static mut A: ArcInner<String> = ArcInner::uninit();
 
fn main() {
    let x = spawn(|| unsafe { 
        let _a = A.to_arc_with(|| format!("foobar")); 
        sleep(Duration::from_secs(2)); 
    });
 
    let y = spawn(|| unsafe {
        let _a = A.to_arc_with(|| format!("barfoo"));  
        sleep(Duration::from_secs(2)); 
    });
 
    // wait one second to wait for the threads to initialize the value
    // which in turn wait another to maintain their reference alive
    // and don't drop the count until create another
    sleep(Duration::from_secs(1)); 
    let z = unsafe { A.to_arc_with(|| format!("baz")) };
  
    assert_ne!(*z, "baz");
 
    drop(z);
    x.join().unwrap();
    y.join().unwrap();
    let z = unsafe { A.to_arc_with(|| format!("foo")) };
     
    assert_eq!(*z, "foo");
}

Implementations§

Source§

impl<T> ArcInner<T>

Source

pub const fn uninit() -> Self

Declares a new ArcInner in an uninit state,use to_arc_with or to_arc to initialize it.

Source

pub fn to_arc_with<F: FnOnce() -> T>(&mut self, default: F) -> Arc<T>

Returns an Arc after upgrading the counts and initialize the value calling default if the strong is zero.

A good example is at the type docs.

Source

pub fn to_arc(&mut self, x: T) -> Arc<T>

Returns an Arc after upgrading the counts and initialize the value with x if the strong is zero.

A good example is at the type docs.

Source

pub fn as_mut_ptr(&mut self) -> *mut T

Returns a raw mutable pointer to the inner data.

Source

pub fn as_ptr(&self) -> *const T

Extracts a raw pointer to the inner data.

Auto Trait Implementations§

§

impl<T> !Freeze for ArcInner<T>

§

impl<T> RefUnwindSafe for ArcInner<T>
where T: RefUnwindSafe,

§

impl<T> Send for ArcInner<T>
where T: Send,

§

impl<T> Sync for ArcInner<T>
where T: Sync,

§

impl<T> Unpin for ArcInner<T>
where T: Unpin,

§

impl<T> UnwindSafe for ArcInner<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.