pub enum Foster<OwnedT, FosteredT> {
Owned(OwnedT),
Fostered(FosteredT),
}
Expand description
Fostering allows for subjective equivalence between owned values and differently-typed fostered values. Equivalence can mean comparison, iteration, hashing, representation, etc.
An example use case:
You create Vec<String> values dynamically in your program, but you also want to allow for
constant values, which cannot be a Vec. A reasonable constant equivalent would be
&'static [&'static str]
, which is a very different type.
Although you could convert &'static [&'static str]
to Vec<String>, it would require
allocation, which is unnecessary and wasteful in situations in which you don’t actually need or
care about ownership, e.g. comparisons. (Although we can allow for efficient conversion via
the IntoOwned trait.)
“Fostering” means creating a single type on top of both types, and then implementing necessary traits for that type, e.g. PartialEq.
This type simply provides a unified mechanism for fostering. Furthermore, this module contains commonly useful implementations, such as FosterString and FosterStringVector, as well as macros for delegating the implemented traits to newtypes.
Note that Cow also counts as fostering, but it’s more specific in that the fostered type is always a reference of the owned type, which allows for generalized conversion to ownership when necessary via ToOwned.
Variants§
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<OwnedT, FosteredT> Freeze for Foster<OwnedT, FosteredT>
impl<OwnedT, FosteredT> RefUnwindSafe for Foster<OwnedT, FosteredT>where
OwnedT: RefUnwindSafe,
FosteredT: RefUnwindSafe,
impl<OwnedT, FosteredT> Send for Foster<OwnedT, FosteredT>
impl<OwnedT, FosteredT> Sync for Foster<OwnedT, FosteredT>
impl<OwnedT, FosteredT> Unpin for Foster<OwnedT, FosteredT>
impl<OwnedT, FosteredT> UnwindSafe for Foster<OwnedT, FosteredT>where
OwnedT: UnwindSafe,
FosteredT: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.