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

small fix to the tutorial-ffi destructor example #6244

Merged
merged 1 commit into from
May 5, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions doc/tutorial-ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,7 @@ wrapping `malloc` and `free`:

~~~~
use core::libc::{c_void, size_t, malloc, free};

#[abi = "rust-intrinsic"]
extern "rust-intrinsic" mod rusti {
fn init<T>() -> T;
}
use core::unstable::intrinsics;

// a wrapper around the handle returned by the foreign code
pub struct Unique<T> {
Expand All @@ -166,7 +162,8 @@ pub impl<'self, T: Owned> Unique<T> {
unsafe {
let ptr = malloc(core::sys::size_of::<T>() as size_t) as *mut T;
assert!(!ptr::is_null(ptr));
*ptr = value;
// `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it
intrinsics::move_val_init(&mut *ptr, value);
Unique{ptr: ptr}
}
}
Expand All @@ -186,7 +183,7 @@ pub impl<'self, T: Owned> Unique<T> {
impl<T: Owned> Drop for Unique<T> {
fn finalize(&self) {
unsafe {
let mut x = rusti::init(); // dummy value to swap in
let mut x = intrinsics::init(); // dummy value to swap in
x <-> *self.ptr; // moving the object out is needed to call the destructor
free(self.ptr as *c_void)
}
Expand Down