RefMut

Struct RefMut 

Source
pub struct RefMut<'a, T>
where T: ?Sized,
{ /* private fields */ }
Expand description

Wrapper for mutably borrowed AtomicCell that will released lock on drop.

This type can be dereferenced to [&mut T].

Implements Borrow<T>, BorrowMut<T>, AsRef<T> and AsMut<T> for convenience.

Implements Debug, Display, PartialEq, PartialOrd and Hash by delegating to T.

Implementations§

Source§

impl<'a, T> RefMut<'a, T>
where T: ?Sized,

Source

pub fn new(r: &'a mut T) -> RefMut<'a, T>

Wraps external reference into RefMut.

This function’s purpose is to satisfy type requirements where RefMut is required but reference does not live in AtomicCell.

§Examples
use atomicell::RefMut;

let mut value = 42;
let r = RefMut::new(&mut value);
Source

pub fn with_borrow(r: &'a mut T, borrow: AtomicBorrowMut<'a>) -> RefMut<'a, T>

Wraps external reference into RefMut. And associates it with provided AtomicBorrowMut

This function is intended to be used by AtomicCell or other abstractions that use AtomicBorrow for locking.

§Examples
use core::sync::atomic::AtomicIsize;
use atomicell::{borrow::{AtomicBorrowMut, new_lock}, RefMut};
let counter = new_lock();
let borrow = AtomicBorrowMut::try_new(&counter).unwrap();

let mut value = 42;
let r = RefMut::with_borrow(&mut value, borrow);
assert_eq!(*r, 42);
Source

pub fn into_split(r: RefMut<'a, T>) -> (NonNull<T>, AtomicBorrowMut<'a>)

Splits wrapper into two parts. One is reference to the value and the other is AtomicBorrowMut that guards it from being borrowed.

§Safety

User must ensure NonNull is not dereferenced after AtomicBorrowMut is dropped.

You must also treat the NonNull as invariant over T. This means that any custom wrapper types you make around the NonNull<T> must also be invariant over T. This can be done by adding a PhantomData<*mut T> field to the struct.

See the source definition of RefMut for an example.

§Examples
use atomicell::{AtomicCell, RefMut};

let cell = AtomicCell::new(42);
let r: RefMut<'_, i32> = cell.borrow_mut();

unsafe {
    let (r, borrow) = RefMut::into_split(r);
    assert_eq!(*r.as_ref(), 42);

    assert!(cell.try_borrow().is_none(), "Must not be able to borrow mutably yet");
    assert!(cell.try_borrow_mut().is_none(), "Must not be able to borrow mutably yet");
    drop(borrow);
    assert!(cell.try_borrow_mut().is_some(), "Must be able to borrow mutably now");
}
Source

pub fn map<F, U>(r: RefMut<'a, T>, f: F) -> RefMut<'a, U>
where F: FnOnce(&mut T) -> &mut U, U: ?Sized,

Makes a new RefMut for a component of the borrowed data.

The AtomicCell is already mutably borrowed, so this cannot fail.

This is an associated function that needs to be used as RefMut::map(…). A method would interfere with methods of the same name on the contents of a AtomicCell used through Deref.

§Examples
use atomicell::{AtomicCell, RefMut};

let c = AtomicCell::new((5, 'b'));
let b1: RefMut<(u32, char)> = c.borrow_mut();
let b2: RefMut<u32> = RefMut::map(b1, |t| &mut t.0);
assert_eq!(*b2, 5)
Source

pub fn filter_map<U, F>( r: RefMut<'a, T>, f: F, ) -> Result<RefMut<'a, U>, RefMut<'a, T>>
where F: FnOnce(&mut T) -> Option<&mut U>, U: ?Sized,

Makes a new RefMut for an optional component of the borrowed data. The original guard is returned as an Err(..) if the closure returns None.

The AtomicCell is already mutably borrowed, so this cannot fail.

This is an associated function that needs to be used as RefMut::filter_map(…). A method would interfere with methods of the same name on the contents of a AtomicCell used through Deref.

§Examples
use atomicell::{AtomicCell, RefMut};

let c = AtomicCell::new(vec![1, 2, 3]);

{
    let b1: RefMut<Vec<u32>> = c.borrow_mut();
    let mut b2: Result<RefMut<u32>, _> = RefMut::filter_map(b1, |v| v.get_mut(1));

    if let Ok(mut b2) = b2 {
        *b2 += 2;
    }
}

assert_eq!(*c.borrow(), vec![1, 4, 3]);
Source

pub fn map_split<U, V, F>( r: RefMut<'a, T>, f: F, ) -> (RefMut<'a, U>, RefMut<'a, V>)
where F: FnOnce(&mut T) -> (&mut U, &mut V), U: ?Sized, V: ?Sized,

Splits a RefMut into multiple RefMuts for different components of the borrowed data.

The AtomicCell is already immutably borrowed, so this cannot fail.

This is an associated function that needs to be used as RefMut::map_split(...). A method would interfere with methods of the same name on the contents of a AtomicCell used through Deref.

§Examples
use atomicell::{RefMut, AtomicCell};

let cell = AtomicCell::new([1, 2, 3, 4]);
let borrow = cell.borrow_mut();
let (begin, end) = RefMut::map_split(borrow, |slice| slice.split_at_mut(2));
assert_eq!(*begin, [1, 2]);
assert_eq!(*end, [3, 4]);
Source

pub fn leak(r: RefMut<'a, T>) -> &'a mut T

Convert into a reference to the underlying data.

The underlying AtomicCell can never be mutably borrowed from again and will always appear already immutably borrowed. It is not a good idea to leak more than a constant number of references. The AtomicCell can be immutably borrowed again if only a smaller number of leaks have occurred in total.

This is an associated function that needs to be used as RefMut::leak(…). A method would interfere with methods of the same name on the contents of a AtomicCell used through Deref.

§Examples
use atomicell::{AtomicCell, RefMut};
let cell = AtomicCell::new(0);

let value = RefMut::leak(cell.borrow_mut());
assert_eq!(*value, 0);
*value = 1;
assert_eq!(*value, 1);

assert!(cell.try_borrow().is_none());
assert!(cell.try_borrow_mut().is_none());
Source

pub fn as_mut<U>(r: RefMut<'a, T>) -> RefMut<'a, U>
where T: AsMut<U>, U: ?Sized,

Converts reference and returns result wrapped in the RefMut.

The AtomicCell is already mutably borrowed, so this cannot fail.

This is an associated function that needs to be used as RefMut::map_split(...). A method would interfere with methods of the same name on the contents of a AtomicCell used through Deref.

§Examples
use atomicell::{AtomicCell, RefMut};

let c = AtomicCell::new(String::from("hello"));
let b1: RefMut<String> = c.borrow_mut();
let mut b2: RefMut<str> = RefMut::as_mut(b1);
b2.make_ascii_uppercase();
assert_eq!(*b2, *"HELLO")
Source

pub fn as_deref_mut(r: RefMut<'a, T>) -> RefMut<'a, <T as Deref>::Target>
where T: DerefMut,

Dereferences and returns result wrapped in the RefMut.

The AtomicCell is already mutably borrowed, so this cannot fail.

This is an associated function that needs to be used as RefMut::map_split(...). A method would interfere with methods of the same name on the contents of a AtomicCell used through Deref.

§Examples
use atomicell::{AtomicCell, RefMut};

let c = AtomicCell::new(String::from("hello"));
let b1: RefMut<String> = c.borrow_mut();
let mut b2: RefMut<str> = RefMut::as_deref_mut(b1);
b2.make_ascii_uppercase();
assert_eq!(*b2, *"HELLO")
Source§

impl<'a, T> RefMut<'a, Option<T>>

Source

pub fn transpose(r: RefMut<'a, Option<T>>) -> Option<RefMut<'a, T>>

Transposes a RefMut of an Option into an Option of a RefMut. Releases shared lock of AtomicCell if the value is None.

The AtomicCell is already mutably borrowed, so this cannot fail.

This is an associated function that needs to be used as RefMut::map_split(...). A method would interfere with methods of the same name on the contents of a AtomicCell used through Deref.

§Examples
use atomicell::{AtomicCell, RefMut};

let c = AtomicCell::new(Some(5));
let b1: RefMut<Option<i32>> = c.borrow_mut();
let b2: Option<RefMut<i32>> = RefMut::transpose(b1);
assert!(b2.is_some());

let c = AtomicCell::new(None);
let b1: RefMut<Option<i32>> = c.borrow_mut();
let b2: Option<RefMut<i32>> = RefMut::transpose(b1);
assert!(b2.is_none());
assert!(c.try_borrow_mut().is_some());
Source§

impl<'a, T> RefMut<'a, [T]>

Source

pub fn slice<R>(r: RefMut<'a, [T]>, range: R) -> RefMut<'a, [T]>
where R: RangeBounds<usize>,

Makes a new RefMut for a sub-slice of the borrowed slice.

The AtomicCell is already mutably borrowed, so this cannot fail.

This is an associated function that needs to be used as RefMut::map(…). A method would interfere with methods of the same name on the contents of a AtomicCell used through Deref.

§Examples
use atomicell::{AtomicCell, RefMut};

let c: &AtomicCell<[i32]> = &AtomicCell::new([1, 2, 3, 4, 5]);
let b1: RefMut<[i32]> = RefMut::as_mut(c.borrow_mut());
let b2: RefMut<[i32]> = RefMut::slice(b1, 2..4);
assert_eq!(*b2, [3, 4])

Trait Implementations§

Source§

impl<'a, T, U> AsMut<U> for RefMut<'a, T>
where T: AsMut<U> + ?Sized,

Source§

fn as_mut(&mut self) -> &mut U

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

impl<'a, T, U> AsRef<U> for RefMut<'a, T>
where T: AsRef<U> + ?Sized,

Source§

fn as_ref(&self) -> &U

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

impl<'a, T> Borrow<T> for RefMut<'a, T>
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<'a, T> BorrowMut<T> for RefMut<'a, T>
where T: ?Sized,

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<'a, T> Debug for RefMut<'a, T>
where T: Debug,

Source§

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

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

impl<'a, T> Deref for RefMut<'a, T>
where T: ?Sized,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<'a, T> DerefMut for RefMut<'a, T>
where T: ?Sized,

Source§

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

Mutably dereferences the value.
Source§

impl<'a, T> Display for RefMut<'a, T>
where T: Display + ?Sized,

Source§

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

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

impl<'a, T> Hash for RefMut<'a, T>
where T: Hash + ?Sized,

Source§

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

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

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<'a, T, U> PartialEq<U> for RefMut<'a, T>
where T: PartialEq<U> + ?Sized,

Source§

fn eq(&self, other: &U) -> bool

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

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<'a, T, U> PartialOrd<U> for RefMut<'a, T>
where T: PartialOrd<U> + ?Sized,

Source§

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

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

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

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

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§

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

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

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<'b, T> Send for RefMut<'b, T>
where T: 'b + ?Sized, &'a mut T: for<'a> Send,

Source§

impl<'b, T> Sync for RefMut<'b, T>
where T: 'b + ?Sized, &'a mut T: for<'a> Sync,

Auto Trait Implementations§

§

impl<'a, T> Freeze for RefMut<'a, T>
where T: ?Sized,

§

impl<'a, T> RefUnwindSafe for RefMut<'a, T>
where T: RefUnwindSafe + ?Sized,

§

impl<'a, T> Unpin for RefMut<'a, T>
where T: ?Sized,

§

impl<'a, T> !UnwindSafe for RefMut<'a, T>

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

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> RawDrop for T

Source§

unsafe fn raw_drop(ptr: *mut c_void)

Write the default value of the type to the pointer. Read more
Source§

fn raw_drop_cb() -> Unsafe<&'static (dyn Fn(*mut c_void) + Send + Sync)>

Get a callback suitable for [SchemaData].
Source§

impl<T> RawHash for T
where T: Hash,

Source§

unsafe fn raw_hash(ptr: *const c_void) -> u64

Get the hash of the type. Read more
Source§

fn raw_hash_cb() -> Unsafe<&'static (dyn Fn(*const c_void) -> u64 + Send + Sync)>

Get a callback suitable for [SchemaData].
§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

§

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<R> Rng for R
where R: RngCore + ?Sized,

Source§

fn random<T>(&mut self) -> T

Return a random value via the StandardUniform distribution. Read more
Source§

fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>

Return an iterator over random variates Read more
Source§

fn random_range<T, R>(&mut self, range: R) -> T
where T: SampleUniform, R: SampleRange<T>,

Generate a random value in the given range. Read more
Source§

fn random_bool(&mut self, p: f64) -> bool

Return a bool with a probability p of being true. Read more
Source§

fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool

Return a bool with a probability of numerator/denominator of being true. Read more
Source§

fn sample<T, D>(&mut self, distr: D) -> T
where D: Distribution<T>,

Sample a new value, using the given distribution. Read more
Source§

fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>
where D: Distribution<T>, Self: Sized,

Create an iterator that generates values using the given distribution. Read more
Source§

fn fill<T>(&mut self, dest: &mut T)
where T: Fill + ?Sized,

Fill any type implementing Fill with random data Read more
Source§

fn gen<T>(&mut self) -> T

👎Deprecated since 0.9.0: Renamed to random to avoid conflict with the new gen keyword in Rust 2024.
Alias for Rng::random.
Source§

fn gen_range<T, R>(&mut self, range: R) -> T
where T: SampleUniform, R: SampleRange<T>,

👎Deprecated since 0.9.0: Renamed to random_range
Source§

fn gen_bool(&mut self, p: f64) -> bool

👎Deprecated since 0.9.0: Renamed to random_bool
Alias for Rng::random_bool.
Source§

fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool

👎Deprecated since 0.9.0: Renamed to random_ratio
Source§

impl<T> RngCore for T
where T: DerefMut, <T as Deref>::Target: RngCore,

Source§

fn next_u32(&mut self) -> u32

Return the next random u32. Read more
Source§

fn next_u64(&mut self) -> u64

Return the next random u64. Read more
Source§

fn fill_bytes(&mut self, dst: &mut [u8])

Fill dest with random data. Read more
§

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

§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.
Source§

impl<R> TryRngCore for R
where R: RngCore + ?Sized,

Source§

type Error = Infallible

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

fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>

Return the next random u32.
Source§

fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>

Return the next random u64.
Source§

fn try_fill_bytes( &mut self, dst: &mut [u8], ) -> Result<(), <R as TryRngCore>::Error>

Fill dest entirely with random data.
Source§

fn unwrap_err(self) -> UnwrapErr<Self>
where Self: Sized,

Wrap RNG with the UnwrapErr wrapper.
Source§

fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>

Wrap RNG with the UnwrapMut wrapper.
Source§

fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>
where Self: Sized,

Convert an RngCore to a RngReadAdapter.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> CryptoRng for T
where T: DerefMut, <T as Deref>::Target: CryptoRng,

Source§

impl<R> TryCryptoRng for R
where R: CryptoRng + ?Sized,