From bacc6ef48f342058848d3ce525bee04357e84c39 Mon Sep 17 00:00:00 2001 From: blackanger Date: Sun, 26 Jun 2022 13:34:47 +0800 Subject: [PATCH 1/2] Improve documentation for Box::into_inner Signed-off-by: blackanger --- library/alloc/src/boxed.rs | 43 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index cc395759b2078..5602afca94f61 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -614,9 +614,48 @@ impl Box { /// ``` /// #![feature(box_into_inner)] /// - /// let c = Box::new(5); + /// // if you have a type `Point` which is `Copy` + /// // and you have a `Box` value, + /// // and you want to take the `Point` value out, + /// // and drop the box memory. + /// + /// #[derive(Copy, Clone, Debug)] + /// struct Point { + /// x: i32, + /// y: i32, + /// } /// - /// assert_eq!(Box::into_inner(c), 5); + /// let boxed_point = Box::new(Point { x: 0, y: 0}); + /// + /// // this moves the wrapped value, and the memory will be deallocated. + /// let mut point = Box::into_inner(boxed_point); + /// + /// point.x = 1; + /// point.y = 2; + /// + /// assert_eq!(1, point.x); + /// assert_eq!(2, point.y); + /// + /// // error[E0382]: borrow of moved value: `boxed_point` + /// // println!("{:?}", boxed_point); + /// + /// // Note the difference between `into_inner` and `*`. + /// // The language actually supports `*x` as a special case. + /// // However it can't be generalized to other types easily. + /// // It also doesn't consume the `Box` value immediately if `T` is `Copy`, which is quite a footgun. + /// + /// let boxed_point = Box::new(Point { x: 0, y: 0}); + /// + /// // this copies the wrapped value out, but the memory is not deallocated. + /// let mut point = *boxed_point; + /// + /// point.x = 1; + /// point.y = 2; + /// + /// assert_eq!(1, point.x); + /// assert_eq!(2, point.y); + /// + /// println!("{:?}", boxed_point); // it's still alive now, needs another `drop` to deallocate. /// ``` #[unstable(feature = "box_into_inner", issue = "80437")] #[rustc_const_unstable(feature = "const_box", issue = "92521")] From 2e1ef2fe9afb5678ae915e6001de140be515b4cb Mon Sep 17 00:00:00 2001 From: blackanger Date: Sun, 26 Jun 2022 15:34:22 +0800 Subject: [PATCH 2/2] fix tidy check errors Signed-off-by: blackanger --- library/alloc/src/boxed.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 5602afca94f61..5ff76de03f535 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -614,11 +614,11 @@ impl Box { /// ``` /// #![feature(box_into_inner)] /// - /// // if you have a type `Point` which is `Copy` + /// // if you have a type `Point` which is `Copy` /// // and you have a `Box` value, - /// // and you want to take the `Point` value out, + /// // and you want to take the `Point` value out, /// // and drop the box memory. - /// + /// /// #[derive(Copy, Clone, Debug)] /// struct Point { /// x: i32, @@ -626,10 +626,10 @@ impl Box { /// } /// /// let boxed_point = Box::new(Point { x: 0, y: 0}); - /// + /// /// // this moves the wrapped value, and the memory will be deallocated. /// let mut point = Box::into_inner(boxed_point); - /// + /// /// point.x = 1; /// point.y = 2; /// @@ -639,22 +639,22 @@ impl Box { /// // error[E0382]: borrow of moved value: `boxed_point` /// // println!("{:?}", boxed_point); /// - /// // Note the difference between `into_inner` and `*`. - /// // The language actually supports `*x` as a special case. - /// // However it can't be generalized to other types easily. + /// // Note the difference between `into_inner` and `*`. + /// // The language actually supports `*x` as a special case. + /// // However it can't be generalized to other types easily. /// // It also doesn't consume the `Box` value immediately if `T` is `Copy`, which is quite a footgun. /// /// let boxed_point = Box::new(Point { x: 0, y: 0}); - /// + /// /// // this copies the wrapped value out, but the memory is not deallocated. /// let mut point = *boxed_point; - /// + /// /// point.x = 1; /// point.y = 2; - /// + /// /// assert_eq!(1, point.x); /// assert_eq!(2, point.y); - /// + /// /// println!("{:?}", boxed_point); // it's still alive now, needs another `drop` to deallocate. /// ``` #[unstable(feature = "box_into_inner", issue = "80437")]