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

Update Box::from_raw example to generalize better #73678

Merged
merged 1 commit into from
Jul 1, 2020

Commits on Jun 24, 2020

  1. Update Box::from_raw example to generalize better

    I know very little about rust, so I saw this example and tried to generalize it by writing,
    ```
        let layout = Layout::new::<T>();
        let new_obj = unsafe {
            let ptr = alloc(layout) as *mut T;
            *ptr = obj;
            Box::from_raw(ptr)
        };
    ```
    for some more complicated `T`, which ended up crashing with SIGSEGV,
    because it tried to `drop_in_place` the previous object in `ptr` which is
    of course garbage. I also added a comment that explains why `.write`
    is used, but I think adding that comment is optional and may be too verbose
    here. I do however think that changing this example is a good idea to
    suggest the correct generalization. `.write` is also used in most of the rest
    of the documentation here, even if the example is `i32`, so it would additionally
    be more consistent.
    Keno committed Jun 24, 2020
    Configuration menu
    Copy the full SHA
    0c88dd6 View commit details
    Browse the repository at this point in the history