structsy/
projection.rs

1use crate::{Persistent, Ref};
2
3pub trait Projection<T> {
4    fn projection(source: &T) -> Self;
5}
6
7macro_rules! projections {
8    ($($t:ty),+) => {
9        $(
10        impl Projection<$t> for $t {
11            fn projection(source:&$t) -> Self {
12                *source
13            }
14        }
15        )+
16    }
17}
18
19projections!(bool, u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, f32, f64);
20
21impl Projection<String> for String {
22    fn projection(source: &String) -> Self {
23        source.clone()
24    }
25}
26impl<T: Persistent> Projection<Ref<T>> for Ref<T> {
27    fn projection(source: &Ref<T>) -> Self {
28        source.clone()
29    }
30}
31impl<T: Projection<T>> Projection<Vec<T>> for Vec<T> {
32    fn projection(source: &Vec<T>) -> Self {
33        source.iter().map(Projection::projection).collect()
34    }
35}
36
37impl<T: Projection<T>> Projection<Option<T>> for Option<T> {
38    fn projection(source: &Option<T>) -> Self {
39        source.as_ref().map(Projection::projection)
40    }
41}