Skip to content

Commit aa69c38

Browse files
committed
adressed pedantic clippy warnings
1 parent 788db1b commit aa69c38

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

benches/bench1.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! This benchmark is used to compare the time it takes to create
2-
//! * borrowing MownStr's vs. standard &str references
3-
//! * owning MownStr's vs. Strings
2+
//! * borrowing `MownStr`'s vs. standard &str references
3+
//! * owning `MownStr`'s vs. Strings
44
//!
55
//! The results of `borrowed_mownstr` should therefore be compared to `refs`,
66
//! and that of `owned_mownstr` should be compared to `strings`.
@@ -18,8 +18,8 @@ fn refs(c: &mut Criterion) {
1818
// strangely, replacing .iter().cloned().collect() by to_vec(),
1919
// as suggested by clippy, causes this test to crash :-/
2020
#[allow(clippy::needless_collect)]
21-
let v = i.iter().cloned().collect::<Vec<_>>();
22-
assert!(v.len() == i.len())
21+
let v = i.iter().copied().collect::<Vec<_>>();
22+
assert!(v.len() == i.len());
2323
});
2424
},
2525
);
@@ -33,7 +33,7 @@ fn borrowed_mownstr(c: &mut Criterion) {
3333
b.iter(|| {
3434
#[allow(clippy::needless_collect)]
3535
let v = i.iter().map(|r| MownStr::from(*r)).collect::<Vec<_>>();
36-
assert!(v.len() == i.len())
36+
assert!(v.len() == i.len());
3737
});
3838
},
3939
);
@@ -48,10 +48,10 @@ fn strings(c: &mut Criterion) {
4848
#[allow(clippy::needless_collect)]
4949
let v = i
5050
.iter()
51-
.map(|r| r.to_string())
51+
.map(|r| (*r).to_string())
5252
.map(String::into_boxed_str)
5353
.collect::<Vec<_>>();
54-
assert!(v.len() == i.len())
54+
assert!(v.len() == i.len());
5555
});
5656
},
5757
);
@@ -66,10 +66,10 @@ fn owned_mownstr(c: &mut Criterion) {
6666
#[allow(clippy::needless_collect)]
6767
let v = i
6868
.iter()
69-
.map(|r| r.to_string())
69+
.map(|r| (*r).to_string())
7070
.map(MownStr::from)
7171
.collect::<Vec<_>>();
72-
assert!(v.len() == i.len())
72+
assert!(v.len() == i.len());
7373
});
7474
},
7575
);

src/lib.rs

+25-16
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ const OWN_FLAG: usize = !LEN_MASK;
3939

4040
impl<'a> MownStr<'a> {
4141
#[deprecated = "use from_ref instead. This method caused confusion with FromStr::from_str."]
42+
#[must_use]
4243
pub const fn from_str(other: &'a str) -> MownStr<'a> {
4344
Self::from_ref(other)
4445
}
4546

47+
#[must_use]
4648
pub const fn from_ref(other: &'a str) -> MownStr<'a> {
4749
assert!(other.len() <= LEN_MASK);
4850
// NB: The only 'const' constructor for NonNull is new_unchecked
4951
// so we need an unsafe block.
5052

5153
// SAFETY: we need a *mut u8 for new_unchecked,
5254
// but MownStr will never mutate its content
53-
let ptr = other.as_ptr() as *mut u8;
55+
let ptr = other.as_ptr().cast_mut();
5456
let addr = unsafe {
5557
// SAFETY: ptr can not be null,
5658
NonNull::new_unchecked(ptr)
@@ -62,14 +64,17 @@ impl<'a> MownStr<'a> {
6264
}
6365
}
6466

67+
#[must_use]
6568
pub const fn is_borrowed(&self) -> bool {
6669
(self.xlen & OWN_FLAG) == 0
6770
}
6871

72+
#[must_use]
6973
pub const fn is_owned(&self) -> bool {
7074
(self.xlen & OWN_FLAG) == OWN_FLAG
7175
}
7276

77+
#[must_use]
7378
pub const fn borrowed(&self) -> MownStr {
7479
MownStr {
7580
addr: self.addr,
@@ -91,7 +96,7 @@ impl<'a> MownStr<'a> {
9196
str::from_utf8_unchecked(slice)
9297
}
9398

94-
/// Convert an *owned* MownStr to a box.
99+
/// Convert an *owned* `MownStr` to a box.
95100
//
96101
// NB: conceptually this method consumes the Mownstr.
97102
// 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> {
125130
impl<'a> Clone for MownStr<'a> {
126131
fn clone(&self) -> MownStr<'a> {
127132
if self.is_owned() {
128-
Box::<str>::from(self.deref()).into()
133+
Box::<str>::from(&**self).into()
129134
} else {
130135
MownStr {
131136
addr: self.addr,
@@ -155,8 +160,11 @@ impl<'a> From<Box<str>> for MownStr<'a> {
155160
};
156161

157162
let xlen = len | OWN_FLAG;
158-
let _phd = PhantomData;
159-
MownStr { addr, xlen, _phd }
163+
MownStr {
164+
addr,
165+
xlen,
166+
_phd: PhantomData,
167+
}
160168
}
161169
}
162170

@@ -192,27 +200,27 @@ impl<'a> Deref for MownStr<'a> {
192200

193201
impl<'a> AsRef<str> for MownStr<'a> {
194202
fn as_ref(&self) -> &str {
195-
self.deref()
203+
self
196204
}
197205
}
198206

199207
impl<'a> std::borrow::Borrow<str> for MownStr<'a> {
200208
fn borrow(&self) -> &str {
201-
self.deref()
209+
self
202210
}
203211
}
204212

205213
// Comparing between MownStr
206214

207215
impl<'a> hash::Hash for MownStr<'a> {
208216
fn hash<H: hash::Hasher>(&self, state: &mut H) {
209-
self.deref().hash(state)
217+
self.deref().hash(state);
210218
}
211219
}
212220

213221
impl<'a> PartialEq for MownStr<'a> {
214222
fn eq(&self, other: &MownStr<'a>) -> bool {
215-
self.deref() == other.deref()
223+
**self == **other
216224
}
217225
}
218226

@@ -226,15 +234,15 @@ impl<'a> PartialOrd for MownStr<'a> {
226234

227235
impl<'a> Ord for MownStr<'a> {
228236
fn cmp(&self, other: &MownStr<'a>) -> std::cmp::Ordering {
229-
self.deref().cmp(other.deref())
237+
self.deref().cmp(&**other)
230238
}
231239
}
232240

233241
// Comparing MownStr with str
234242

235243
impl<'a> PartialEq<&'a str> for MownStr<'a> {
236244
fn eq(&self, other: &&'a str) -> bool {
237-
self.deref() == *other
245+
&**self == *other
238246
}
239247
}
240248

@@ -246,27 +254,27 @@ impl<'a> PartialOrd<&'a str> for MownStr<'a> {
246254

247255
impl<'a> PartialEq<MownStr<'a>> for &'a str {
248256
fn eq(&self, other: &MownStr<'a>) -> bool {
249-
self == &other.deref()
257+
self == &&**other
250258
}
251259
}
252260

253261
impl<'a> PartialOrd<MownStr<'a>> for &'a str {
254262
fn partial_cmp(&self, other: &MownStr<'a>) -> Option<std::cmp::Ordering> {
255-
self.partial_cmp(&other.deref())
263+
self.partial_cmp(&&**other)
256264
}
257265
}
258266

259267
// Formatting
260268

261269
impl<'a> fmt::Debug for MownStr<'a> {
262270
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
263-
fmt::Debug::fmt(self.deref(), f)
271+
fmt::Debug::fmt(&**self, f)
264272
}
265273
}
266274

267275
impl<'a> fmt::Display for MownStr<'a> {
268276
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
269-
fmt::Display::fmt(self.deref(), f)
277+
fmt::Display::fmt(&**self, f)
270278
}
271279
}
272280

@@ -311,6 +319,7 @@ impl<'a> MownStr<'a> {
311319
/// let o1 = Some(MownStr::from("hi there"));
312320
/// let o2 = o1.map(MownStr::to::<Rc<str>>);
313321
/// ```
322+
#[must_use]
314323
pub fn to<T>(mut self) -> T
315324
where
316325
T: From<&'a str> + From<Box<str>>,
@@ -348,7 +357,7 @@ mod test {
348357

349358
#[test]
350359
fn empty_string() {
351-
let empty = "".to_string();
360+
let empty = String::new();
352361
let _ = MownStr::from(empty);
353362
}
354363

0 commit comments

Comments
 (0)