1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Utilities for dispatching based on generic types.

#![warn(missing_docs)]

extern crate self as type_dispatch;

#[doc(hidden)]
pub use duplicate;
pub use type_dispatch_macros::*;

pub mod derive;
mod tests;

/// A dispatch of an object.
///
/// # Examples
///
/// ```
/// # use std::marker::PhantomData;
/// # use type_dispatch::Dispatch;
/// # use type_dispatch_macros::impl_dispatch;
/// #[derive(Debug, Default, PartialEq, Eq)]
/// struct Painting(Vec<usize>);
/// struct Stroke {
///     thickness: usize,
/// }
/// impl Painting {
///     fn draw(&mut self, stroke: Stroke) {
///         self.0.push(2 * stroke.thickness)
///     }
/// }
/// #[derive(Debug, Default, PartialEq, Eq)]
/// struct Photoshop(Vec<usize>);
/// struct Filter {
///     strength: usize,
/// }
/// impl Photoshop {
///     fn draw(&mut self, filter: Filter) {
///         self.0.push(5 * filter.strength)
///     }
/// }
///
/// struct Dispatcher<T>(usize, PhantomData<T>);
///
/// impl Dispatch for Dispatcher<Painting> {
///     type Output = Stroke;
///     fn dispatch(self) -> Self::Output {
///         Stroke { thickness: self.0 }
///     }
/// }
/// impl Dispatch for Dispatcher<Photoshop> {
///     type Output = Filter;
///     fn dispatch(self) -> Self::Output {
///         Filter { strength: self.0 }
///     }
/// }
///
/// #[impl_dispatch({Painting; Photoshop})]
/// impl<A> Into<A> for Vec<usize> {
///     fn into(self) -> A {
///         let mut drawing = A::default();
///         for num in self {
///             drawing.draw(Dispatcher(num, PhantomData::<A>).dispatch());
///         }
///         drawing
///     }
/// }
///
/// let painting: Painting = vec![1, 2, 3].into();
/// let photoshop: Photoshop = vec![1, 2, 3].into();
/// assert_eq!(painting, Painting(vec![2, 4, 6]));
/// assert_eq!(photoshop, Photoshop(vec![5, 10, 15]));
/// ```
pub trait Dispatch {
    /// The type of the output object.
    type Output;

    /// Dispatches the object to a new object.
    fn dispatch(self) -> Self::Output;
}

/// A dispatch of a static function.
///
/// Prefer using the [`dispatch_fn`] macro unless the dispatcher will
/// be used several times.
///
/// # Examples
///
/// ```
/// # use std::marker::PhantomData;
/// # use type_dispatch::DispatchFn;
/// # use type_dispatch_macros::impl_dispatch;
/// #[derive(Debug, Default, PartialEq, Eq)]
/// struct Painting(Vec<usize>);
/// struct Stroke {
///     thickness: usize,
/// }
/// impl Painting {
///     fn draw(&mut self, stroke: Stroke) {
///         self.0.push(2 * stroke.thickness)
///     }
/// }
/// #[derive(Debug, Default, PartialEq, Eq)]
/// struct Photoshop(Vec<usize>);
/// struct Filter {
///     strength: usize,
/// }
/// impl Photoshop {
///     fn draw(&mut self, filter: Filter) {
///         self.0.push(5 * filter.strength)
///     }
/// }
///
/// #[derive(Default)]
/// struct Dispatcher<T>(PhantomData<T>);
///
/// impl DispatchFn for Dispatcher<Painting> {
///     type Output = Stroke;
///     fn dispatch_fn() -> Self::Output {
///         Stroke { thickness: 5 }
///     }
/// }
/// impl DispatchFn for Dispatcher<Photoshop> {
///     type Output = Filter;
///     fn dispatch_fn() -> Self::Output {
///         Filter { strength: 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>::dispatch_fn());
///         drawing
///     }
/// }
///
/// let painting: Painting = SingleStrokeMasterpiece.into();
/// let photoshop: Photoshop = SingleStrokeMasterpiece.into();
/// assert_eq!(painting, Painting(vec![10]));
/// assert_eq!(photoshop, Photoshop(vec![15]));
/// ```
pub trait DispatchFn {
    /// The type of the dispatched function's output.
    type Output;

    /// Dispatches a static function.
    fn dispatch_fn() -> Self::Output;
}

/// A dispatch of a constant.
///
/// Prefer using the [`dispatch_const`] macro unless the dispatcher will
/// be used several times.
///
/// # Examples
///
///
/// ```
/// # use std::marker::PhantomData;
/// # use type_dispatch::{DispatchConst, DispatchFn};
/// # use type_dispatch_macros::impl_dispatch;
/// #[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]));
/// ```
pub trait DispatchConst {
    /// The type of the constant.
    type Constant;

    /// The constant.
    const CONST: Self::Constant;
}