base_traits/traits/
as_str.rs

1// src/traits/as_str.rs : `AsStr`
2
3/// Trait defining instance method `as_str() : &str` that allows a type to
4/// expose its contiguous character representation to client code.
5///
6/// # Additional Implementations on Foreign Types
7///
8/// ## Built-in Types
9///
10/// If the feature `"implement-AsStr-for-built_ins"`
11/// is defined (as it is by `"default"`), then this is also implemented
12/// for the following type(s):
13/// - [`str`];
14///
15/// ## Standard Collection Types
16///
17/// If the feature `"implement-AsStr-for-standard_collection_types"`
18/// is defined (as it is by `"default"`), then this is also implemented
19/// for the following type(s):
20/// - [`String`];
21pub trait AsStr {
22    fn as_str(&self) -> &str;
23}
24
25
26impl<T : AsStr + ?Sized> AsStr for Box<T> {
27    fn as_str(&self) -> &str {
28        (**self).as_str()
29    }
30}
31
32impl<T : AsStr + ?Sized> AsStr for std::rc::Rc<T> {
33    fn as_str(&self) -> &str {
34        (**self).as_str()
35    }
36}
37
38
39#[cfg(feature = "implement-AsStr-for-built_ins")]
40mod impl_for_built_ins {
41
42    impl super::AsStr for str {
43        #[inline]
44        fn as_str(&self) -> &str {
45            self
46        }
47    }
48
49    impl super::AsStr for &str {
50        #[inline]
51        fn as_str(&self) -> &str {
52            self
53        }
54    }
55}
56
57#[cfg(feature = "implement-AsStr-for-standard_collection_types")]
58mod impl_for_std_coll_types {
59
60    impl super::AsStr for String {
61        #[inline]
62        fn as_str(&self) -> &str {
63            self.as_str()
64        }
65    }
66}
67
68
69#[cfg(test)]
70mod tests {
71    #![allow(non_snake_case)]
72
73    use super::AsStr;
74
75    use std::rc::Rc;
76
77
78    #[allow(unused)]
79    fn as_AsStr<T : AsStr>(t : &T) -> &impl AsStr {
80        t
81    }
82
83
84    mod TEST_CUSTOM_TYPE {
85        #![allow(non_snake_case)]
86
87        use super::*;
88
89
90        #[derive(Debug)]
91        struct CustomType {
92            s : String,
93        }
94
95        impl AsStr for CustomType {
96            fn as_str(&self) -> &str {
97                &self.s
98            }
99        }
100
101
102        #[test]
103        fn TEST_AS_VALUE() {
104            let ct = CustomType { s : "abc".into() };
105
106            assert_eq!("abc", ct.as_str());
107
108            let ct = &ct;
109
110            assert_eq!("abc", ct.as_str());
111        }
112
113        #[test]
114        fn TEST_IN_Box() {
115            {
116                let v = CustomType { s : "abc".into() };
117                let ct = Box::new(v);
118
119                assert_eq!("abc", ct.as_str());
120
121                let ct = &ct;
122
123                assert_eq!("abc", ct.as_str());
124            }
125
126            {
127                let v = CustomType { s : "abc".into() };
128                let ct = &Box::new(v);
129
130                assert_eq!("abc", ct.as_str());
131
132                let ct = &ct;
133
134                assert_eq!("abc", ct.as_str());
135            }
136
137            {
138                let v = CustomType { s : "abc".into() };
139                let ct = Box::new(&v);
140
141                assert_eq!("abc", ct.as_str());
142
143                let ct = &ct;
144
145                assert_eq!("abc", ct.as_str());
146            }
147
148            {
149                let v = CustomType { s : "abc".into() };
150                let ct = &Box::new(&v);
151
152                assert_eq!("abc", ct.as_str());
153
154                let ct = &ct;
155
156                assert_eq!("abc", ct.as_str());
157            }
158        }
159
160        #[test]
161        fn TEST_IN_Rc() {
162            {
163                let v = CustomType { s : "abc".into() };
164                let ct = Rc::new(v);
165
166                assert_eq!("abc", ct.as_str());
167
168                let ct = &ct;
169
170                assert_eq!("abc", ct.as_str());
171            }
172
173            {
174                let v = CustomType { s : "abc".into() };
175                let ct = &Rc::new(v);
176
177                assert_eq!("abc", ct.as_str());
178
179                let ct = &ct;
180
181                assert_eq!("abc", ct.as_str());
182            }
183
184            {
185                let v = CustomType { s : "abc".into() };
186                let ct = Rc::new(&v);
187
188                assert_eq!("abc", ct.as_str());
189
190                let ct = &ct;
191
192                assert_eq!("abc", ct.as_str());
193            }
194
195            {
196                let v = CustomType { s : "abc".into() };
197                let ct = &Rc::new(&v);
198
199                assert_eq!("abc", ct.as_str());
200
201                let ct = &ct;
202
203                assert_eq!("abc", ct.as_str());
204            }
205        }
206    }
207}
208
209
210// ///////////////////////////// end of file //////////////////////////// //
211