Skip to content

Commit 40ca167

Browse files
committed
Improve #Safety in various methods in core::ptr
For all methods which read a value of type T, `read`, `read_unaligned`, `read_volatile` and `replace`, added missing constraint: The value they point to must be properly initialized
1 parent 351782d commit 40ca167

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/libcore/ptr/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
475475
///
476476
/// * `dst` must be properly aligned.
477477
///
478+
/// * `dst` must point to a properly initialized value of type `T`.
479+
///
478480
/// Note that even if `T` has size `0`, the pointer must be non-NULL and properly aligned.
479481
///
480482
/// [valid]: ../ptr/index.html#safety
@@ -514,6 +516,8 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
514516
/// * `src` must be properly aligned. Use [`read_unaligned`] if this is not the
515517
/// case.
516518
///
519+
/// * `src` must point to a properly initialized value of type `T`.
520+
///
517521
/// Note that even if `T` has size `0`, the pointer must be non-NULL and properly aligned.
518522
///
519523
/// # Examples
@@ -628,6 +632,8 @@ pub unsafe fn read<T>(src: *const T) -> T {
628632
///
629633
/// * `src` must be [valid] for reads.
630634
///
635+
/// * `src` must point to a properly initialized value of type `T`.
636+
///
631637
/// Like [`read`], `read_unaligned` creates a bitwise copy of `T`, regardless of
632638
/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the returned
633639
/// value and the value at `*src` can [violate memory safety][read-ownership].
@@ -922,6 +928,8 @@ pub unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
922928
///
923929
/// * `src` must be properly aligned.
924930
///
931+
/// * `src` must point to a properly initialized value of type `T`.
932+
///
925933
/// Like [`read`], `read_volatile` creates a bitwise copy of `T`, regardless of
926934
/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the returned
927935
/// value and the value at `*src` can [violate memory safety][read-ownership].

0 commit comments

Comments
 (0)