Skip to content

Commit 7514d76

Browse files
committed
cleanup some of the less terrifying library code
1 parent 31e1cde commit 7514d76

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

library/alloc/src/slice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,9 +1043,9 @@ where
10431043

10441044
impl<T> Drop for MergeHole<T> {
10451045
fn drop(&mut self) {
1046-
// `T` is not a zero-sized type, so it's okay to divide by its size.
1047-
let len = (self.end.addr() - self.start.addr()) / mem::size_of::<T>();
1046+
// `T` is not a zero-sized type, and these are pointers into a slice's elements.
10481047
unsafe {
1048+
let len = self.end.offset_from(self.start) as usize;
10491049
ptr::copy_nonoverlapping(self.start, self.dest, len);
10501050
}
10511051
}

library/core/src/slice/ascii.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,17 @@ fn is_ascii(s: &[u8]) -> bool {
294294
// Paranoia check about alignment, since we're about to do a bunch of
295295
// unaligned loads. In practice this should be impossible barring a bug in
296296
// `align_offset` though.
297-
debug_assert_eq!((word_ptr.addr()) % mem::align_of::<usize>(), 0);
297+
debug_assert_eq!(word_ptr.addr() % mem::align_of::<usize>(), 0);
298298

299299
// Read subsequent words until the last aligned word, excluding the last
300300
// aligned word by itself to be done in tail check later, to ensure that
301301
// tail is always one `usize` at most to extra branch `byte_pos == len`.
302302
while byte_pos < len - USIZE_SIZE {
303303
debug_assert!(
304304
// Sanity check that the read is in bounds
305-
(word_ptr.addr() + USIZE_SIZE) <= (start.wrapping_add(len).addr()) &&
305+
(word_ptr.addr() + USIZE_SIZE) <= start.addr().wrapping_add(len) &&
306306
// And that our assumptions about `byte_pos` hold.
307-
(word_ptr.addr()) - (start.addr()) == byte_pos
307+
(word_ptr.addr() - start.addr()) == byte_pos
308308
);
309309

310310
// SAFETY: We know `word_ptr` is properly aligned (because of

library/core/src/slice/iter/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ macro_rules! len {
2020
if size == 0 {
2121
// This _cannot_ use `unchecked_sub` because we depend on wrapping
2222
// to represent the length of long ZST slice iterators.
23-
($self.end.addr()).wrapping_sub(start.as_ptr().addr())
23+
$self.end.addr().wrapping_sub(start.as_ptr().addr())
2424
} else {
2525
// We know that `start <= end`, so can do better than `offset_from`,
2626
// which needs to deal in signed. By setting appropriate flags here

library/std/src/io/error/repr_bitpacked.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl Repr {
136136
let p = Box::into_raw(b).cast::<u8>();
137137
// Should only be possible if an allocator handed out a pointer with
138138
// wrong alignment.
139-
debug_assert_eq!((p.addr() & TAG_MASK), 0);
139+
debug_assert_eq!(p.addr() & TAG_MASK, 0);
140140
// Note: We know `TAG_CUSTOM <= size_of::<Custom>()` (static_assert at
141141
// end of file), and both the start and end of the expression must be
142142
// valid without address space wraparound due to `Box`'s semantics.

0 commit comments

Comments
 (0)