Skip to main content

timsrust_core/coordinates/
converters.rs

1pub trait Converter<Input, Output> {
2    fn convert(&self, value: Input) -> Output;
3
4    fn batch_convert(&self, values: &[Input]) -> Vec<Output>
5    where
6        Input: Clone,
7    {
8        values.iter().map(|v| self.convert(v.clone())).collect()
9    }
10}
11
12pub trait InvertibleConverter<Input, Output>:
13    Converter<Input, Output> + Converter<Output, Input>
14{
15}
16
17impl<Input, Output, T> InvertibleConverter<Input, Output> for T where
18    T: Converter<Input, Output> + Converter<Output, Input>
19{
20}
21
22pub trait ConvertibleTo<Output>: Sized + Copy {
23    fn convert<C: Converter<Self, Output>>(&self, converter: &C) -> Output {
24        converter.convert(*self)
25    }
26}
27
28// impl<Input, Output, T, U> Converter<Input, Output> for T
29// where
30//     T: Deref<Target = U>,
31//     U: Converter<Input, Output>,
32impl<Input, Output, T> Converter<Input, Output> for &T
33where
34    T: Converter<Input, Output>,
35{
36    fn convert(&self, value: Input) -> Output {
37        (*self).convert(value)
38    }
39
40    // fn batch_convert(&self, values: &[Input]) -> Vec<Output>
41    // where
42    //     Input: Clone,
43    // {
44    //     (*self).batch_convert(values)
45    // }
46}
47
48#[derive(Debug, Clone)]
49pub struct BitConverter();