Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Morph
  • Loading branch information
gavofyork committed May 31, 2022
commit 1b0e63a6f1e45e4892a4cb1ba7982326badc1f97
36 changes: 36 additions & 0 deletions primitives/runtime/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,42 @@ where
}
}

/// Extensible conversion trait. Generic over only source type, with destination type being
/// associated.
pub trait Morph<A> {
/// The type into which `A` is mutated.
type Outcome;

/// Make conversion.
fn morph(a: A) -> Self::Outcome;
}

/// A structure that performs identity conversion.
impl<T> Morph<T> for Identity {
type Outcome = T;
fn morph(a: T) -> T {
a
}
}

/// Extensible conversion trait. Generic over only source type, with destination type being
/// associated.
pub trait TryMorph<A> {
/// The type into which `A` is mutated.
type Outcome;

/// Make conversion.
fn try_morph(a: A) -> Result<Self::Outcome, ()>;
}

/// A structure that performs identity conversion.
impl<T> TryMorph<T> for Identity {
type Outcome = T;
fn try_morph(a: T) -> Result<T, ()> {
Ok(a)
}
}

/// Extensible conversion trait. Generic over both source and destination types.
pub trait Convert<A, B> {
/// Make conversion.
Expand Down