Skip to content

Commit 0ec87d0

Browse files
committed
rearrange for clarity
1 parent 2713d36 commit 0ec87d0

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed

src/libcore/ptr.rs

+40-41
Original file line numberDiff line numberDiff line change
@@ -472,47 +472,6 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
472472
///
473473
/// Note that even if `T` has size `0`, the pointer must be non-NULL and properly aligned.
474474
///
475-
/// ## Ownership of the Returned Value
476-
///
477-
/// `read` creates a bitwise copy of `T`, regardless of whether `T` is [`Copy`].
478-
/// If `T` is not [`Copy`], using both the returned value and the value at
479-
/// `*src` can violate memory safety. Note that assigning to `src` counts as a
480-
/// use because it will attempt to drop the value at `*src`.
481-
///
482-
/// [`write`] can be used to overwrite data without causing it to be dropped.
483-
///
484-
/// [valid]: ../ptr/index.html#safety
485-
/// [`Copy`]: ../marker/trait.Copy.html
486-
/// [`read_unaligned`]: ./fn.read_unaligned.html
487-
/// [`write`]: ./fn.write.html
488-
///
489-
/// ```
490-
/// use std::ptr;
491-
///
492-
/// let mut s = String::from("foo");
493-
/// unsafe {
494-
/// // `s2` now points to the same underlying memory as `s`.
495-
/// let mut s2: String = ptr::read(&s);
496-
///
497-
/// assert_eq!(s2, "foo");
498-
///
499-
/// // Assigning to `s2` causes its original value to be dropped. Beyond
500-
/// // this point, `s` must no longer be used, as the underlying memory has
501-
/// // been freed.
502-
/// s2 = String::default();
503-
/// assert_eq!(s2, "");
504-
///
505-
/// // Assigning to `s` would cause the old value to be dropped again,
506-
/// // resulting in undefined behavior.
507-
/// // s = String::from("bar"); // ERROR
508-
///
509-
/// // `ptr::write` can be used to overwrite a value without dropping it.
510-
/// ptr::write(&mut s, String::from("bar"));
511-
/// }
512-
///
513-
/// assert_eq!(s, "bar");
514-
/// ```
515-
///
516475
/// # Examples
517476
///
518477
/// Basic usage:
@@ -565,7 +524,47 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
565524
/// assert_eq!(bar, "foo");
566525
/// ```
567526
///
527+
/// ## Ownership of the Returned Value
528+
///
529+
/// `read` creates a bitwise copy of `T`, regardless of whether `T` is [`Copy`].
530+
/// If `T` is not [`Copy`], using both the returned value and the value at
531+
/// `*src` can violate memory safety. Note that assigning to `*src` counts as a
532+
/// use because it will attempt to drop the value at `*src`.
533+
///
534+
/// [`write`] can be used to overwrite data without causing it to be dropped.
535+
///
536+
/// ```
537+
/// use std::ptr;
538+
///
539+
/// let mut s = String::from("foo");
540+
/// unsafe {
541+
/// // `s2` now points to the same underlying memory as `s`.
542+
/// let mut s2: String = ptr::read(&s);
543+
///
544+
/// assert_eq!(s2, "foo");
545+
///
546+
/// // Assigning to `s2` causes its original value to be dropped. Beyond
547+
/// // this point, `s` must no longer be used, as the underlying memory has
548+
/// // been freed.
549+
/// s2 = String::default();
550+
/// assert_eq!(s2, "");
551+
///
552+
/// // Assigning to `s` would cause the old value to be dropped again,
553+
/// // resulting in undefined behavior.
554+
/// // s = String::from("bar"); // ERROR
555+
///
556+
/// // `ptr::write` can be used to overwrite a value without dropping it.
557+
/// ptr::write(&mut s, String::from("bar"));
558+
/// }
559+
///
560+
/// assert_eq!(s, "bar");
561+
/// ```
562+
///
568563
/// [`mem::swap`]: ../mem/fn.swap.html
564+
/// [valid]: ../ptr/index.html#safety
565+
/// [`Copy`]: ../marker/trait.Copy.html
566+
/// [`read_unaligned`]: ./fn.read_unaligned.html
567+
/// [`write`]: ./fn.write.html
569568
#[inline]
570569
#[stable(feature = "rust1", since = "1.0.0")]
571570
pub unsafe fn read<T>(src: *const T) -> T {

0 commit comments

Comments
 (0)