Struct abi_stable::std_types::RArc
source · #[repr(C)]pub struct RArc<T> { /* private fields */ }
Expand description
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§
source§impl<T> RArc<T>
impl<T> RArc<T>
sourcepub fn into_arc(this: Self) -> Arc<T>where
T: Clone,
pub fn into_arc(this: Self) -> Arc<T>where
T: Clone,
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));
sourcepub fn try_unwrap(this: Self) -> Result<T, Self>
pub fn try_unwrap(this: Self) -> Result<T, Self>
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()));
sourcepub fn get_mut(this: &mut Self) -> Option<&mut T>
pub fn get_mut(this: &mut Self) -> Option<&mut T>
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);
sourcepub fn make_mut(this: &mut Self) -> &mut Twhere
T: Clone,
pub fn make_mut(this: &mut Self) -> &mut Twhere
T: Clone,
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);
sourcepub fn strong_count(this: &Self) -> usize
pub fn strong_count(this: &Self) -> usize
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);
sourcepub fn weak_count(this: &Self) -> usize
pub fn weak_count(this: &Self) -> usize
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§
source§impl<T, O> CanTransmuteElement<O> for RArc<T>
impl<T, O> CanTransmuteElement<O> for RArc<T>
§type TransmutedPtr = RArc<O>
type TransmutedPtr = RArc<O>
source§unsafe fn transmute_element_(self) -> Self::TransmutedPtr
unsafe fn transmute_element_(self) -> Self::TransmutedPtr
source§impl<'de, T> Deserialize<'de> for RArc<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for RArc<T>where
T: Deserialize<'de>,
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl<T> GetPointerKind for RArc<T>
impl<T> GetPointerKind for RArc<T>
source§impl<T> GetStaticEquivalent_ for RArc<T>where
T: __StableAbi,
impl<T> GetStaticEquivalent_ for RArc<T>where
T: __StableAbi,
§type StaticEquivalent = _static_RArc<<T as GetStaticEquivalent_>::StaticEquivalent>
type StaticEquivalent = _static_RArc<<T as GetStaticEquivalent_>::StaticEquivalent>
'static
equivalent of Self
source§impl<T> IntoReprRust for RArc<T>
impl<T> IntoReprRust for RArc<T>
source§impl<T> Ord for RArc<T>where
T: Ord,
impl<T> Ord for RArc<T>where
T: Ord,
source§impl<T> PartialEq for RArc<T>where
T: PartialEq,
impl<T> PartialEq for RArc<T>where
T: PartialEq,
source§impl<T> PartialOrd for RArc<T>where
T: PartialOrd,
impl<T> PartialOrd for RArc<T>where
T: PartialOrd,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<T> StableAbi for RArc<T>where
T: __StableAbi,
impl<T> StableAbi for RArc<T>where
T: __StableAbi,
§type IsNonZeroType = False
type IsNonZeroType = False
source§const LAYOUT: &'static TypeLayout = _
const LAYOUT: &'static TypeLayout = _
source§const ABI_CONSTS: AbiConsts = _
const ABI_CONSTS: AbiConsts = _
const
-equivalents of the associated types.impl<T> Eq for RArc<T>where
T: Eq,
impl<T> Send for RArc<T>
impl<T> Sync for RArc<T>
impl<T> Unpin for RArc<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for RArc<T>where
T: RefUnwindSafe,
impl<T> UnwindSafe for RArc<T>where
T: UnwindSafe + RefUnwindSafe,
Blanket Implementations§
source§impl<T> AlignerFor<1> for T
impl<T> AlignerFor<1> for T
source§impl<T> AlignerFor<1024> for T
impl<T> AlignerFor<1024> for T
§type Aligner = AlignTo1024<T>
type Aligner = AlignTo1024<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<128> for T
impl<T> AlignerFor<128> for T
§type Aligner = AlignTo128<T>
type Aligner = AlignTo128<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<16> for T
impl<T> AlignerFor<16> for T
source§impl<T> AlignerFor<16384> for T
impl<T> AlignerFor<16384> for T
§type Aligner = AlignTo16384<T>
type Aligner = AlignTo16384<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<2> for T
impl<T> AlignerFor<2> for T
source§impl<T> AlignerFor<2048> for T
impl<T> AlignerFor<2048> for T
§type Aligner = AlignTo2048<T>
type Aligner = AlignTo2048<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<256> for T
impl<T> AlignerFor<256> for T
§type Aligner = AlignTo256<T>
type Aligner = AlignTo256<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<32> for T
impl<T> AlignerFor<32> for T
source§impl<T> AlignerFor<32768> for T
impl<T> AlignerFor<32768> for T
§type Aligner = AlignTo32768<T>
type Aligner = AlignTo32768<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<4> for T
impl<T> AlignerFor<4> for T
source§impl<T> AlignerFor<4096> for T
impl<T> AlignerFor<4096> for T
§type Aligner = AlignTo4096<T>
type Aligner = AlignTo4096<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<512> for T
impl<T> AlignerFor<512> for T
§type Aligner = AlignTo512<T>
type Aligner = AlignTo512<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<64> for T
impl<T> AlignerFor<64> for T
source§impl<T> AlignerFor<8> for T
impl<T> AlignerFor<8> for T
source§impl<T> AlignerFor<8192> for T
impl<T> AlignerFor<8192> for T
§type Aligner = AlignTo8192<T>
type Aligner = AlignTo8192<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.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<'a, T> RCowCompatibleRef<'a> for Twhere
T: Clone + 'a,
impl<'a, T> RCowCompatibleRef<'a> for Twhere
T: Clone + 'a,
source§fn as_c_ref(from: &'a T) -> <T as RCowCompatibleRef<'a>>::RefC
fn as_c_ref(from: &'a T) -> <T as RCowCompatibleRef<'a>>::RefC
source§fn as_rust_ref(from: <T as RCowCompatibleRef<'a>>::RefC) -> &'a T
fn as_rust_ref(from: <T as RCowCompatibleRef<'a>>::RefC) -> &'a T
source§impl<S> ROExtAcc for S
impl<S> ROExtAcc for S
source§fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
offset
. Read moresource§fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
offset
. Read moresource§fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
offset
. Read moresource§fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
offset
. Read moresource§impl<S> ROExtOps<Aligned> for S
impl<S> ROExtOps<Aligned> for S
source§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
offset
) with value
,
returning the previous value of the field. Read moresource§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
source§impl<S> ROExtOps<Unaligned> for S
impl<S> ROExtOps<Unaligned> for S
source§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
offset
) with value
,
returning the previous value of the field. Read moresource§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
source§impl<T> SelfOps for Twhere
T: ?Sized,
impl<T> SelfOps for Twhere
T: ?Sized,
source§fn piped<F, U>(self, f: F) -> U
fn piped<F, U>(self, f: F) -> U
source§fn piped_ref<'a, F, U>(&'a self, f: F) -> U
fn piped_ref<'a, F, U>(&'a self, f: F) -> U
piped
except that the function takes &Self
Useful for functions that take &Self
instead of Self
. Read moresource§fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
piped
, except that the function takes &mut Self
.
Useful for functions that take &mut Self
instead of Self
.source§fn mutated<F>(self, f: F) -> Self
fn mutated<F>(self, f: F) -> Self
source§fn observe<F>(self, f: F) -> Self
fn observe<F>(self, f: F) -> Self
source§fn as_ref_<T>(&self) -> &T
fn as_ref_<T>(&self) -> &T
AsRef
,
using the turbofish .as_ref_::<_>()
syntax. Read moresource§impl<T> StringExt for T
impl<T> StringExt for T
source§fn previous_char_boundary(&self, index: usize) -> usize
fn previous_char_boundary(&self, index: usize) -> usize
source§fn next_char_boundary(&self, index: usize) -> usize
fn next_char_boundary(&self, index: usize) -> usize
source§fn left_char_boundary(&self, index: usize) -> usize
fn left_char_boundary(&self, index: usize) -> usize
source§fn right_char_boundary(&self, index: usize) -> usize
fn right_char_boundary(&self, index: usize) -> usize
source§fn split_while<P, T, 'a>(&'a self, mapper: P) -> SplitWhile<'a, P, T>
fn split_while<P, T, 'a>(&'a self, mapper: P) -> SplitWhile<'a, P, T>
mapper
. Read moresource§fn rsplit_while<P, T, 'a>(&'a self, mapper: P) -> RSplitWhile<'a, P, T>
fn rsplit_while<P, T, 'a>(&'a self, mapper: P) -> RSplitWhile<'a, P, T>
split_while
that iterates
from the right(the order of substrings is reversed). Read moresource§fn get_nth_char_index(&self, nth: usize) -> Option<usize>
fn get_nth_char_index(&self, nth: usize) -> Option<usize>
nth
character Read moresource§fn nth_char(&self, nth: usize) -> Option<char>
fn nth_char(&self, nth: usize) -> Option<char>
nth
character in the str. Read moresource§fn first_chars(&self, n: usize) -> &str
fn first_chars(&self, n: usize) -> &str
n
chars. Read moresource§fn calc_len_utf16(&self) -> usize
fn calc_len_utf16(&self) -> usize
source§fn get_char_at(&self, at_byte: usize) -> Option<char>
fn get_char_at(&self, at_byte: usize) -> Option<char>
at_byte
index inside of the string,
returning None
if the index is outside the string. Read moresource§fn char_indices_to(&self, to: usize) -> CharIndices<'_>
fn char_indices_to(&self, to: usize) -> CharIndices<'_>
to
byte. Read moresource§fn char_indices_from(&self, from: usize) -> CharIndicesFrom<'_>
fn char_indices_from(&self, from: usize) -> CharIndicesFrom<'_>
from
byte. Read moresource§fn left_pad(&self, how_much: usize) -> String
fn left_pad(&self, how_much: usize) -> String
how_much
additional spaces. Read moresource§fn left_padder<'a>(&'a self, how_much: usize) -> LeftPadder<'a>
fn left_padder<'a>(&'a self, how_much: usize) -> LeftPadder<'a>
how_much
additional
spaces in its Display
impl. Read more