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

Don't use transmute! #2

Closed
steveklabnik opened this issue Mar 8, 2017 · 1 comment
Closed

Don't use transmute! #2

steveklabnik opened this issue Mar 8, 2017 · 1 comment

Comments

@steveklabnik
Copy link

steveklabnik commented Mar 8, 2017

Hey there! I wanted to send you a PR, but couldn't find the text of the gitbook, so I'll just open an issue.

in https://davidmcneil.gitbooks.io/the-rusty-web/content/counter-example.html

this code is better written as

#[no_mangle]
unsafe pub extern "C" fn counter_destroy(counter: *mut Counter) {
    Box::from_raw(counter)
}

#[no_mangle]
pub extern "C" fn counter_create() -> *mut Counter {
    Box::into_raw(Box::new(Counter::new()))
}

That is, from_raw/into_raw over transmute, and the unsafety only belongs on counter_destroy; if you did a null check inside of it, you could make it safe too.

Very interesting guide, thanks for sharing it!

@davidMcneil
Copy link
Owner

Thank you for the feedback! That is a much better method.

And thank you for all you do in the Rust community. The books specifically are amazing!

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

No branches or pull requests

2 participants