Enum Foster

Source
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§

§

Owned(OwnedT)

Owned.

§

Fostered(FosteredT)

Fostered.

Implementations§

Source§

impl<OwnedT, FosteredT> Foster<OwnedT, FosteredT>

Source

pub const fn new_owned(value: OwnedT) -> Self

Constructor.

Source

pub const fn new_fostered(value: FosteredT) -> Self

Constructor.

Source

pub const fn is_owned(&self) -> bool

True if owned.

Trait Implementations§

Source§

impl<OwnedT: Clone, FosteredT: Clone> Clone for Foster<OwnedT, FosteredT>

Source§

fn clone(&self) -> Foster<OwnedT, FosteredT>

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<OwnedT: Debug, FosteredT: Debug> Debug for Foster<OwnedT, FosteredT>

Source§

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

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

impl<OwnedT, FosteredT> From<OwnedT> for Foster<OwnedT, FosteredT>

Source§

fn from(value: OwnedT) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<OwnedT, FosteredT> Freeze for Foster<OwnedT, FosteredT>
where OwnedT: Freeze, FosteredT: Freeze,

§

impl<OwnedT, FosteredT> RefUnwindSafe for Foster<OwnedT, FosteredT>
where OwnedT: RefUnwindSafe, FosteredT: RefUnwindSafe,

§

impl<OwnedT, FosteredT> Send for Foster<OwnedT, FosteredT>
where OwnedT: Send, FosteredT: Send,

§

impl<OwnedT, FosteredT> Sync for Foster<OwnedT, FosteredT>
where OwnedT: Sync, FosteredT: Sync,

§

impl<OwnedT, FosteredT> Unpin for Foster<OwnedT, FosteredT>
where OwnedT: Unpin, FosteredT: Unpin,

§

impl<OwnedT, FosteredT> UnwindSafe for Foster<OwnedT, FosteredT>
where OwnedT: UnwindSafe, FosteredT: UnwindSafe,

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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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<I> IntoIterator for I
where I: Iterator,

Source§

type Item = <I as Iterator>::Item

The type of the elements being iterated over.
Source§

type IntoIter = I

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

fn into_iter(self) -> I

Creates an iterator from a value. Read more
Source§

impl<'own, ItemT, IterableT> JoinConjunction<'own> for IterableT
where ItemT: 'own + AsRef<str>, &'own IterableT: 'own + IntoIterator<Item = ItemT>,

Source§

fn join_conjunction(&'own self, conjunction: &str) -> String

Join iterated strings as human-readable in English with a conjunction and an Oxford comma. Read more
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.