[][src]Struct abi_stable::std_types::RArc

#[repr(C)]pub struct RArc<T> { /* fields omitted */ }

Ffi-safe version of std::sync::Arc

Example

Using an RArc<RMutex<RVec<u32>>> to get a list populated from multiple threads.

use abi_stable::{
    external_types::RMutex,
    std_types::{RArc,RVec},
};

use std::thread;

let arc=RArc::new(RMutex::new(RVec::new()));

{
    let arc2=RArc::clone(&arc);
    assert!( std::ptr::eq(&*arc,&*arc2) );
}

let mut guards=Vec::new();

for i in 0..10_u64 {
    let arc=RArc::clone(&arc);
    guards.push(thread::spawn(move||{
        for j in 0..100_u64{
            arc.lock().push(i*100+j);
        }
    }));
}

for guard in guards{
    guard.join().unwrap();
}

let mut vec=RArc::try_unwrap(arc)
    .ok()
    .expect("All the threads were joined,so this must be the only RArc")
    .into_inner();

vec.sort();

assert_eq!( vec, (0..1000).collect::<RVec<_>>() );

Implementations

impl<T> RArc<T>[src]

pub fn new(this: T) -> Self[src]

Constructs an RArc from a value.

Example

use abi_stable::std_types::RArc;

let arc=RArc::new(100);

pub fn into_arc(this: Self) -> Arc<T> where
    T: Clone
[src]

Converts this RArc<T> into an Arc<T>

Allocators

RArc<T> cannot always be converted to an Arc<T>, because their allocators might be different.

When is T cloned

T is cloned if the current dynamic_library/executable is not the one that created the RArc<T>, and the strong count is greater than 1.

Example

use abi_stable::std_types::RArc;
use std::sync::Arc;

let arc=RArc::new(100);

assert_eq!( RArc::into_arc(arc), Arc::new(100) );

pub fn try_unwrap(this: Self) -> Result<T, Self>[src]

Attempts to unwrap this RArc<T> into a T, returns Err(self) if the RArc<T>'s strong count is greater than 1.

Example

use abi_stable::std_types::RArc;
 
let arc0=RArc::new(100);
assert_eq!( RArc::try_unwrap(arc0), Ok(100) );

let arc1=RArc::new(100);
let arc1_clone=RArc::clone(&arc1);
assert_eq!( RArc::try_unwrap(arc1), Err(arc1_clone.clone()) );

pub fn get_mut(this: &mut Self) -> Option<&mut T>[src]

Attempts to create a mutable reference to T, failing if the RArc<T>'s strong count is greater than 1.

Example

use abi_stable::std_types::RArc;
 
let mut arc0=RArc::new(100);
*RArc::get_mut(&mut arc0).unwrap()+=400;
assert_eq!( *arc0, 500 );

let mut arc1=RArc::new(100);
let _arc1_clone=RArc::clone(&arc1);
assert_eq!( RArc::get_mut(&mut arc1), None );

pub fn make_mut(this: &mut Self) -> &mut T where
    T: Clone
[src]

Makes a mutable reference to T.

If there are other RArc<T>s pointing to the same value, then T is cloned into a new RArc<T> to ensure unique ownership of the value.

Postconditions

After this call, the strong count of this will be 1, because either it was 1 before the call, or because a new RArc<T> was created to ensure unique ownership of T.

Example

use abi_stable::std_types::RArc;
 
let mut arc0=RArc::new(100);
*RArc::make_mut(&mut arc0)+=400;
assert_eq!( *arc0, 500 );

let mut arc1=RArc::new(100);
let arc1_clone=RArc::clone(&arc1);
*RArc::make_mut(&mut arc1)+=400;
assert_eq!( *arc1, 500 );
assert_eq!( *arc1_clone, 100 );

pub fn strong_count(this: &Self) -> usize[src]

Gets the number of RArc that point to the value.

Example

use abi_stable::std_types::RArc;
 
let arc=RArc::new(0);
assert_eq!( RArc::strong_count(&arc), 1 );

let clone=RArc::clone(&arc);
assert_eq!( RArc::strong_count(&arc), 2 );

pub fn weak_count(this: &Self) -> usize[src]

Gets the number of std::sync::Weak that point to the value.

Example

use abi_stable::std_types::RArc;
 
use std::sync::Arc;
 
let rustarc=Arc::new(0);
let arc=RArc::from(rustarc.clone());
assert_eq!( RArc::weak_count(&arc), 0 );
 
let weak_0=Arc::downgrade(&rustarc);
assert_eq!( RArc::weak_count(&arc), 1 );

let weak_1=Arc::downgrade(&rustarc);
assert_eq!( RArc::weak_count(&arc), 2 );

Trait Implementations

impl<T> AsRef<T> for RArc<T>[src]

impl<T> Borrow<T> for RArc<T>[src]

impl<T, O> CanTransmuteElement<O> for RArc<T>[src]

type TransmutedPtr = RArc<O>

The type of the pointer after it's element type has been changed.

impl<T> Clone for RArc<T>[src]

impl<T> Debug for RArc<T> where
    T: Debug
[src]

impl<T> Default for RArc<T> where
    T: Default
[src]

impl<T> Deref for RArc<T>[src]

type Target = T

The resulting type after dereferencing.

impl<'de, T> Deserialize<'de> for RArc<T> where
    T: Deserialize<'de>, 
[src]

impl<T> Display for RArc<T> where
    T: Display
[src]

impl<T> Drop for RArc<T>[src]

impl<T> Eq for RArc<T> where
    T: Eq
[src]

impl<T> From<Arc<T>> for RArc<T>[src]

impl<T> GetPointerKind for RArc<T>[src]

type Kind = PK_SmartPointer

The kind of the pointer.

impl<T> GetStaticEquivalent_ for RArc<T> where
    T: __StableAbi
[src]

type StaticEquivalent = _static_RArc<__GetStaticEquivalent<T>>

impl<T> Hash for RArc<T> where
    T: Hash
[src]

impl<T> Into<Arc<T>> for RArc<T> where
    T: Clone + StableAbi
[src]

impl<T> IntoReprRust for RArc<T> where
    T: Clone + StableAbi
[src]

type ReprRust = Arc<T>

The #[repr(Rust)] equivalent.

impl<T> Ord for RArc<T> where
    T: Ord
[src]

impl<T> PartialEq<RArc<T>> for RArc<T> where
    T: PartialEq
[src]

impl<T> PartialOrd<RArc<T>> for RArc<T> where
    T: PartialOrd
[src]

impl<T> Pointer for RArc<T>[src]

impl<T> Send for RArc<T> where
    T: Send + Sync
[src]

impl<T> Serialize for RArc<T> where
    T: Serialize
[src]

impl<T> StableAbi for RArc<T> where
    T: __StableAbi
[src]

type IsNonZeroType = False

Whether this type has a single invalid bit-pattern. Read more

impl<T> Sync for RArc<T> where
    T: Send + Sync
[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for RArc<T> where
    T: RefUnwindSafe

impl<T> Unpin for RArc<T> where
    T: Unpin

impl<T> UnwindSafe for RArc<T> where
    T: RefUnwindSafe + UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<'a, T> BorrowOwned<'a> for T where
    T: 'a + Clone
[src]

type ROwned = T

The owned type, stored in RCow::Owned

type RBorrowed = &'a T

The borrowed type, stored in RCow::Borrowed

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T> GetWithMetadata for T[src]

type ForSelf = WithMetadata_<T, T>

This is always WithMetadata_<Self, Self>

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> SelfOps for T where
    T: ?Sized
[src]

impl<T> StringExt for T where
    T: Borrow<str> + ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<This> TransmuteElement for This where
    This: ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The error type returned when the conversion fails.

impl<T> TypeIdentity for T where
    T: ?Sized
[src]

type Type = T

The same type as Self. Read more

impl<This> ValidTag_Bounds for This where
    This: Debug + Clone + PartialEq<This>, 
[src]