Skip to main content

OwningRefMut

Struct OwningRefMut 

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

An mutable owning reference.

This wraps an owner O and a reference &mut T pointing at something reachable from O::Target while keeping the ability to move self around.

The owner is usually a pointer that points at some base type.

For more details and examples, see the module and method docs.

Implementations§

Source§

impl<'t, O, T> OwningRefMut<'t, O, T>
where T: ?Sized,

Source

pub fn new(o: O) -> OwningRefMut<'t, O, T>
where O: StableDeref<Target = T> + DerefMut,

Creates a new owning reference from a owner initialized to the direct dereference of it.

§Example
extern crate owning_ref;
use owning_ref::OwningRefMut;

fn main() {
    let owning_ref_mut = OwningRefMut::new(Box::new(42));
    assert_eq!(*owning_ref_mut, 42);
}
Source

pub unsafe fn new_assert_stable_address(o: O) -> OwningRefMut<'t, O, T>
where O: DerefMut<Target = T>,

Like new, but doesn’t require O to implement the StableAddress trait. Instead, the caller is responsible to make the same promises as implementing the trait.

This is useful for cases where coherence rules prevents implementing the trait without adding a dependency to this crate in a third-party library.

Source

pub unsafe fn map<F, U>(self, f: F) -> OwningRef<'t, O, U>
where O: StableDeref, F: FnOnce(&mut T) -> &U, U: ?Sized,

👎Deprecated since 0.5.0:

unsafe function. can create aliased references

Converts self into a new shared owning reference that points at something reachable from the previous one.

This can be a reference to a field of U, something reachable from a field of U, or even something unrelated with a 'static lifetime.

§Example
extern crate owning_ref;
use owning_ref::OwningRefMut;

fn main() {
    let owning_ref_mut = OwningRefMut::new(Box::new([1, 2, 3, 4]));

    // create a owning reference that points at the
    // third element of the array.
    let owning_ref = unsafe { owning_ref_mut.map(|array| &array[2]) };
    assert_eq!(*owning_ref, 3);
}
Source

pub fn map_mut<F, U>(self, f: F) -> OwningRefMut<'t, O, U>
where O: StableDeref, F: FnOnce(&mut T) -> &mut U, U: ?Sized,

Converts self into a new mutable owning reference that points at something reachable from the previous one.

This can be a reference to a field of U, something reachable from a field of U, or even something unrelated with a 'static lifetime.

§Example
extern crate owning_ref;
use owning_ref::OwningRefMut;

fn main() {
    let owning_ref_mut = OwningRefMut::new(Box::new([1, 2, 3, 4]));

    // create a owning reference that points at the
    // third element of the array.
    let owning_ref_mut = owning_ref_mut.map_mut(|array| &mut array[2]);
    assert_eq!(*owning_ref_mut, 3);
}
Source

pub unsafe fn try_map<F, U, E>(self, f: F) -> Result<OwningRef<'t, O, U>, E>
where O: StableDeref, F: FnOnce(&mut T) -> Result<&U, E>, U: ?Sized,

👎Deprecated since 0.5.0:

unsafe function. can create aliased references

Tries to convert self into a new shared owning reference that points at something reachable from the previous one.

This can be a reference to a field of U, something reachable from a field of U, or even something unrelated with a 'static lifetime.

§Example
extern crate owning_ref;
use owning_ref::OwningRefMut;

fn main() {
    let owning_ref_mut = OwningRefMut::new(Box::new([1, 2, 3, 4]));

    // create a owning reference that points at the
    // third element of the array.
    let owning_ref = unsafe {
        owning_ref_mut.try_map(|array| {
            if array[2] == 3 { Ok(&array[2]) } else { Err(()) }
        })
    };
    assert_eq!(*owning_ref.unwrap(), 3);
}
Source

pub fn try_map_mut<F, U, E>(self, f: F) -> Result<OwningRefMut<'t, O, U>, E>
where O: StableDeref, F: FnOnce(&mut T) -> Result<&mut U, E>, U: ?Sized,

Tries to convert self into a new mutable owning reference that points at something reachable from the previous one.

This can be a reference to a field of U, something reachable from a field of U, or even something unrelated with a 'static lifetime.

§Example
extern crate owning_ref;
use owning_ref::OwningRefMut;

fn main() {
    let owning_ref_mut = OwningRefMut::new(Box::new([1, 2, 3, 4]));

    // create a owning reference that points at the
    // third element of the array.
    let owning_ref_mut = owning_ref_mut.try_map_mut(|array| {
        if array[2] == 3 { Ok(&mut array[2]) } else { Err(()) }
    });
    assert_eq!(*owning_ref_mut.unwrap(), 3);
}
Source

pub unsafe fn map_owner<F, P>(self, f: F) -> OwningRefMut<'t, P, T>
where O: StableDeref, P: StableDeref, F: FnOnce(O) -> P,

Converts self into a new owning reference with a different owner type.

The new owner type needs to still contain the original owner in some way so that the reference into it remains valid. This function is marked unsafe because the user needs to manually uphold this guarantee.

Source

pub fn map_owner_box(self) -> OwningRefMut<'t, Box<O>, T>

Converts self into a new owning reference where the owner is wrapped in an additional Box<O>.

This can be used to safely erase the owner of any OwningRefMut<'_, O, T> to a OwningRefMut<'_, Box<dyn Erased>, T>.

Source

pub fn erase_owner<'a>( self, ) -> OwningRefMut<'t, <O as IntoErased<'a>>::Erased, T>
where O: IntoErased<'a>,

Erases the concrete base type of the owner with a trait object.

This allows mixing of owned references with different owner base types.

§Example
extern crate owning_ref;
use owning_ref::{OwningRefMut, Erased};

fn main() {
    // NB: Using the concrete types here for explicitnes.
    // For less verbose code type aliases like `BoxRef` are provided.

    let owning_ref_mut_a: OwningRefMut<'_, Box<[i32; 4]>, [i32; 4]>
        = OwningRefMut::new(Box::new([1, 2, 3, 4]));

    let owning_ref_mut_b: OwningRefMut<'_, Box<Vec<(i32, bool)>>, Vec<(i32, bool)>>
        = OwningRefMut::new(Box::new(vec![(0, false), (1, true)]));

    let owning_ref_mut_a: OwningRefMut<'_, Box<[i32; 4]>, i32>
        = owning_ref_mut_a.map_mut(|a| &mut a[0]);

    let owning_ref_mut_b: OwningRefMut<'_, Box<Vec<(i32, bool)>>, i32>
        = owning_ref_mut_b.map_mut(|a| &mut a[1].0);

    let owning_refs_mut: [OwningRefMut<'_, Box<dyn Erased>, i32>; 2]
        = [owning_ref_mut_a.erase_owner(), owning_ref_mut_b.erase_owner()];

    assert_eq!(*owning_refs_mut[0], 1);
    assert_eq!(*owning_refs_mut[1], 1);
}
Source

pub unsafe fn as_owner(&self) -> &O

👎Deprecated since 0.5.0:

unsafe function. can create aliased references

A reference to the underlying owner.

Source

pub unsafe fn as_owner_mut(&mut self) -> &mut O

👎Deprecated since 0.5.0:

unsafe function. can create aliased references

A mutable reference to the underlying owner.

Source

pub fn into_owner(self) -> O

Discards the reference and retrieves the owner.

Trait Implementations§

Source§

impl<'t, O, T> AsMut<T> for OwningRefMut<'t, O, T>
where T: ?Sized,

Source§

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

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

impl<'t, O, T> AsRef<T> for OwningRefMut<'t, O, T>
where T: ?Sized,

Source§

fn as_ref(&self) -> &T

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

impl<'t, O, T> Borrow<T> for OwningRefMut<'t, O, T>
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<'t, O, T> BorrowMut<T> for OwningRefMut<'t, O, T>
where T: ?Sized,

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<'t, O, T> Debug for OwningRefMut<'t, O, T>
where O: Debug, T: Debug + ?Sized,

Source§

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

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

impl<'t, O, T> Deref for OwningRefMut<'t, O, T>
where T: ?Sized,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<'t, O, T> DerefMut for OwningRefMut<'t, O, T>
where T: ?Sized,

Source§

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

Mutably dereferences the value.
Source§

impl<'t, O, T> Eq for OwningRefMut<'t, O, T>
where T: Eq + ?Sized,

Source§

impl<'t, O, T> From<O> for OwningRefMut<'t, O, T>
where O: StableDeref<Target = T> + DerefMut, T: ?Sized,

Source§

fn from(owner: O) -> OwningRefMut<'t, O, T>

Converts to this type from the input type.
Source§

impl<'t, O, T> Hash for OwningRefMut<'t, O, 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 · 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, O, T> Ord for OwningRefMut<'t, O, T>
where T: Ord + ?Sized,

Source§

fn cmp(&self, other: &OwningRefMut<'t, O, T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

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

impl<'t, O, T> PartialEq for OwningRefMut<'t, O, T>
where T: PartialEq + ?Sized,

Source§

fn eq(&self, other: &OwningRefMut<'t, O, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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, O, T> PartialOrd for OwningRefMut<'t, O, T>
where T: PartialOrd + ?Sized,

Source§

fn partial_cmp(&self, other: &OwningRefMut<'t, O, T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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, O, T> Send for OwningRefMut<'t, O, T>
where O: Send, &'a mut T: for<'a> Send, T: ?Sized,

Source§

impl<'t, O, T> StableDeref for OwningRefMut<'t, O, T>
where T: ?Sized,

Source§

impl<'t, O, T> Sync for OwningRefMut<'t, O, T>
where O: Sync, &'a mut T: for<'a> Sync, T: ?Sized,

Auto Trait Implementations§

§

impl<'t, O, T> Freeze for OwningRefMut<'t, O, T>
where O: Freeze, T: ?Sized,

§

impl<'t, O, T> RefUnwindSafe for OwningRefMut<'t, O, T>

§

impl<'t, O, T> Unpin for OwningRefMut<'t, O, T>
where O: Unpin, T: ?Sized,

§

impl<'t, O, T> UnsafeUnpin for OwningRefMut<'t, O, T>
where O: UnsafeUnpin, T: ?Sized,

§

impl<'t, O, T> UnwindSafe for OwningRefMut<'t, O, T>
where T: RefUnwindSafe + ?Sized, O: 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> Any for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Source§

fn type_name(&self) -> &'static str

Source§

impl<T> AnySync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

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

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> Erased for T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> NumBytes for T
where T: Debug + AsRef<[u8]> + AsMut<[u8]> + PartialEq + Eq + PartialOrd + Ord + Hash + Borrow<[u8]> + BorrowMut<[u8]> + ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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<R> Rng for R
where R: TryRng<Error = Infallible> + ?Sized,

Source§

fn next_u32(&mut self) -> u32

Return the next random u32.
Source§

fn next_u64(&mut self) -> u64

Return the next random u64.
Source§

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

Fill dest with random data. Read more
Source§

impl<R> RngCore for R
where R: Rng,

Source§

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

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

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(value: T, _simd: S) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<R> TryCryptoRng for R
where R: DerefMut, <R as Deref>::Target: TryCryptoRng,

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.
Source§

impl<R> TryRng for R
where R: DerefMut, <R as Deref>::Target: TryRng,

Source§

type Error = <<R as Deref>::Target as TryRng>::Error

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

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

Return the next random u32.
Source§

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

Return the next random u64.
Source§

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

Fill dst entirely with random data.
Source§

impl<R> TryRngCore for R
where R: TryRng,

Source§

type Error = <R as TryRng>::Error

👎Deprecated since 0.10.0:

use TryRng instead

Error type.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more