Skip to content

Commit c38efd0

Browse files
committed
avoid check in vec deref
1 parent 3389c50 commit c38efd0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

library/alloc/src/vec/mod.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -2703,15 +2703,20 @@ impl<T, A: Allocator> ops::Deref for Vec<T, A> {
27032703

27042704
#[inline]
27052705
fn deref(&self) -> &[T] {
2706-
unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
2706+
// slice::from_raw_parts brings in an unsafe precondition check which we want to avoid
2707+
// here in this really hot functions. To forge an invalid pointer, users basically need
2708+
// to transmute to pass an unaligned pointer to from_raw_parts (WHICH SHOULD BE DETECTED THERE, FIXME).
2709+
// So the cost-benefit of this check leans towards not having it.
2710+
unsafe { &*ptr::slice_from_raw_parts(self.as_ptr(), self.len) }
27072711
}
27082712
}
27092713

27102714
#[stable(feature = "rust1", since = "1.0.0")]
27112715
impl<T, A: Allocator> ops::DerefMut for Vec<T, A> {
27122716
#[inline]
27132717
fn deref_mut(&mut self) -> &mut [T] {
2714-
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
2718+
// See deref.
2719+
unsafe { &mut *ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), self.len) }
27152720
}
27162721
}
27172722

0 commit comments

Comments
 (0)