Skip to content

Commit f001cf2

Browse files
authored
Refactor unsafe blocks; update TODO comments (#432)
Update TODO comments which track adding safety comments to `unsafe` blocks which are missing them. Previously, we used #61 to track these. Now, we're using #429.
1 parent b083f1f commit f001cf2

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

src/lib.rs

+34-28
Original file line numberDiff line numberDiff line change
@@ -754,13 +754,15 @@ pub unsafe trait FromZeroes {
754754
return Box::new(Self::new_zeroed());
755755
}
756756

757-
// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
757+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
758+
#[allow(clippy::undocumented_unsafe_blocks)]
759+
let ptr = unsafe { alloc::alloc::alloc_zeroed(layout).cast::<Self>() };
760+
if ptr.is_null() {
761+
alloc::alloc::handle_alloc_error(layout);
762+
}
763+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
758764
#[allow(clippy::undocumented_unsafe_blocks)]
759765
unsafe {
760-
let ptr = alloc::alloc::alloc_zeroed(layout).cast::<Self>();
761-
if ptr.is_null() {
762-
alloc::alloc::handle_alloc_error(layout);
763-
}
764766
Box::from_raw(ptr)
765767
}
766768
}
@@ -810,21 +812,25 @@ pub unsafe trait FromZeroes {
810812
let layout =
811813
Layout::from_size_align(size, align).expect("total allocation size overflows `isize`");
812814

813-
// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
815+
let ptr = if layout.size() != 0 {
816+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
817+
#[allow(clippy::undocumented_unsafe_blocks)]
818+
let ptr = unsafe { alloc::alloc::alloc_zeroed(layout).cast::<Self>() };
819+
if ptr.is_null() {
820+
alloc::alloc::handle_alloc_error(layout);
821+
}
822+
ptr
823+
} else {
824+
// `Box<[T]>` does not allocate when `T` is zero-sized or when `len`
825+
// is zero, but it does require a non-null dangling pointer for its
826+
// allocation.
827+
NonNull::<Self>::dangling().as_ptr()
828+
};
829+
830+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
814831
#[allow(clippy::undocumented_unsafe_blocks)]
815832
unsafe {
816-
if layout.size() != 0 {
817-
let ptr = alloc::alloc::alloc_zeroed(layout).cast::<Self>();
818-
if ptr.is_null() {
819-
alloc::alloc::handle_alloc_error(layout);
820-
}
821-
Box::from_raw(slice::from_raw_parts_mut(ptr, len))
822-
} else {
823-
// `Box<[T]>` does not allocate when `T` is zero-sized or when
824-
// `len` is zero, but it does require a non-null dangling
825-
// pointer for its allocation.
826-
Box::from_raw(slice::from_raw_parts_mut(NonNull::<Self>::dangling().as_ptr(), len))
827-
}
833+
Box::from_raw(slice::from_raw_parts_mut(ptr, len))
828834
}
829835
}
830836

@@ -2328,7 +2334,7 @@ where
23282334
/// and no mutable references to the same memory may be constructed during
23292335
/// `'a`.
23302336
unsafe fn deref_helper<'a>(&self) -> &'a T {
2331-
// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2337+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
23322338
#[allow(clippy::undocumented_unsafe_blocks)]
23332339
unsafe {
23342340
&*self.0.as_ptr().cast::<T>()
@@ -2353,7 +2359,7 @@ where
23532359
/// and no other references - mutable or immutable - to the same memory may
23542360
/// be constructed during `'a`.
23552361
unsafe fn deref_mut_helper<'a>(&mut self) -> &'a mut T {
2356-
// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2362+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
23572363
#[allow(clippy::undocumented_unsafe_blocks)]
23582364
unsafe {
23592365
&mut *self.0.as_mut_ptr().cast::<T>()
@@ -2382,7 +2388,7 @@ where
23822388
debug_assert_eq!(len % elem_size, 0);
23832389
len / elem_size
23842390
};
2385-
// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2391+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
23862392
#[allow(clippy::undocumented_unsafe_blocks)]
23872393
unsafe {
23882394
slice::from_raw_parts(self.0.as_ptr().cast::<T>(), elems)
@@ -2412,7 +2418,7 @@ where
24122418
debug_assert_eq!(len % elem_size, 0);
24132419
len / elem_size
24142420
};
2415-
// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2421+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
24162422
#[allow(clippy::undocumented_unsafe_blocks)]
24172423
unsafe {
24182424
slice::from_raw_parts_mut(self.0.as_mut_ptr().cast::<T>(), elems)
@@ -2754,7 +2760,7 @@ pub unsafe trait ByteSliceMut: ByteSlice + DerefMut {
27542760
}
27552761

27562762
impl<'a> sealed::ByteSliceSealed for &'a [u8] {}
2757-
// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2763+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
27582764
#[allow(clippy::undocumented_unsafe_blocks)]
27592765
unsafe impl<'a> ByteSlice for &'a [u8] {
27602766
#[inline]
@@ -2764,7 +2770,7 @@ unsafe impl<'a> ByteSlice for &'a [u8] {
27642770
}
27652771

27662772
impl<'a> sealed::ByteSliceSealed for &'a mut [u8] {}
2767-
// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2773+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
27682774
#[allow(clippy::undocumented_unsafe_blocks)]
27692775
unsafe impl<'a> ByteSlice for &'a mut [u8] {
27702776
#[inline]
@@ -2774,7 +2780,7 @@ unsafe impl<'a> ByteSlice for &'a mut [u8] {
27742780
}
27752781

27762782
impl<'a> sealed::ByteSliceSealed for cell::Ref<'a, [u8]> {}
2777-
// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2783+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
27782784
#[allow(clippy::undocumented_unsafe_blocks)]
27792785
unsafe impl<'a> ByteSlice for cell::Ref<'a, [u8]> {
27802786
#[inline]
@@ -2784,7 +2790,7 @@ unsafe impl<'a> ByteSlice for cell::Ref<'a, [u8]> {
27842790
}
27852791

27862792
impl<'a> sealed::ByteSliceSealed for RefMut<'a, [u8]> {}
2787-
// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2793+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
27882794
#[allow(clippy::undocumented_unsafe_blocks)]
27892795
unsafe impl<'a> ByteSlice for RefMut<'a, [u8]> {
27902796
#[inline]
@@ -2793,11 +2799,11 @@ unsafe impl<'a> ByteSlice for RefMut<'a, [u8]> {
27932799
}
27942800
}
27952801

2796-
// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2802+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
27972803
#[allow(clippy::undocumented_unsafe_blocks)]
27982804
unsafe impl<'a> ByteSliceMut for &'a mut [u8] {}
27992805

2800-
// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2806+
// TODO(#429): Add a "SAFETY" comment and remove this `allow`.
28012807
#[allow(clippy::undocumented_unsafe_blocks)]
28022808
unsafe impl<'a> ByteSliceMut for RefMut<'a, [u8]> {}
28032809

0 commit comments

Comments
 (0)