Struct Specializer

Source
pub struct Specializer<T, U, F>(/* private fields */);
Expand description

Specialized behavior runner (Owned -> Owned)

Implementations§

Source§

impl<T, U, F> Specializer<T, U, F>
where F: FnOnce(T) -> U, T: 'static, U: 'static,

Source

pub const fn new(params: T, f: F) -> Self

Create a new specializer with a fallback function.

Source

pub fn specialize<P, R>( self, f: impl FnOnce(P) -> R, ) -> Specializer<T, U, impl FnOnce(T) -> U>
where P: 'static, R: 'static,

Specialize on the parameter and the return type of the closure.

use specializer::Specializer;

fn specialized<T, U>(ty: T) -> U
where
    T: 'static,
    U: 'static + From<T> + From<u8>,
{
    Specializer::new(ty, From::from)
        .specialize(|int: i32| -> i32 { int * 2 })
        .specialize_param(|int: u8| { U::from(int * 3) })
        .run()
}

assert_eq!(specialized::<i16, i32>(3), 3);
assert_eq!(specialized::<i32, i32>(3), 6);
assert_eq!(specialized::<u8, i32>(3), 9);
Source

pub fn specialize_map<P, R>( self, p: impl FnOnce(P) -> P, f: impl FnOnce(T) -> U, r: impl FnOnce(R) -> R, ) -> Specializer<T, U, impl FnOnce(T) -> U>
where P: 'static, R: 'static,

Specialize on the parameter and the return type of the closure, mapping both.

use std::convert;

use specializer::Specializer;

fn specialized<T, U>(ty: T) -> U
where
    T: 'static,
    U: 'static + From<T>,
{
    Specializer::new(ty, From::from)
        .specialize(|int: i32| -> i32 { int * 2 })
        .specialize_map(
            |int: u8| int * 3,
            From::from,
            convert::identity::<U>,
        )
        .run()
}

assert_eq!(specialized::<i16, i32>(3), 3);
assert_eq!(specialized::<i32, i32>(3), 6);
assert_eq!(specialized::<u8, i32>(3), 9);
Source

pub fn specialize_param<P>( self, f: impl FnOnce(P) -> U, ) -> Specializer<T, U, impl FnOnce(T) -> U>
where P: 'static,

Specialize on the parameter of the closure.

use specializer::Specializer;

fn specialized<T>(ty: T) -> String
where
    T: 'static
{
    let fallback = |_| "unknown".to_owned();

    Specializer::new(ty, fallback)
        .specialize_param(|int: i32| (int * 2).to_string())
        .specialize_param(|string: String| string)
        .run()
}

assert_eq!(specialized(3), "6");
assert_eq!(specialized("Hello world".to_string()), "Hello world");
assert_eq!(specialized(()), "unknown");
Source

pub fn specialize_return<R>( self, f: impl FnOnce(T) -> R, ) -> Specializer<T, U, impl FnOnce(T) -> U>
where R: 'static,

Specialize on the return type of the closure.

use specializer::Specializer;

fn specialized<T>(int: i32) -> T
where
    T: 'static + Default
{
    let fallback = |_| -> T { Default::default() };

    Specializer::new(int, fallback)
        .specialize_return(|int| -> i32 { int * 2 })
        .specialize_return(|int| -> String { int.to_string() })
        .run()
}

assert_eq!(specialized::<i32>(3), 6);
assert_eq!(specialized::<String>(3), "3");
assert_eq!(specialized::<u8>(3), 0);
Source

pub fn specialize_map_param<P>( self, p: impl FnOnce(P) -> P, f: impl FnOnce(T) -> U, ) -> Specializer<T, U, impl FnOnce(T) -> U>
where P: 'static,

Specialize on the parameter and the return type of the closure, mapping the parameter.

use std::convert;

use specializer::Specializer;

fn specialized<T, U>(ty: T) -> U
where
    T: 'static,
    U: 'static + From<T>,
{
    Specializer::new(ty, From::from)
        .specialize(|int: i32| -> i32 { int * 2 })
        .specialize_map_param(|int: u8| int * 3, From::from)
        .run()
}

assert_eq!(specialized::<i16, i32>(3), 3);
assert_eq!(specialized::<i32, i32>(3), 6);
assert_eq!(specialized::<u8, i32>(3), 9);
Source

pub fn specialize_map_return<R>( self, f: impl FnOnce(T) -> U, r: impl FnOnce(R) -> R, ) -> Specializer<T, U, impl FnOnce(T) -> U>
where R: 'static,

Specialize on the parameter and the return type of the closure, mapping the parameter.

use std::convert;

use specializer::Specializer;

fn specialized<T, U>(ty: T) -> U
where
    T: 'static,
    U: 'static + From<T>,
{
    Specializer::new(ty, From::from)
        .specialize_map_return(From::from, |int: i16| int * 2)
        .specialize_map_param(|int: u8| int * 3, From::from)
        .run()
}

assert_eq!(specialized::<i16, i32>(3), 3);
assert_eq!(specialized::<i8, i16>(3), 6);
assert_eq!(specialized::<u8, i32>(3), 9);
Source

pub fn run(self) -> U

Run the specializer.

Trait Implementations§

Source§

impl<T: Debug, U: Debug, F: Debug> Debug for Specializer<T, U, F>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, U, F> Freeze for Specializer<T, U, F>
where T: Freeze, F: Freeze,

§

impl<T, U, F> RefUnwindSafe for Specializer<T, U, F>

§

impl<T, U, F> Send for Specializer<T, U, F>
where T: Send, F: Send,

§

impl<T, U, F> Sync for Specializer<T, U, F>
where T: Sync, F: Sync,

§

impl<T, U, F> Unpin for Specializer<T, U, F>
where T: Unpin, F: Unpin,

§

impl<T, U, F> UnwindSafe for Specializer<T, U, F>
where T: UnwindSafe, F: 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> 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<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.