maybe_owned

Enum MaybeOwnedMut

source
pub enum MaybeOwnedMut<'a, T: 'a> {
    Owned(T),
    Borrowed(&'a mut T),
}
Expand description

This type is basically the same as MaybeOwned, but works with mutable references.

Note that while you can se MaybeOwned as a alternative implementation for a Cow (Copy-On-Write) type this isn’t really the case for MaybeOwnedMut as changing it will potentially change the source through the given &mut reference. For example the transitive add assign (+=) implementation for MaybeOwned does (need to) convert the given instance into a owned variant before using += on the contained type. But for MaybeOwnedMut it can directly use += on the &mut contained in the Borrowed variant!

Variants§

§

Owned(T)

owns T

§

Borrowed(&'a mut T)

has a reference to T

Implementations§

source§

impl<T> MaybeOwnedMut<'_, T>

source

pub fn is_owned(&self) -> bool

Returns true if the data is owned else false.

source§

impl<T: Clone> MaybeOwnedMut<'_, T>

source

pub fn into_owned(self) -> T

Return the contained data in it’s owned form.

If it’s borrowed this will clone it.

source

pub fn make_owned(&mut self) -> &mut T

Internally converts the type into it’s owned variant.

Conversion from a reference to the owned variant is done by cloning.

This returns a &mut T and as such can be used to “unconditionally” get an &mut T. Be aware that while this works with both MaybeOwned and MaybeOwnedMut it also converts it to an owned variant in both cases. So while it’s the best way to get a &mut T for MaybeOwned for MaybeOwnedMut it’s preferable to use as_mut from AsMut.

§Example
use maybe_owned::MaybeOwned;

#[derive(Clone, Debug, PartialEq, Eq)]
struct PseudoBigData(u8);

let data = PseudoBigData(12);

let mut maybe: MaybeOwned<PseudoBigData> = (&data).into();
assert_eq!(false, maybe.is_owned());

{
    let reference = maybe.make_owned();
    assert_eq!(&mut PseudoBigData(12), reference);
}
assert!(maybe.is_owned());

Trait Implementations§

source§

impl<'min, L, R, OUT: 'min> Add<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: Add<R, Output = OUT> + Add<&'min R, Output = OUT>, &'min L: Add<R, Output = OUT> + Add<&'min R, Output = OUT>,

source§

type Output = OUT

The resulting type after applying the + operator.
source§

fn add(self, rhs: MaybeOwnedMut<'min, R>) -> Self::Output

Performs the + operation. Read more
source§

impl<'min, L, R> AddAssign<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: AddAssign<R> + AddAssign<&'min R>,

source§

fn add_assign(&mut self, rhs: MaybeOwnedMut<'min, R>)

Performs the += operation. Read more
source§

impl<T> AsMut<T> for MaybeOwnedMut<'_, T>

source§

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

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

impl<T> AsRef<T> for MaybeOwnedMut<'_, T>

source§

fn as_ref(&self) -> &T

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

impl<'min, L, R, OUT: 'min> BitAnd<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: BitAnd<R, Output = OUT> + BitAnd<&'min R, Output = OUT>, &'min L: BitAnd<R, Output = OUT> + BitAnd<&'min R, Output = OUT>,

source§

type Output = OUT

The resulting type after applying the & operator.
source§

fn bitand(self, rhs: MaybeOwnedMut<'min, R>) -> Self::Output

Performs the & operation. Read more
source§

impl<'min, L, R> BitAndAssign<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>

source§

fn bitand_assign(&mut self, rhs: MaybeOwnedMut<'min, R>)

Performs the &= operation. Read more
source§

impl<'min, L, R, OUT: 'min> BitOr<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: BitOr<R, Output = OUT> + BitOr<&'min R, Output = OUT>, &'min L: BitOr<R, Output = OUT> + BitOr<&'min R, Output = OUT>,

source§

type Output = OUT

The resulting type after applying the | operator.
source§

fn bitor(self, rhs: MaybeOwnedMut<'min, R>) -> Self::Output

Performs the | operation. Read more
source§

impl<'min, L, R> BitOrAssign<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>

source§

fn bitor_assign(&mut self, rhs: MaybeOwnedMut<'min, R>)

Performs the |= operation. Read more
source§

impl<'min, L, R, OUT: 'min> BitXor<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: BitXor<R, Output = OUT> + BitXor<&'min R, Output = OUT>, &'min L: BitXor<R, Output = OUT> + BitXor<&'min R, Output = OUT>,

source§

type Output = OUT

The resulting type after applying the ^ operator.
source§

fn bitxor(self, rhs: MaybeOwnedMut<'min, R>) -> Self::Output

Performs the ^ operation. Read more
source§

impl<'min, L, R> BitXorAssign<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>

source§

fn bitxor_assign(&mut self, rhs: MaybeOwnedMut<'min, R>)

Performs the ^= operation. Read more
source§

impl<T> Borrow<T> for MaybeOwnedMut<'_, T>

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for MaybeOwnedMut<'_, T>

source§

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

Mutably borrows from an owned value. Read more
source§

impl<'a, T: Debug + 'a> Debug for MaybeOwnedMut<'a, T>

source§

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

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

impl<T: Default> Default for MaybeOwnedMut<'_, T>

source§

fn default() -> Self

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

impl<T> Deref for MaybeOwnedMut<'_, T>

source§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &T

Dereferences the value.
source§

impl<T> DerefMut for MaybeOwnedMut<'_, T>

source§

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

Mutably dereferences the value.
source§

impl<'a, T: Display> Display for MaybeOwnedMut<'a, T>

source§

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

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

impl<'min, L, R, OUT: 'min> Div<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: Div<R, Output = OUT> + Div<&'min R, Output = OUT>, &'min L: Div<R, Output = OUT> + Div<&'min R, Output = OUT>,

source§

type Output = OUT

The resulting type after applying the / operator.
source§

fn div(self, rhs: MaybeOwnedMut<'min, R>) -> Self::Output

Performs the / operation. Read more
source§

impl<'min, L, R> DivAssign<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: DivAssign<R> + DivAssign<&'min R>,

source§

fn div_assign(&mut self, rhs: MaybeOwnedMut<'min, R>)

Performs the /= operation. Read more
source§

impl<'a, T> From<&'a mut T> for MaybeOwnedMut<'a, T>

source§

fn from(v: &'a mut T) -> Self

Converts to this type from the input type.
source§

impl<T> From<T> for MaybeOwnedMut<'_, T>

source§

fn from(v: T) -> Self

Converts to this type from the input type.
source§

impl<T: FromStr> FromStr for MaybeOwnedMut<'_, T>

source§

type Err = <T as FromStr>::Err

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl<T: Hash> Hash for MaybeOwnedMut<'_, 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<'min, L, R, OUT: 'min> Mul<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: Mul<R, Output = OUT> + Mul<&'min R, Output = OUT>, &'min L: Mul<R, Output = OUT> + Mul<&'min R, Output = OUT>,

source§

type Output = OUT

The resulting type after applying the * operator.
source§

fn mul(self, rhs: MaybeOwnedMut<'min, R>) -> Self::Output

Performs the * operation. Read more
source§

impl<'min, L, R> MulAssign<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: MulAssign<R> + MulAssign<&'min R>,

source§

fn mul_assign(&mut self, rhs: MaybeOwnedMut<'min, R>)

Performs the *= operation. Read more
source§

impl<'l, V, OUT> Neg for MaybeOwnedMut<'l, V>
where V: Neg<Output = OUT>, &'l V: Neg<Output = OUT>,

source§

type Output = OUT

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<'l, V, OUT> Not for MaybeOwnedMut<'l, V>
where V: Not<Output = OUT>, &'l V: Not<Output = OUT>,

source§

type Output = <V as Not>::Output

The resulting type after applying the ! operator.
source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
source§

impl<T: Ord> Ord for MaybeOwnedMut<'_, T>

source§

fn cmp(&self, other: &Self) -> 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<'b, A: PartialEq<B>, B> PartialEq<MaybeOwnedMut<'b, B>> for MaybeOwnedMut<'_, A>

source§

fn eq(&self, other: &MaybeOwnedMut<'b, B>) -> 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 MaybeOwnedMut<'_, T>

source§

fn partial_cmp(&self, other: &Self) -> 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<'min, L, R, OUT: 'min> Shl<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: Shl<R, Output = OUT> + Shl<&'min R, Output = OUT>, &'min L: Shl<R, Output = OUT> + Shl<&'min R, Output = OUT>,

source§

type Output = OUT

The resulting type after applying the << operator.
source§

fn shl(self, rhs: MaybeOwnedMut<'min, R>) -> Self::Output

Performs the << operation. Read more
source§

impl<'min, L, R> ShlAssign<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: ShlAssign<R> + ShlAssign<&'min R>,

source§

fn shl_assign(&mut self, rhs: MaybeOwnedMut<'min, R>)

Performs the <<= operation. Read more
source§

impl<'min, L, R, OUT: 'min> Shr<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: Shr<R, Output = OUT> + Shr<&'min R, Output = OUT>, &'min L: Shr<R, Output = OUT> + Shr<&'min R, Output = OUT>,

source§

type Output = OUT

The resulting type after applying the >> operator.
source§

fn shr(self, rhs: MaybeOwnedMut<'min, R>) -> Self::Output

Performs the >> operation. Read more
source§

impl<'min, L, R> ShrAssign<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: ShrAssign<R> + ShrAssign<&'min R>,

source§

fn shr_assign(&mut self, rhs: MaybeOwnedMut<'min, R>)

Performs the >>= operation. Read more
source§

impl<'min, L, R, OUT: 'min> Sub<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: Sub<R, Output = OUT> + Sub<&'min R, Output = OUT>, &'min L: Sub<R, Output = OUT> + Sub<&'min R, Output = OUT>,

source§

type Output = OUT

The resulting type after applying the - operator.
source§

fn sub(self, rhs: MaybeOwnedMut<'min, R>) -> Self::Output

Performs the - operation. Read more
source§

impl<'min, L, R> SubAssign<MaybeOwnedMut<'min, R>> for MaybeOwnedMut<'min, L>
where L: SubAssign<R> + SubAssign<&'min R>,

source§

fn sub_assign(&mut self, rhs: MaybeOwnedMut<'min, R>)

Performs the -= operation. Read more
source§

impl<'a, T: Eq> Eq for MaybeOwnedMut<'a, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for MaybeOwnedMut<'a, T>
where T: Freeze,

§

impl<'a, T> RefUnwindSafe for MaybeOwnedMut<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for MaybeOwnedMut<'a, T>
where T: Send,

§

impl<'a, T> Sync for MaybeOwnedMut<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for MaybeOwnedMut<'a, T>
where T: Unpin,

§

impl<'a, T> !UnwindSafe for MaybeOwnedMut<'a, 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> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
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<T> ToString for T
where T: Display + ?Sized,

source§

default 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.