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

Replace mem::uninitialized by MaybeUninit #239

Merged
merged 3 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
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
10 changes: 2 additions & 8 deletions rustler/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,8 @@ pub fn open_struct_resource_type<'a, T: ResourceTypeProvider>(
flags,
)
};
if let Some(res) = res {
Some(ResourceType {
res,
struct_type: PhantomData,
})
} else {
None
}

res.map(|r| ResourceType { res: r, struct_type: PhantomData})
}

fn get_alloc_size_struct<T>() -> usize {
Expand Down
4 changes: 2 additions & 2 deletions rustler/src/thread.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::env::OwnedEnv;
use std::panic;
use std::thread;
use crate::types::atom::Atom;
use crate::{Encoder, Env, Term};
use std::panic;
use std::thread;

/// A `JobSpawner` is a value that can run Rust code on non-Erlang system threads.
/// Abstracts away details of thread management for `spawn()`.
Expand Down
18 changes: 9 additions & 9 deletions rustler/src/types/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{Decoder, Encoder, Env, Error, NifResult, Term};

use std::borrow::{Borrow, BorrowMut};
use std::io::Write;
use std::mem::MaybeUninit;
use std::ops::{Deref, DerefMut};

// Owned
Expand Down Expand Up @@ -114,7 +115,7 @@ impl DerefMut for OwnedBinary {
impl Drop for OwnedBinary {
fn drop(&mut self) {
if self.release {
unsafe { rustler_sys::enif_release_binary(self.inner.as_c_arg()) };
unsafe { rustler_sys::enif_release_binary(&mut self.inner) };
}
}
}
Expand All @@ -123,7 +124,6 @@ unsafe impl Send for OwnedBinary {}

// Borrowed

#[derive(Copy, Clone)]
pub struct Binary<'a> {
inner: ErlNifBinary,
term: Term<'a>,
Expand All @@ -137,7 +137,7 @@ impl<'a> Binary<'a> {
let term = unsafe {
Term::new(
env,
rustler_sys::enif_make_binary(env.as_c_arg(), bin.inner.as_c_arg()),
rustler_sys::enif_make_binary(env.as_c_arg(), &mut bin.inner),
)
};
Binary {
Expand All @@ -151,37 +151,37 @@ impl<'a> Binary<'a> {
}

pub fn from_term(term: Term<'a>) -> Result<Self, Error> {
let mut binary = unsafe { ErlNifBinary::new_empty() };
let mut binary = MaybeUninit::uninit();
if unsafe {
rustler_sys::enif_inspect_binary(
term.get_env().as_c_arg(),
term.as_c_arg(),
binary.as_c_arg(),
binary.as_mut_ptr(),
)
} == 0
{
return Err(Error::BadArg);
}
Ok(Binary {
inner: binary,
inner: unsafe { binary.assume_init() },
term,
})
}

pub fn from_iolist(term: Term<'a>) -> Result<Self, Error> {
let mut binary = unsafe { ErlNifBinary::new_empty() };
let mut binary = MaybeUninit::uninit();
if unsafe {
rustler_sys::enif_inspect_iolist_as_binary(
term.get_env().as_c_arg(),
term.as_c_arg(),
binary.as_c_arg(),
binary.as_mut_ptr(),
)
} == 0
{
return Err(Error::BadArg);
}
Ok(Binary {
inner: binary,
inner: unsafe { binary.assume_init() },
term,
})
}
Expand Down
10 changes: 6 additions & 4 deletions rustler/src/types/pid.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::wrapper::{pid, ErlNifPid};
use crate::{Decoder, Encoder, Env, Error, NifResult, Term};
use std::mem;
use std::mem::MaybeUninit;

#[derive(Clone)]
pub struct Pid {
Expand Down Expand Up @@ -36,10 +36,12 @@ impl<'a> Env<'a> {
/// environment is to use `OwnedEnv`. The `Env` that Rustler passes to NIFs when they're
/// called is always associated with the calling Erlang process.)
pub fn pid(self) -> Pid {
let mut pid: ErlNifPid = unsafe { mem::uninitialized() };
if unsafe { rustler_sys::enif_self(self.as_c_arg(), &mut pid) }.is_null() {
let mut pid = MaybeUninit::uninit();
if unsafe { rustler_sys::enif_self(self.as_c_arg(), pid.as_mut_ptr()) }.is_null() {
panic!("environment is process-independent");
}
Pid { c: pid }
Pid {
c: unsafe { pid.assume_init() },
}
}
}
1 change: 0 additions & 1 deletion rustler/src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub type size_t = usize;

pub type NIF_ENV = *mut rustler_sys::ErlNifEnv;
pub type NIF_TERM = size_t;
pub type NIF_BINARY = *mut rustler_sys::ErlNifBinary;
pub type NIF_RESOURCE_TYPE = *const rustler_sys::ErlNifResourceType;

pub fn get_nif_resource_type_init_size() -> usize {
Expand Down
34 changes: 7 additions & 27 deletions rustler/src/wrapper/binary.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@
use crate::wrapper::{c_void, size_t, NIF_BINARY};
use std::mem::uninitialized;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct ErlNifBinary {
pub size: size_t,
pub data: *mut u8,
_internal: [*mut c_void; 3],
}

impl ErlNifBinary {
pub unsafe fn new_empty() -> Self {
ErlNifBinary {
size: uninitialized(),
data: uninitialized(),
_internal: uninitialized(),
}
}
pub fn as_c_arg(&mut self) -> NIF_BINARY {
(self as *mut ErlNifBinary) as NIF_BINARY
}
}
use crate::wrapper::size_t;
pub(in crate) use rustler_sys::ErlNifBinary;
use std::mem::MaybeUninit;

pub unsafe fn alloc(size: size_t) -> Option<ErlNifBinary> {
let mut binary = ErlNifBinary::new_empty();
let success = rustler_sys::enif_alloc_binary(size, binary.as_c_arg());
let mut binary = MaybeUninit::uninit();
let success = rustler_sys::enif_alloc_binary(size, binary.as_mut_ptr());
if success == 0 {
return None;
}
Some(binary)
Some(binary.assume_init())
}

pub unsafe fn realloc(binary: &mut ErlNifBinary, size: size_t) -> bool {
let success = rustler_sys::enif_realloc_binary(binary.as_c_arg(), size);
let success = rustler_sys::enif_realloc_binary(binary, size);
success != 0
}
14 changes: 7 additions & 7 deletions rustler/src/wrapper/env.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use crate::wrapper::binary::ErlNifBinary;
use crate::wrapper::{ERL_NIF_BIN2TERM_SAFE, NIF_ENV, NIF_TERM};
use std::mem;
use std::mem::MaybeUninit;

pub unsafe fn binary_to_term(env: NIF_ENV, data: &[u8], safe: bool) -> Option<(NIF_TERM, usize)> {
let opts = if safe { ERL_NIF_BIN2TERM_SAFE } else { 0 };

let mut result: NIF_TERM = mem::uninitialized();
let mut result = MaybeUninit::uninit();
let read_count =
rustler_sys::enif_binary_to_term(env, data.as_ptr(), data.len(), &mut result, opts);
rustler_sys::enif_binary_to_term(env, data.as_ptr(), data.len(), result.as_mut_ptr(), opts);

if read_count == 0 {
return None;
}

Some((result, read_count))
Some((result.assume_init(), read_count))
}

pub unsafe fn term_to_binary(env: NIF_ENV, term: NIF_TERM) -> Option<ErlNifBinary> {
let mut binary = ErlNifBinary::new_empty();
let success = rustler_sys::enif_term_to_binary(env, term, binary.as_c_arg());
let mut binary = MaybeUninit::uninit();
let success = rustler_sys::enif_term_to_binary(env, term, binary.as_mut_ptr());

if success == 0 {
return None;
}

Some(binary)
Some(binary.assume_init())
}
16 changes: 8 additions & 8 deletions rustler/src/wrapper/list.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::wrapper::{NIF_ENV, NIF_TERM};
use std::mem;
use std::mem::MaybeUninit;

pub unsafe fn get_list_cell(env: NIF_ENV, list: NIF_TERM) -> Option<(NIF_TERM, NIF_TERM)> {
let mut head: NIF_TERM = mem::uninitialized();
let mut tail: NIF_TERM = mem::uninitialized();
let success = rustler_sys::enif_get_list_cell(env, list, &mut head, &mut tail);
let mut head = MaybeUninit::uninit();
let mut tail = MaybeUninit::uninit();
let success = rustler_sys::enif_get_list_cell(env, list, head.as_mut_ptr(), tail.as_mut_ptr());

if success != 1 {
return None;
}
Some((head, tail))
Some((head.assume_init(), tail.assume_init()))
}

pub unsafe fn get_list_length(env: NIF_ENV, list: NIF_TERM) -> Option<usize> {
Expand All @@ -31,11 +31,11 @@ pub unsafe fn make_list_cell(env: NIF_ENV, head: NIF_TERM, tail: NIF_TERM) -> NI
}

pub unsafe fn make_reverse_list(env: NIF_ENV, list: NIF_TERM) -> Option<NIF_TERM> {
let mut list_out: NIF_TERM = mem::uninitialized();
let success = rustler_sys::enif_make_reverse_list(env, list, &mut list_out);
let mut list_out = MaybeUninit::uninit();
let success = rustler_sys::enif_make_reverse_list(env, list, list_out.as_mut_ptr());

if success != 1 {
return None;
}
Some(list_out)
Some(list_out.assume_init())
}
53 changes: 27 additions & 26 deletions rustler/src/wrapper/map.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
pub use crate::wrapper::ErlNifMapIterator;
use crate::wrapper::{ErlNifMapIteratorEntry, NIF_ENV, NIF_TERM};
use std::mem;
use std::mem::MaybeUninit;

pub unsafe fn get_map_value(env: NIF_ENV, map: NIF_TERM, key: NIF_TERM) -> Option<NIF_TERM> {
let mut result: NIF_TERM = mem::uninitialized();
let success = rustler_sys::enif_get_map_value(env, map, key, &mut result);
let mut result = MaybeUninit::uninit();
let success = rustler_sys::enif_get_map_value(env, map, key, result.as_mut_ptr());

if success != 1 {
return None;
}
Some(result)
Some(result.assume_init())
}

pub unsafe fn get_map_size(env: NIF_ENV, map: NIF_TERM) -> Option<usize> {
let mut size: rustler_sys::size_t = mem::uninitialized();
let success = rustler_sys::enif_get_map_size(env, map, &mut size);
let mut size = MaybeUninit::uninit();
let success = rustler_sys::enif_get_map_size(env, map, size.as_mut_ptr());

if success != 1 {
return None;
}
Some(size)
Some(size.assume_init())
}

pub unsafe fn map_new(env: NIF_ENV) -> NIF_TERM {
Expand All @@ -32,23 +32,23 @@ pub unsafe fn map_put(
key: NIF_TERM,
value: NIF_TERM,
) -> Option<NIF_TERM> {
let mut result: NIF_TERM = mem::uninitialized();
let success = rustler_sys::enif_make_map_put(env, map, key, value, &mut result);
let mut result = MaybeUninit::uninit();
let success = rustler_sys::enif_make_map_put(env, map, key, value, result.as_mut_ptr());

if success != 1 {
return None;
}
Some(result)
Some(result.assume_init())
}

pub unsafe fn map_remove(env: NIF_ENV, map: NIF_TERM, key: NIF_TERM) -> Option<NIF_TERM> {
let mut result: NIF_TERM = mem::uninitialized();
let success = rustler_sys::enif_make_map_remove(env, map, key, &mut result);
let mut result = MaybeUninit::uninit();
let success = rustler_sys::enif_make_map_remove(env, map, key, result.as_mut_ptr());

if success != 1 {
return None;
}
Some(result)
Some(result.assume_init())
}

pub unsafe fn map_update(
Expand All @@ -57,27 +57,27 @@ pub unsafe fn map_update(
key: NIF_TERM,
new_value: NIF_TERM,
) -> Option<NIF_TERM> {
let mut result: NIF_TERM = mem::uninitialized();
let success = rustler_sys::enif_make_map_update(env, map, key, new_value, &mut result);
let mut result = MaybeUninit::uninit();
let success = rustler_sys::enif_make_map_update(env, map, key, new_value, result.as_mut_ptr());

if success != 1 {
return None;
}
Some(result)
Some(result.assume_init())
}

pub unsafe fn map_iterator_create(env: NIF_ENV, map: NIF_TERM) -> Option<ErlNifMapIterator> {
let mut iter = mem::uninitialized();
let mut iter = MaybeUninit::uninit();
let success = rustler_sys::enif_map_iterator_create(
env,
map,
&mut iter,
iter.as_mut_ptr(),
ErlNifMapIteratorEntry::ERL_NIF_MAP_ITERATOR_HEAD,
);
if success == 0 {
None
} else {
Some(iter)
Some(iter.assume_init())
}
}

Expand All @@ -89,12 +89,13 @@ pub unsafe fn map_iterator_get_pair(
env: NIF_ENV,
iter: &mut ErlNifMapIterator,
) -> Option<(NIF_TERM, NIF_TERM)> {
let mut key: NIF_TERM = mem::uninitialized();
let mut value: NIF_TERM = mem::uninitialized();
if rustler_sys::enif_map_iterator_get_pair(env, iter, &mut key, &mut value) == 0 {
let mut key = MaybeUninit::uninit();
let mut value = MaybeUninit::uninit();
if rustler_sys::enif_map_iterator_get_pair(env, iter, key.as_mut_ptr(), value.as_mut_ptr()) == 0
{
None
} else {
Some((key, value))
Some((key.assume_init(), value.assume_init()))
}
}

Expand All @@ -108,17 +109,17 @@ pub unsafe fn make_map_from_arrays(
keys: &[NIF_TERM],
values: &[NIF_TERM],
) -> Option<NIF_TERM> {
let mut map = mem::uninitialized();
let mut map = MaybeUninit::uninit();
if rustler_sys::enif_make_map_from_arrays(
env,
keys.as_ptr(),
values.as_ptr(),
keys.len() as usize,
&mut map,
map.as_mut_ptr(),
) == 0
{
return None;
}

Some(map)
Some(map.assume_init())
}
Loading