managed

Enum Managed

Source
pub enum Managed<'a, T: 'a + ?Sized> {
    Borrowed(&'a mut T),
    Owned(Box<T>),
}
Expand description

A managed object.

This enum can be used to represent exclusive access to objects. In Rust, exclusive access to an object is obtained by either owning the object, or owning a mutable pointer to the object; hence, “managed”.

The purpose of this enum is providing good ergonomics with std present while making it possible to avoid having a heap at all (which of course means that std is not present). To achieve this, the variants other than Borrow are only available when the corresponding feature is opted in.

A function that requires a managed object should be generic over an Into<Managed<'a, T>> argument; then, it will be possible to pass either a Box<T>, Vec<T>, or a &'a mut T without any conversion at the call site.

Note that a Vec<T> converted into an Into<Managed<'a, [T]>> gets transformed into a boxed slice, and can no longer be resized. See also ManagedSlice, which does not have this drawback.

Variants§

§

Borrowed(&'a mut T)

Borrowed variant.

§

Owned(Box<T>)

Owned variant, only available with the std or alloc feature enabled.

Trait Implementations§

Source§

impl<'a, T> Debug for Managed<'a, T>
where T: Debug + 'a + ?Sized,

Source§

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

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

impl<'a, T: 'a + ?Sized> Deref for Managed<'a, T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a, T: 'a + ?Sized> DerefMut for Managed<'a, T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'a, T: 'a + ?Sized> From<&'a mut T> for Managed<'a, T>

Source§

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

Converts to this type from the input type.
Source§

impl<'a, T: ?Sized + 'a> From<Box<T>> for Managed<'a, T>

Source§

fn from(value: Box<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T: 'a> From<Vec<T>> for Managed<'a, [T]>

Source§

fn from(value: Vec<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

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

§

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

§

impl<'a, T> Send for Managed<'a, T>
where T: Send + ?Sized,

§

impl<'a, T> Sync for Managed<'a, T>
where T: Sync + ?Sized,

§

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

§

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

§

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.