Flex

Enum Flex 

Source
pub enum Flex<'a, T: ?Sized> {
    Lend(&'a T),
    Give(Box<T>),
}
Expand description

A flexible container that can hold either a borrowed reference or an owned boxed value.

Flex provides a unified interface for working with both borrowed (&'a T) and owned (Box<T>) data of the same type. This is particularly useful for unsized types where the owned representation is naturally Box<T> rather than a separate container type.

§Variants

  • Lend: Holds a borrowed reference &'a T
  • Give: Holds an owned Box<T> (requires alloc feature)

§Comparison with Other Types

Unlike Cow, which works with type pairs like str/String or [T]/Vec<T>, Flex works with a single type in two ownership models. This makes it ideal for trait objects and other unsized types where there isn’t a natural “owned” container type.

§Examples

use flex::Flex;
use std::fmt::Debug;

// Works with trait objects
fn print_debug(value: Flex<dyn Debug>) {
    println!("{:?}", value);
}

let borrowed: Flex<dyn Debug> = Flex::from(&42 as &dyn Debug);
print_debug(borrowed);

let owned: Flex<dyn Debug> = Flex::from(Box::new("hello") as Box<dyn Debug>);
print_debug(owned);
use flex::Flex;

// Works with string slices
let s = String::from("hello");
let borrowed = Flex::from(s.as_str());
assert_eq!(&*borrowed, "hello");

Variants§

§

Lend(&'a T)

A borrowed reference to data with lifetime 'a.

§

Give(Box<T>)

Available on crate feature alloc only.

An owned, heap-allocated value.

Only available with the alloc feature.

Implementations§

Source§

impl<'a, T: ?Sized> Flex<'a, T>
where Box<T>: From<&'a T>,

Source

pub fn into_box(self) -> Box<T>

Available on crate feature alloc only.

Converts the Flex into a Box<T>, consuming the Flex.

For Lend variants, this allocates a new Box<T> from the borrowed reference. For Give variants, this simply returns the owned Box<T>.

Source

pub fn claim<'b>(self) -> Flex<'b, T>

Available on crate feature alloc only.

Claims ownership of the data, converting borrowed data to owned.

For Lend variants, this converts the borrowed data into an owned Box<T> by using Box<T>::from(&T). This typically involves cloning or allocating the data.

For Give variants, this is a no-op that simply changes the lifetime bound, as the data is already owned.

§Examples
use flex::Flex;

let borrowed = Flex::from(&[1, 2, 3][..]);
let owned: Flex<'static, [i32]> = borrowed.claim();
assert_eq!(&*owned, &[1, 2, 3]);
use flex::Flex;

let s = "hello";
let borrowed = Flex::from(s);
let owned: Flex<'static, str> = borrowed.claim();
assert_eq!(&*owned, "hello");

Trait Implementations§

Source§

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

Source§

fn as_ref(&self) -> &U

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

impl<'a, T: ?Sized + Binary> Binary for Flex<'a, T>

Source§

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

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

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<'a, T: ?Sized> Clone for Flex<'a, T>
where Box<T>: Clone,

Available on crate feature alloc only.
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<'a, T: Debug + ?Sized> Debug for Flex<'a, T>

Source§

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

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

impl<'a, T: ?Sized> Default for Flex<'a, T>
where &'a T: Default,

Source§

fn default() -> Self

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

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

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

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

Source§

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

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

impl<'a, T: ?Sized> From<&'a T> for Flex<'a, T>

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

impl<T: ?Sized> From<Box<T>> for Flex<'_, T>

Available on crate feature alloc only.
Source§

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

Converts to this type from the input type.
Source§

impl<'a, T: ?Sized + ToOwned> From<Cow<'a, T>> for Flex<'a, T>
where T::Owned: Into<Box<T>>,

Available on crate feature alloc only.
Source§

fn from(c: Cow<'a, T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T: ?Sized + ToOwned> From<Flex<'a, T>> for Cow<'a, T>
where T::Owned: From<Box<T>>,

Available on crate feature alloc only.
Source§

fn from(c: Flex<'a, T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T: ?Sized + Hash> Hash for Flex<'a, 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<'a, T: ?Sized + Index<I>, I> Index<I> for Flex<'a, T>

Source§

type Output = <T as Index<I>>::Output

The returned type after indexing.
Source§

fn index(&self, index: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, T: ?Sized> IntoIterator for &'a Flex<'a, T>

Source§

type Item = <&'a T as IntoIterator>::Item

The type of the elements being iterated over.
Source§

type IntoIter = <&'a T as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T: ?Sized + LowerExp> LowerExp for Flex<'a, T>

Source§

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

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

impl<'a, T: ?Sized + LowerHex> LowerHex for Flex<'a, T>

Source§

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

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

impl<'a, T: ?Sized + Octal> Octal for Flex<'a, T>

Source§

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

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

impl<'a, T: ?Sized + Ord> Ord for Flex<'a, 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<'a, T: ?Sized + PartialEq> PartialEq<&T> for Flex<'a, T>

Source§

fn eq(&self, other: &&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<'a, T: ?Sized + PartialEq> PartialEq<Box<T>> for Flex<'a, T>

Available on crate feature alloc only.
Source§

fn eq(&self, other: &Box<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<'a, T: ?Sized + PartialEq> PartialEq<T> for Flex<'a, T>

Source§

fn eq(&self, other: &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<'a, T: ?Sized + PartialEq> PartialEq for Flex<'a, T>

Source§

fn eq(&self, other: &Self) -> 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<'a, T: ?Sized + PartialOrd> PartialOrd<&T> for Flex<'a, T>

Source§

fn partial_cmp(&self, other: &&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<'a, T: ?Sized + PartialOrd> PartialOrd<Box<T>> for Flex<'a, T>

Available on crate feature alloc only.
Source§

fn partial_cmp(&self, other: &Box<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<'a, T: ?Sized + PartialOrd> PartialOrd<T> for Flex<'a, T>

Source§

fn partial_cmp(&self, other: &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<'a, T: ?Sized + PartialOrd> PartialOrd for Flex<'a, 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<'a, T: ?Sized> Pointer for Flex<'a, T>

Source§

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

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

impl<'a, T: ?Sized + UpperExp> UpperExp for Flex<'a, T>

Source§

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

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

impl<'a, T: ?Sized + UpperHex> UpperHex for Flex<'a, T>

Source§

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

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

impl<'a, T: ?Sized + Eq> Eq for Flex<'a, T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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