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
§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>)
alloc only.An owned, heap-allocated value.
Only available with the alloc feature.
Implementations§
Source§impl<'a, T: ?Sized> Flex<'a, T>
impl<'a, T: ?Sized> Flex<'a, T>
Sourcepub fn into_box(self) -> Box<T>
Available on crate feature alloc only.
pub fn into_box(self) -> Box<T>
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>.
Sourcepub fn claim<'b>(self) -> Flex<'b, T>
Available on crate feature alloc only.
pub fn claim<'b>(self) -> Flex<'b, T>
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 + ToOwned> From<Cow<'a, T>> for Flex<'a, T>
Available on crate feature alloc only.
impl<'a, T: ?Sized + ToOwned> From<Cow<'a, T>> for Flex<'a, T>
alloc only.Source§impl<'a, T: ?Sized + ToOwned> From<Flex<'a, T>> for Cow<'a, T>
Available on crate feature alloc only.
impl<'a, T: ?Sized + ToOwned> From<Flex<'a, T>> for Cow<'a, T>
alloc only.Source§impl<'a, T: ?Sized> IntoIterator for &'a Flex<'a, T>where
&'a T: IntoIterator,
impl<'a, T: ?Sized> IntoIterator for &'a Flex<'a, T>where
&'a T: IntoIterator,
Source§impl<'a, T: ?Sized + Ord> Ord for Flex<'a, T>
impl<'a, T: ?Sized + Ord> Ord for Flex<'a, T>
Source§impl<'a, T: ?Sized + PartialEq> PartialEq<Box<T>> for Flex<'a, T>
Available on crate feature alloc only.
impl<'a, T: ?Sized + PartialEq> PartialEq<Box<T>> for Flex<'a, T>
alloc only.Source§impl<'a, T: ?Sized + PartialOrd> PartialOrd<&T> for Flex<'a, T>
impl<'a, T: ?Sized + PartialOrd> PartialOrd<&T> for Flex<'a, T>
Source§impl<'a, T: ?Sized + PartialOrd> PartialOrd<Box<T>> for Flex<'a, T>
Available on crate feature alloc only.
impl<'a, T: ?Sized + PartialOrd> PartialOrd<Box<T>> for Flex<'a, T>
alloc only.