-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace mem::uninitialized by MaybeUninit (#239)
* Replace mem::uninitialized by MaybeUninit * Run cargo fmt * Fix clippy warnings
- Loading branch information
Showing
15 changed files
with
84 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.