@@ -39,18 +39,20 @@ const OWN_FLAG: usize = !LEN_MASK;
39
39
40
40
impl < ' a > MownStr < ' a > {
41
41
#[ deprecated = "use from_ref instead. This method caused confusion with FromStr::from_str." ]
42
+ #[ must_use]
42
43
pub const fn from_str ( other : & ' a str ) -> MownStr < ' a > {
43
44
Self :: from_ref ( other)
44
45
}
45
46
47
+ #[ must_use]
46
48
pub const fn from_ref ( other : & ' a str ) -> MownStr < ' a > {
47
49
assert ! ( other. len( ) <= LEN_MASK ) ;
48
50
// NB: The only 'const' constructor for NonNull is new_unchecked
49
51
// so we need an unsafe block.
50
52
51
53
// SAFETY: we need a *mut u8 for new_unchecked,
52
54
// but MownStr will never mutate its content
53
- let ptr = other. as_ptr ( ) as * mut u8 ;
55
+ let ptr = other. as_ptr ( ) . cast_mut ( ) ;
54
56
let addr = unsafe {
55
57
// SAFETY: ptr can not be null,
56
58
NonNull :: new_unchecked ( ptr)
@@ -62,14 +64,17 @@ impl<'a> MownStr<'a> {
62
64
}
63
65
}
64
66
67
+ #[ must_use]
65
68
pub const fn is_borrowed ( & self ) -> bool {
66
69
( self . xlen & OWN_FLAG ) == 0
67
70
}
68
71
72
+ #[ must_use]
69
73
pub const fn is_owned ( & self ) -> bool {
70
74
( self . xlen & OWN_FLAG ) == OWN_FLAG
71
75
}
72
76
77
+ #[ must_use]
73
78
pub const fn borrowed ( & self ) -> MownStr {
74
79
MownStr {
75
80
addr : self . addr ,
@@ -91,7 +96,7 @@ impl<'a> MownStr<'a> {
91
96
str:: from_utf8_unchecked ( slice)
92
97
}
93
98
94
- /// Convert an *owned* MownStr to a box.
99
+ /// Convert an *owned* ` MownStr` to a box.
95
100
//
96
101
// NB: conceptually this method consumes the Mownstr.
97
102
// The reason why self is a mutable ref instead of a move is purely technical
@@ -125,7 +130,7 @@ impl<'a> Drop for MownStr<'a> {
125
130
impl < ' a > Clone for MownStr < ' a > {
126
131
fn clone ( & self ) -> MownStr < ' a > {
127
132
if self . is_owned ( ) {
128
- Box :: < str > :: from ( self . deref ( ) ) . into ( )
133
+ Box :: < str > :: from ( & * * self ) . into ( )
129
134
} else {
130
135
MownStr {
131
136
addr : self . addr ,
@@ -155,8 +160,11 @@ impl<'a> From<Box<str>> for MownStr<'a> {
155
160
} ;
156
161
157
162
let xlen = len | OWN_FLAG ;
158
- let _phd = PhantomData ;
159
- MownStr { addr, xlen, _phd }
163
+ MownStr {
164
+ addr,
165
+ xlen,
166
+ _phd : PhantomData ,
167
+ }
160
168
}
161
169
}
162
170
@@ -192,27 +200,27 @@ impl<'a> Deref for MownStr<'a> {
192
200
193
201
impl < ' a > AsRef < str > for MownStr < ' a > {
194
202
fn as_ref ( & self ) -> & str {
195
- self . deref ( )
203
+ self
196
204
}
197
205
}
198
206
199
207
impl < ' a > std:: borrow:: Borrow < str > for MownStr < ' a > {
200
208
fn borrow ( & self ) -> & str {
201
- self . deref ( )
209
+ self
202
210
}
203
211
}
204
212
205
213
// Comparing between MownStr
206
214
207
215
impl < ' a > hash:: Hash for MownStr < ' a > {
208
216
fn hash < H : hash:: Hasher > ( & self , state : & mut H ) {
209
- self . deref ( ) . hash ( state)
217
+ self . deref ( ) . hash ( state) ;
210
218
}
211
219
}
212
220
213
221
impl < ' a > PartialEq for MownStr < ' a > {
214
222
fn eq ( & self , other : & MownStr < ' a > ) -> bool {
215
- self . deref ( ) == other. deref ( )
223
+ * * self == * * other
216
224
}
217
225
}
218
226
@@ -226,15 +234,15 @@ impl<'a> PartialOrd for MownStr<'a> {
226
234
227
235
impl < ' a > Ord for MownStr < ' a > {
228
236
fn cmp ( & self , other : & MownStr < ' a > ) -> std:: cmp:: Ordering {
229
- self . deref ( ) . cmp ( other. deref ( ) )
237
+ self . deref ( ) . cmp ( & * * other)
230
238
}
231
239
}
232
240
233
241
// Comparing MownStr with str
234
242
235
243
impl < ' a > PartialEq < & ' a str > for MownStr < ' a > {
236
244
fn eq ( & self , other : & & ' a str ) -> bool {
237
- self . deref ( ) == * other
245
+ & * * self == * other
238
246
}
239
247
}
240
248
@@ -246,27 +254,27 @@ impl<'a> PartialOrd<&'a str> for MownStr<'a> {
246
254
247
255
impl < ' a > PartialEq < MownStr < ' a > > for & ' a str {
248
256
fn eq ( & self , other : & MownStr < ' a > ) -> bool {
249
- self == & other. deref ( )
257
+ self == & & * * other
250
258
}
251
259
}
252
260
253
261
impl < ' a > PartialOrd < MownStr < ' a > > for & ' a str {
254
262
fn partial_cmp ( & self , other : & MownStr < ' a > ) -> Option < std:: cmp:: Ordering > {
255
- self . partial_cmp ( & other. deref ( ) )
263
+ self . partial_cmp ( & & * * other)
256
264
}
257
265
}
258
266
259
267
// Formatting
260
268
261
269
impl < ' a > fmt:: Debug for MownStr < ' a > {
262
270
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
263
- fmt:: Debug :: fmt ( self . deref ( ) , f)
271
+ fmt:: Debug :: fmt ( & * * self , f)
264
272
}
265
273
}
266
274
267
275
impl < ' a > fmt:: Display for MownStr < ' a > {
268
276
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
269
- fmt:: Display :: fmt ( self . deref ( ) , f)
277
+ fmt:: Display :: fmt ( & * * self , f)
270
278
}
271
279
}
272
280
@@ -311,6 +319,7 @@ impl<'a> MownStr<'a> {
311
319
/// let o1 = Some(MownStr::from("hi there"));
312
320
/// let o2 = o1.map(MownStr::to::<Rc<str>>);
313
321
/// ```
322
+ #[ must_use]
314
323
pub fn to < T > ( mut self ) -> T
315
324
where
316
325
T : From < & ' a str > + From < Box < str > > ,
@@ -348,7 +357,7 @@ mod test {
348
357
349
358
#[ test]
350
359
fn empty_string ( ) {
351
- let empty = "" . to_string ( ) ;
360
+ let empty = String :: new ( ) ;
352
361
let _ = MownStr :: from ( empty) ;
353
362
}
354
363
0 commit comments