From c351107cdc378dec4f855c29a3566fbd1d2ac358 Mon Sep 17 00:00:00 2001 From: rodrimati1992 Date: Sun, 31 Jan 2021 01:21:06 -0300 Subject: [PATCH 1/4] Document how `MaybeUninit` can be initialized. --- library/core/src/mem/maybe_uninit.rs | 39 +++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index 05bcd90d3ca76..c85b5655bdb0c 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -172,10 +172,41 @@ use crate::ptr; /// /// ## Initializing a struct field-by-field /// -/// There is currently no supported way to create a raw pointer or reference -/// to a field of a struct inside `MaybeUninit`. That means it is not possible -/// to create a struct by calling `MaybeUninit::uninit::()` and then writing -/// to its fields. +/// You can use `MaybeUninit`, and the [`std::ptr::addr_of_mut`] macro, to initialize structs field by field: +/// +/// ```rust +/// use std::mem::MaybeUninit; +/// use std::ptr::addr_of_mut; +/// +/// #[derive(Debug, PartialEq)] +/// pub struct Foo { +/// name: String, +/// list: Vec, +/// } +/// +/// let foo = { +/// let mut uninit: MaybeUninit = MaybeUninit::uninit(); +/// let ptr = uninit.as_mut_ptr(); +/// +/// // Initializing the `name` field +/// unsafe { addr_of_mut!((*ptr).name).write("Bob".to_string()); } +/// +/// // Initializing the `list` field +/// // If there was a panic here, then the `String` in the `name` field would be leaked. +/// unsafe { addr_of_mut!((*ptr).list).write(vec![0, 1, 2]); } +/// +/// // All the fields are initialized, so we call `assume_init` to get an initialized Foo. +/// unsafe { uninit.assume_init() } +/// }; +/// +/// assert_eq!( +/// foo, +/// Foo { +/// name: "Bob".to_string(), +/// list: vec![0, 1, 2] +/// } +/// ); +/// ``` /// /// [ub]: ../../reference/behavior-considered-undefined.html /// From 0974026a5de5150cbffa46c1cc8f7d3256b58e30 Mon Sep 17 00:00:00 2001 From: rodrimati1992 Date: Sun, 31 Jan 2021 01:37:48 -0300 Subject: [PATCH 2/4] Removed trailing whitespace --- library/core/src/mem/maybe_uninit.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index c85b5655bdb0c..3be730a35cedc 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -173,32 +173,32 @@ use crate::ptr; /// ## Initializing a struct field-by-field /// /// You can use `MaybeUninit`, and the [`std::ptr::addr_of_mut`] macro, to initialize structs field by field: -/// +/// /// ```rust /// use std::mem::MaybeUninit; /// use std::ptr::addr_of_mut; -/// +/// /// #[derive(Debug, PartialEq)] /// pub struct Foo { /// name: String, /// list: Vec, /// } -/// +/// /// let foo = { /// let mut uninit: MaybeUninit = MaybeUninit::uninit(); /// let ptr = uninit.as_mut_ptr(); -/// +/// /// // Initializing the `name` field /// unsafe { addr_of_mut!((*ptr).name).write("Bob".to_string()); } -/// +/// /// // Initializing the `list` field /// // If there was a panic here, then the `String` in the `name` field would be leaked. /// unsafe { addr_of_mut!((*ptr).list).write(vec![0, 1, 2]); } -/// +/// /// // All the fields are initialized, so we call `assume_init` to get an initialized Foo. /// unsafe { uninit.assume_init() } /// }; -/// +/// /// assert_eq!( /// foo, /// Foo { From aa83e2aa0489c56c2256a0853ebfab442f57061e Mon Sep 17 00:00:00 2001 From: rodrimati1992 Date: Sun, 31 Jan 2021 01:56:53 -0300 Subject: [PATCH 3/4] Update maybe_uninit.rs --- library/core/src/mem/maybe_uninit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index 3be730a35cedc..9452eb83da012 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -207,7 +207,7 @@ use crate::ptr; /// } /// ); /// ``` -/// +/// [`std::ptr::addr_of_mut`]: crate::ptr::addr_of_mut /// [ub]: ../../reference/behavior-considered-undefined.html /// /// # Layout From 21c2343d3f95348b10b3b6ff73c3c237eea89c32 Mon Sep 17 00:00:00 2001 From: rodrimati1992 Date: Sun, 31 Jan 2021 14:20:04 -0300 Subject: [PATCH 4/4] Update comment about leaking --- library/core/src/mem/maybe_uninit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index 9452eb83da012..3760f5c479481 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -192,7 +192,7 @@ use crate::ptr; /// unsafe { addr_of_mut!((*ptr).name).write("Bob".to_string()); } /// /// // Initializing the `list` field -/// // If there was a panic here, then the `String` in the `name` field would be leaked. +/// // If there is a panic here, then the `String` in the `name` field leaks. /// unsafe { addr_of_mut!((*ptr).list).write(vec![0, 1, 2]); } /// /// // All the fields are initialized, so we call `assume_init` to get an initialized Foo.