ahtml/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
77impl MyFrom<&str> for String {
78 fn myfrom(s: &str) -> Self {
79 s.into()
80 }
81}
82
83impl MyFrom<&&str> for String {
84 fn myfrom(s: &&str) -> Self {
85 s.to_string()
86 }
87}
88
89impl MyFrom<&String> for String {
90 fn myfrom(s: &String) -> Self {
91 s.clone()
92 }
93}
94
95impl MyFrom<String> for String {
96 fn myfrom(s: String) -> Self {
97 s
98 }
99}
100
101impl MyFrom<&KString> for String {
102 fn myfrom(s: &KString) -> Self {
103 s.to_string()
104 }
105}
106
107impl MyFrom<KString> for String {
108 fn myfrom(s: KString) -> Self {
109 s.to_string()
110 }
111}
112
113impl<'t> MyFrom<Cow<'t, str>> for String {
114 fn myfrom(s: Cow<'t, str>) -> Self {
115 s.into()
116 }
117}
118
119impl MyFrom<usize> for String {
120 fn myfrom(val: usize) -> Self {
121 val.to_string()
122 }
123}
124
125// ------------------------------------------------------------------
126
127// / MyFrom must remain owned. Or chaining with MyAsStr as input type
128// / leads to non-owned output types combined with owned input
129// / types. (Does Rust never want From to lead to non-owned types,
130// / BTW?) So we extend the trait here and add them if not chained
131// / with MyAsStr or similar.
132
133// pub trait MyFromInclStr<T> : MyFrom<T> {
134// fn myfrom(s: T) -> Self;
135// }
136
137// impl<'s> MyFromInclStr<&'s str> for &'s str {
138// fn myfrom(s: &'s str) -> Self {
139// s
140// }
141// }
142
143// impl<'s> MyFromInclStr<&&'s str> for &'s str {
144// fn myfrom(s: &&'s str) -> Self {
145// *s
146// }
147// }
148
149// impl<'s> MyFromInclStr<&'s String> for &'s str {
150// fn myfrom(s: &'s String) -> Self {
151// s
152// }
153// }
154
155// impl<'s> MyFromInclStr<&'s KString> for &'s str {
156// fn myfrom(s: &'s KString) -> Self {
157// s
158// }
159// }
160
161// Damn. IS THIS WORKABLE now just deal with no owned inputs?:
162// TODO: proper solution.
163
164impl<'s> MyFrom<&'s str> for &'s str {
165 fn myfrom(s: &'s str) -> Self {
166 s
167 }
168}
169
170impl<'s> MyFrom<&&'s str> for &'s str {
171 fn myfrom(s: &&'s str) -> Self {
172 *s
173 }
174}
175
176impl<'s> MyFrom<&'s String> for &'s str {
177 fn myfrom(s: &'s String) -> Self {
178 s
179 }
180}
181
182impl<'s> MyFrom<&'s KString> for &'s str {
183 fn myfrom(s: &'s KString) -> Self {
184 s
185 }
186}