base_traits/traits/
as_str.rs1pub trait AsStr {
22 fn as_str(&self) -> &str;
23}
24
25
26#[cfg(all(not(test), not(feature = "nostd")))]
27impl<T : AsStr + ?Sized> AsStr for Box<T> {
28 fn as_str(&self) -> &str {
29 (**self).as_str()
30 }
31}
32
33#[cfg(all(not(test), not(feature = "nostd")))]
34impl<T : AsStr + ?Sized> AsStr for std::rc::Rc<T> {
35 fn as_str(&self) -> &str {
36 (**self).as_str()
37 }
38}
39
40
41#[cfg(feature = "implement-AsStr-for-built_ins")]
42mod impl_for_built_ins {
43
44 impl super::AsStr for str {
45 #[inline]
46 fn as_str(&self) -> &str {
47 self
48 }
49 }
50
51 impl super::AsStr for &str {
52 #[inline]
53 fn as_str(&self) -> &str {
54 self
55 }
56 }
57}
58
59#[cfg(all(not(feature = "nostd"), feature = "implement-AsStr-for-standard_collection_types"))]
60mod impl_for_std_coll_types {
61
62 impl super::AsStr for String {
63 #[inline]
64 fn as_str(&self) -> &str {
65 self.as_str()
66 }
67 }
68}
69
70
71#[cfg(test)]
72mod tests {
73 #![allow(non_snake_case)]
74
75 use super::AsStr;
76
77 use std::rc::Rc;
78
79
80 #[allow(unused)]
81 fn as_AsStr<T : AsStr>(t : &T) -> &impl AsStr {
82 t
83 }
84
85
86 mod TEST_CUSTOM_TYPE {
87 #![allow(non_snake_case)]
88
89 use super::*;
90
91
92 #[derive(Debug)]
93 struct CustomType {
94 s : String,
95 }
96
97 impl AsStr for CustomType {
98 fn as_str(&self) -> &str {
99 &self.s
100 }
101 }
102
103
104 #[test]
105 fn TEST_AS_VALUE() {
106 let ct = CustomType { s : "abc".into() };
107
108 assert_eq!("abc", ct.as_str());
109
110 let ct = &ct;
111
112 assert_eq!("abc", ct.as_str());
113 }
114
115 #[test]
116 fn TEST_IN_Box() {
117 {
118 let v = CustomType { s : "abc".into() };
119 let ct = Box::new(v);
120
121 assert_eq!("abc", ct.as_str());
122
123 let ct = &ct;
124
125 assert_eq!("abc", ct.as_str());
126 }
127
128 {
129 let v = CustomType { s : "abc".into() };
130 let ct = &Box::new(v);
131
132 assert_eq!("abc", ct.as_str());
133
134 let ct = &ct;
135
136 assert_eq!("abc", ct.as_str());
137 }
138
139 {
140 let v = CustomType { s : "abc".into() };
141 let ct = Box::new(&v);
142
143 assert_eq!("abc", ct.as_str());
144
145 let ct = &ct;
146
147 assert_eq!("abc", ct.as_str());
148 }
149
150 {
151 let v = CustomType { s : "abc".into() };
152 let ct = &Box::new(&v);
153
154 assert_eq!("abc", ct.as_str());
155
156 let ct = &ct;
157
158 assert_eq!("abc", ct.as_str());
159 }
160 }
161
162 #[test]
163 fn TEST_IN_Rc() {
164 {
165 let v = CustomType { s : "abc".into() };
166 let ct = Rc::new(v);
167
168 assert_eq!("abc", ct.as_str());
169
170 let ct = &ct;
171
172 assert_eq!("abc", ct.as_str());
173 }
174
175 {
176 let v = CustomType { s : "abc".into() };
177 let ct = &Rc::new(v);
178
179 assert_eq!("abc", ct.as_str());
180
181 let ct = &ct;
182
183 assert_eq!("abc", ct.as_str());
184 }
185
186 {
187 let v = CustomType { s : "abc".into() };
188 let ct = Rc::new(&v);
189
190 assert_eq!("abc", ct.as_str());
191
192 let ct = &ct;
193
194 assert_eq!("abc", ct.as_str());
195 }
196
197 {
198 let v = CustomType { s : "abc".into() };
199 let ct = &Rc::new(&v);
200
201 assert_eq!("abc", ct.as_str());
202
203 let ct = &ct;
204
205 assert_eq!("abc", ct.as_str());
206 }
207 }
208 }
209}
210
211
212