1pub trait IntoPropValue<R> {
2 fn into_prop_value(self) -> R;
3}
4
5impl<R> IntoPropValue<Option<R>> for R {
6 fn into_prop_value(self) -> Option<R> {
7 Some(self)
8 }
9}
10
11impl<R> IntoPropValue<R> for R {
12 fn into_prop_value(self) -> R {
13 self
14 }
15}
16
17#[cfg(test)]
18mod tests {
19 use super::IntoPropValue;
20
21 #[test]
22 fn simple() {
23 let _: i32 = IntoPropValue::into_prop_value(0i32);
24 let _: Option<i32> = IntoPropValue::into_prop_value(0i32);
25 let _: Option<i32> = IntoPropValue::into_prop_value(Some(0i32));
26 let _: Option<i32> = IntoPropValue::into_prop_value(None);
27 }
28
29 #[test]
30 fn impl_for_custom_type() {
31 struct MyNum(f64);
32
33 impl IntoPropValue<MyNum> for f64 {
34 fn into_prop_value(self) -> MyNum {
35 MyNum(self)
36 }
37 }
38
39 let _: MyNum = IntoPropValue::into_prop_value(0.0);
40 }
41
42 #[test]
43 fn with_trait() {
44 struct MyNum(i32);
45
46 impl IntoPropValue<i32> for MyNum {
47 fn into_prop_value(self) -> i32 {
48 self.0
49 }
50 }
51
52 fn display<D: std::fmt::Display>(v: D) -> String {
53 v.to_string()
54 }
55
56 assert_eq!(display::<i32>(IntoPropValue::into_prop_value(1)), "1");
57 assert_eq!(display::<&str>(IntoPropValue::into_prop_value("2")), "2");
58 assert_eq!(
59 display::<i32>(IntoPropValue::into_prop_value(MyNum(3))),
60 "3"
61 );
62 }
63
64 #[test]
65 fn with_option_trait() {
66 fn display<D: std::fmt::Display>(v: Option<D>) -> String {
67 v.as_ref()
68 .map_or_else(|| String::new(), ToString::to_string)
69 }
70
71 assert_eq!(display(IntoPropValue::into_prop_value(1)), "1");
72 assert_eq!(display(IntoPropValue::into_prop_value("2")), "2");
73 assert_eq!(display::<f64>(IntoPropValue::into_prop_value(None)), "");
74 }
75}