Struct SendRc

Source
pub struct SendRc<T> { /* private fields */ }
Expand description

Reference-counting pointer like Rc<T>, but which is Send if T is Send.

When created, a SendRc is pinned to the current thread, and is usable only within it. Before sending it to a different thread, you must use SendRc::pre_send() to park all the SendRcs that point to the same value. This will make the values temporarily inaccessible through SendRcs, but will allow safe transfer. When done parking, you can obtain a PostSend and send it to the other thread to restore access to the values there. For example:

// create two SendRcs pointing to a shared value
let mut r1 = SendRc::new(RefCell::new(1));
let mut r2 = SendRc::clone(&r1);

// prepare to send them to a different thread
let pre_send = SendRc::pre_send();
pre_send.park(&mut r1); // r1 and r2 cannot be dereferenced from this point
pre_send.park(&mut r2);
// ready() would panic if there were unparked SendRcs pointing to the value
let post_send = pre_send.ready();

// move everything to a different thread
std::thread::spawn(move || {
    // SendRcs are still unusable until unparked
    post_send.unpark();
    // they're again usable from this point, and only in this thread
    *r1.borrow_mut() += 1;
    assert_eq!(*r2.borrow(), 2);
})

This process may be repeated to send the SendRcs to another thread later.

Compared to Rc, tradeoffs of a SendRc are:

  • deref(), clone(), and drop() require a check that the shared value is not parked, and a check that we’re accessing it from the correct thread.
  • a SendRc takes up two machine words.
  • it currently doesn’t support weak pointers.

Implementations§

Source§

impl<T> SendRc<T>

Source

pub fn new(val: T) -> Self

Constructs a new SendRc<T>.

The SendRc is only usable from the current thread. To send and use it in another thread, you must call pre_send().

Source

pub fn pre_send() -> PreSend<T>

Prepare to send SendRcs to another thread.

To move a SendRc to a different thread, you must call park() on that pointer, as well as on all other SendRcs pointing to the same value.

let mut r1 = SendRc::new(RefCell::new(1));
let mut r2 = SendRc::clone(&r1);
let pre_send = SendRc::pre_send();
pre_send.park(&mut r1);
pre_send.park(&mut r2);
let post_send = pre_send.ready();
// post_send, r1, and r2 can now be send to a different thread, and re-enabled
// by calling post_send.unpark()
Source

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

Returns the number of SendRcs pointing to the value.

Panics when invoked from a different thread than the one the SendRc was created in or last pinned to.

Source

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

Returns the value if the SendRc has exactly one reference.

Panics when invoked from a different thread than the one the SendRc was created in or last pinned to.

Source

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

Returns a mutable reference to the value this points to, if no other SendRcs point to the same value.

Panics when invoked from a different thread than the one the SendRc was created in or last pinned to.

Source

pub fn ptr_eq(this: &Self, other: &Self) -> bool

Returns true if this and other point to the same value.

This method can be called from any thread.

Trait Implementations§

Source§

impl<T> AsRef<T> for SendRc<T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Borrow<T> for SendRc<T>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> Clone for SendRc<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for SendRc<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for SendRc<T>

Source§

fn default() -> SendRc<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Deref for SendRc<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T: Display> Display for SendRc<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for SendRc<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Hash> Hash for SendRc<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Ord> Ord for SendRc<T>

Source§

fn cmp(&self, other: &SendRc<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for SendRc<T>

Source§

fn eq(&self, other: &SendRc<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for SendRc<T>

Source§

fn partial_cmp(&self, other: &SendRc<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq> Eq for SendRc<T>

Source§

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

Auto Trait Implementations§

§

impl<T> Freeze for SendRc<T>

§

impl<T> !RefUnwindSafe for SendRc<T>

§

impl<T> !Sync for SendRc<T>

§

impl<T> Unpin for SendRc<T>

§

impl<T> !UnwindSafe for SendRc<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.