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
pub trait IntoPropValue<R> {
    fn into_prop_value(self) -> R;
}

impl<R> IntoPropValue<Option<R>> for R {
    fn into_prop_value(self) -> Option<R> {
        Some(self)
    }
}

impl<R> IntoPropValue<R> for R {
    fn into_prop_value(self) -> R {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::IntoPropValue;

    #[test]
    fn simple() {
        let _: i32 = IntoPropValue::into_prop_value(0i32);
        let _: Option<i32> = IntoPropValue::into_prop_value(0i32);
        let _: Option<i32> = IntoPropValue::into_prop_value(Some(0i32));
        let _: Option<i32> = IntoPropValue::into_prop_value(None);
    }

    #[test]
    fn impl_for_custom_type() {
        struct MyNum(f64);

        impl IntoPropValue<MyNum> for f64 {
            fn into_prop_value(self) -> MyNum {
                MyNum(self)
            }
        }

        let _: MyNum = IntoPropValue::into_prop_value(0.0);
    }

    #[test]
    fn with_trait() {
        struct MyNum(i32);

        impl IntoPropValue<i32> for MyNum {
            fn into_prop_value(self) -> i32 {
                self.0
            }
        }

        fn display<D: std::fmt::Display>(v: D) -> String {
            v.to_string()
        }

        assert_eq!(display::<i32>(IntoPropValue::into_prop_value(1)), "1");
        assert_eq!(display::<&str>(IntoPropValue::into_prop_value("2")), "2");
        assert_eq!(
            display::<i32>(IntoPropValue::into_prop_value(MyNum(3))),
            "3"
        );
    }

    #[test]
    fn with_option_trait() {
        fn display<D: std::fmt::Display>(v: Option<D>) -> String {
            v.as_ref()
                .map_or_else(|| String::new(), ToString::to_string)
        }

        assert_eq!(display(IntoPropValue::into_prop_value(1)), "1");
        assert_eq!(display(IntoPropValue::into_prop_value("2")), "2");
        assert_eq!(display::<f64>(IntoPropValue::into_prop_value(None)), "");
    }
}