-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve boxed docs #34740
Improve boxed docs #34740
Conversation
/// ``` | ||
/// let mut x = 5; | ||
/// let ptr = &mut x as *mut i32; | ||
/// let b = unsafe { Box::from_raw(ptr) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better if this example didn't demonstrate code that is likely to cause a segfault. The typical use case for this is when you start off with a Box
, convert it to a raw pointer with into_raw
, and then need to restore it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Just by curiosity: why would it likely cause a segfault in here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Box ends up being a pointer to the stack, and is then freed when the
Box is dropped.
On Saturday, July 9, 2016, Guillaume Gomez [email protected] wrote:
In src/liballoc/boxed.rs
#34740 (comment):@@ -249,6 +249,14 @@ impl<T: ?Sized> Box {
/// This function is unsafe because improper use may lead to
/// memory problems. For example, a double-free may occur if the
/// function is called twice on the same raw pointer.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut x = 5;
- /// let ptr = &mut x as *mut i32;
- /// let b = unsafe { Box::from_raw(ptr) };
I see. Just by curiosity: why would it likely cause a segfault in here?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/rust-lang/rust/pull/34740/files/297695d76da3a624e380ad9a190b8d3354605ef5#r70173240,
or mute the thread
https://github.com/notifications/unsubscribe/AIPosBuNJtBUz6ID429xaGrWDWV8j17rks5qUCdggaJpZM4JIqu_
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
297695d
to
3e4db3c
Compare
Updated. |
/// use std::any::Any; | ||
/// | ||
/// fn print_if_string(value: Box<Any + Send>) { | ||
/// match value.downcast::<String>() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if let
?
3e4db3c
to
876d3ec
Compare
@frewsxcv: Thanks! |
876d3ec
to
1ef7bdc
Compare
@bors: r+ rollup |
📌 Commit 1ef7bdc has been approved by |
…abnik Improve boxed docs Fixes rust-lang#29343. r? @steveklabnik
Fixes #29343.
r? @steveklabnik