ahtml_html/
myfrom.rs

1// FUTURE: figure out how to inherit from `From` (and keep all the
2// existing From definitions for KString). It doesn't work out of the
3// box (various errors).
4pub trait MyFrom<T> {
5    fn myfrom(s: T) -> Self;
6}
7
8use std::borrow::Cow;
9
10use kstring::KString;
11
12// Can't do KString::from_static: no way to have a separate trait impl
13// for &'static.
14impl MyFrom<&str> for KString {
15    fn myfrom(s: &str) -> Self {
16        KString::from_ref(s)
17    }
18}
19
20impl MyFrom<&&str> for KString {
21    fn myfrom(s: &&str) -> Self {
22        KString::from_ref(*s)
23    }
24}
25
26impl MyFrom<&String> for KString {
27    fn myfrom(s: &String) -> Self {
28        KString::from_ref(s)
29    }
30}
31
32impl MyFrom<String> for KString {
33    fn myfrom(s: String) -> Self {
34        KString::from_string(s)
35    }
36}
37
38impl MyFrom<&KString> for KString {
39    fn myfrom(s: &KString) -> Self {
40        s.clone()
41    }
42}
43
44impl MyFrom<KString> for KString {
45    fn myfrom(s: KString) -> Self {
46        s
47    }
48}
49
50impl<'t> MyFrom<Cow<'t, str>> for KString {
51    fn myfrom(s: Cow<'t, str>) -> Self {
52        KString::from_ref(s.as_ref())
53    }
54}
55
56impl MyFrom<usize> for KString {
57    fn myfrom(val: usize) -> Self {
58        // exact size needed ?
59        // let mut buf: [u8; 32] = Default::default();
60        // let outp: &mut [u8] = &mut buf;
61        // let n = write!(outp, "{}", val).expect("enough space for the formatted number");
62        KString::from_string(val.to_string())
63    }
64}
65
66// impl<'t> MyFrom<HtmlString> for KString {
67//     fn myfrom(s: HtmlString) -> Self {
68//         let s2 = String::from_utf8(*s)?;
69//         KString::from_string(s2)
70//     }
71// }
72// Ah, cannot have errors here. So, do the conversion manually
73// outside, please.
74
75// ------------------------------------------------------------------
76
77// / MyFrom must remain owned. Or chaining with MyAsStr as input type
78// / leads to non-owned output types combined with owned input
79// / types. (Does Rust never want From to lead to non-owned types,
80// / BTW?)  So we extend the trait here and add them if not chained
81// / with MyAsStr or similar.
82
83// pub trait MyFromInclStr<T> : MyFrom<T> {
84//     fn myfrom(s: T) -> Self;
85// }
86
87// impl<'s> MyFromInclStr<&'s str> for &'s str {
88//     fn myfrom(s: &'s str) -> Self {
89//         s
90//     }
91// }
92
93// impl<'s> MyFromInclStr<&&'s str> for &'s str {
94//     fn myfrom(s: &&'s str) -> Self {
95//         *s
96//     }
97// }
98
99// impl<'s> MyFromInclStr<&'s String> for &'s str {
100//     fn myfrom(s: &'s String) -> Self {
101//         s
102//     }
103// }
104
105// impl<'s> MyFromInclStr<&'s KString> for &'s str {
106//     fn myfrom(s: &'s KString) -> Self {
107//         s
108//     }
109// }
110
111// Damn. IS THIS WORKABLE now  just deal with no owned inputs?:
112// TODO: proper solution.
113
114impl<'s> MyFrom<&'s str> for &'s str {
115    fn myfrom(s: &'s str) -> Self {
116        s
117    }
118}
119
120impl<'s> MyFrom<&&'s str> for &'s str {
121    fn myfrom(s: &&'s str) -> Self {
122        *s
123    }
124}
125
126impl<'s> MyFrom<&'s String> for &'s str {
127    fn myfrom(s: &'s String) -> Self {
128        s
129    }
130}
131
132impl<'s> MyFrom<&'s KString> for &'s str {
133    fn myfrom(s: &'s KString) -> Self {
134        s
135    }
136}