Skip to content
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

Merged
merged 1 commit into from
Jul 12, 2016
Merged

Improve boxed docs #34740

merged 1 commit into from
Jul 12, 2016

Conversation

GuillaumeGomez
Copy link
Member

Fixes #29343.

r? @steveklabnik

/// ```
/// let mut x = 5;
/// let ptr = &mut x as *mut i32;
/// let b = unsafe { Box::from_raw(ptr) };
Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Contributor

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_
.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@GuillaumeGomez
Copy link
Member Author

Updated.

/// use std::any::Any;
///
/// fn print_if_string(value: Box<Any + Send>) {
/// match value.downcast::<String>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if let?

@GuillaumeGomez
Copy link
Member Author

@frewsxcv: Thanks!

@steveklabnik
Copy link
Member

@bors: r+ rollup

@bors
Copy link
Contributor

bors commented Jul 11, 2016

📌 Commit 1ef7bdc has been approved by steveklabnik

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jul 12, 2016
bors added a commit that referenced this pull request Jul 12, 2016
Rollup of 7 pull requests

- Successful merges: #34736, #34737, #34740, #34742, #34749, #34750, #34770
- Failed merges: #33951
@bors bors merged commit 1ef7bdc into rust-lang:master Jul 12, 2016
@GuillaumeGomez GuillaumeGomez deleted the boxed_doc branch July 12, 2016 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants