pub trait DispatchConst {
    type Constant;

    const CONST: Self::Constant;
}
Expand description

A dispatch of a constant.

Prefer using the dispatch_const macro unless the dispatcher will be used several times.

§Examples

#[derive(Debug, Default, PartialEq, Eq)]
struct Painting(Vec<usize>);
impl Painting {
    fn draw(&mut self, stroke: usize) {
        self.0.push(2 * stroke)
    }
}
#[derive(Debug, Default, PartialEq, Eq)]
struct Photoshop(Vec<usize>);
impl Photoshop {
    fn draw(&mut self, filter: usize) {
        self.0.push(5 * filter)
    }
}

#[derive(Default)]
struct Dispatcher<T>(PhantomData<T>);

impl DispatchConst for Dispatcher<Painting> {
    type Constant = usize;
    const CONST: Self::Constant = 5;
}
impl DispatchConst for Dispatcher<Photoshop> {
    type Constant = usize;
    const CONST: Self::Constant = 3;
}

struct SingleStrokeMasterpiece;

#[impl_dispatch({Painting; Photoshop})]
impl<A> Into<A> for SingleStrokeMasterpiece {
    fn into(self) -> A {
        let mut drawing = A::default();
        drawing.draw(Dispatcher::<A>::CONST);
        drawing
    }
}

let painting: Painting = SingleStrokeMasterpiece.into();
let photoshop: Photoshop = SingleStrokeMasterpiece.into();
assert_eq!(painting, Painting(vec![10]));
assert_eq!(photoshop, Photoshop(vec![15]));

Required Associated Types§

source

type Constant

The type of the constant.

Required Associated Constants§

source

const CONST: Self::Constant

The constant.

Object Safety§

This trait is not object safe.

Implementors§