use crate::{
wrapper::binary::{alloc, new_binary, realloc, ErlNifBinary},
Decoder, Encoder, Env, Error, NifResult, Term,
};
use std::{
borrow::{Borrow, BorrowMut},
hash::{Hash, Hasher},
io::Write,
mem::MaybeUninit,
ops::{Deref, DerefMut},
};
pub struct OwnedBinary(ErlNifBinary);
impl OwnedBinary {
pub unsafe fn from_raw(inner: ErlNifBinary) -> OwnedBinary {
OwnedBinary(inner)
}
pub fn new(size: usize) -> Option<OwnedBinary> {
unsafe { alloc(size) }.map(OwnedBinary)
}
pub fn from_unowned(src: &Binary) -> Option<OwnedBinary> {
OwnedBinary::new(src.len()).map(|mut b| {
b.as_mut_slice().copy_from_slice(src);
b
})
}
#[must_use]
pub fn realloc(&mut self, size: usize) -> bool {
unsafe { realloc(&mut self.0, size) }
}
pub fn realloc_or_copy(&mut self, size: usize) {
if !self.realloc(size) {
let mut new = OwnedBinary::new(size).unwrap();
if let Ok(num_written) = new.as_mut_slice().write(self.as_slice()) {
if !(num_written == self.len() || num_written == new.len()) {
panic!("Could not copy binary");
}
::std::mem::swap(&mut self.0, &mut new.0);
} else {
panic!("Could not copy binary");
}
}
}
pub fn as_slice(&self) -> &[u8] {
unsafe { ::std::slice::from_raw_parts(self.0.data, self.0.size) }
}
pub fn as_mut_slice(&mut self) -> &mut [u8] {
unsafe { ::std::slice::from_raw_parts_mut(self.0.data, self.0.size) }
}
pub fn release(self, env: Env) -> Binary {
Binary::from_owned(self, env)
}
}
impl Borrow<[u8]> for OwnedBinary {
fn borrow(&self) -> &[u8] {
self.as_slice()
}
}
impl BorrowMut<[u8]> for OwnedBinary {
fn borrow_mut(&mut self) -> &mut [u8] {
self.as_mut_slice()
}
}
impl Deref for OwnedBinary {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.as_slice()
}
}
impl DerefMut for OwnedBinary {
fn deref_mut(&mut self) -> &mut [u8] {
self.as_mut_slice()
}
}
impl Hash for OwnedBinary {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_slice().hash(state);
}
}
impl PartialEq for OwnedBinary {
fn eq(&self, other: &Self) -> bool {
self.as_slice() == other.as_slice()
}
}
impl Eq for OwnedBinary {}
impl PartialEq<Binary<'_>> for OwnedBinary {
fn eq(&self, other: &Binary) -> bool {
self.as_slice() == other.as_slice()
}
}
impl Drop for OwnedBinary {
fn drop(&mut self) {
unsafe { rustler_sys::enif_release_binary(&mut self.0) };
}
}
unsafe impl Send for OwnedBinary {}
unsafe impl Sync for OwnedBinary {}
#[derive(Copy, Clone)]
pub struct Binary<'a> {
buf: *const u8,
size: usize,
term: Term<'a>,
}
impl<'a> Binary<'a> {
pub fn from_owned(owned: OwnedBinary, env: Env<'a>) -> Self {
let mut owned = std::mem::ManuallyDrop::new(owned);
let term = unsafe {
Term::new(
env,
rustler_sys::enif_make_binary(env.as_c_arg(), &mut owned.0),
)
};
Binary {
buf: owned.0.data,
size: owned.0.size,
term,
}
}
#[allow(clippy::wrong_self_convention)]
pub fn to_owned(&self) -> Option<OwnedBinary> {
OwnedBinary::from_unowned(self)
}
pub fn from_term(term: Term<'a>) -> Result<Self, Error> {
let mut binary = MaybeUninit::uninit();
if unsafe {
rustler_sys::enif_inspect_binary(
term.get_env().as_c_arg(),
term.as_c_arg(),
binary.as_mut_ptr(),
)
} == 0
{
return Err(Error::BadArg);
}
let binary = unsafe { binary.assume_init() };
Ok(Binary {
buf: binary.data,
size: binary.size,
term,
})
}
pub(crate) unsafe fn from_term_and_slice(term: Term<'a>, binary: &[u8]) -> Self {
Binary {
term,
buf: binary.as_ptr(),
size: binary.len(),
}
}
pub fn from_iolist(term: Term<'a>) -> Result<Self, Error> {
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_mut_ptr(),
)
} == 0
{
return Err(Error::BadArg);
}
let binary = unsafe { binary.assume_init() };
Ok(Binary {
buf: binary.data,
size: binary.size,
term,
})
}
#[allow(clippy::wrong_self_convention)]
pub fn to_term<'b>(&self, env: Env<'b>) -> Term<'b> {
self.term.in_env(env)
}
pub fn as_slice(&self) -> &'a [u8] {
unsafe { ::std::slice::from_raw_parts(self.buf, self.size) }
}
pub fn make_subbinary(&self, offset: usize, length: usize) -> NifResult<Binary<'a>> {
let min_len = length.checked_add(offset);
if min_len.ok_or(Error::BadArg)? > self.size {
return Err(Error::BadArg);
}
Ok(unsafe { self.make_subbinary_unchecked(offset, length) })
}
#[allow(unused_unsafe)]
pub unsafe fn make_subbinary_unchecked(&self, offset: usize, length: usize) -> Binary<'a> {
let raw_term = unsafe {
rustler_sys::enif_make_sub_binary(
self.term.get_env().as_c_arg(),
self.term.as_c_arg(),
offset,
length,
)
};
let term = unsafe { Term::new(self.term.get_env(), raw_term) };
Binary {
buf: unsafe { self.buf.add(offset) },
size: length,
term,
}
}
}
impl<'a> Borrow<[u8]> for Binary<'a> {
fn borrow(&self) -> &[u8] {
self.as_slice()
}
}
impl<'a> Deref for Binary<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.as_slice()
}
}
impl<'a> Decoder<'a> for Binary<'a> {
fn decode(term: Term<'a>) -> Result<Self, Error> {
Binary::from_term(term)
}
}
impl<'a> Encoder for Binary<'a> {
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
self.to_term(env)
}
}
impl Hash for Binary<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_slice().hash(state);
}
}
impl PartialEq for Binary<'_> {
fn eq(&self, other: &Self) -> bool {
self.as_slice() == other.as_slice()
}
}
impl Eq for Binary<'_> {}
impl PartialEq<OwnedBinary> for Binary<'_> {
fn eq(&self, other: &OwnedBinary) -> bool {
self.as_slice() == other.as_slice()
}
}
impl<'a> Term<'a> {
pub fn into_binary(self) -> NifResult<Binary<'a>> {
Binary::from_term(self)
}
}
pub struct NewBinary<'a> {
buf: *mut u8,
size: usize,
term: Term<'a>,
}
impl<'a> NewBinary<'a> {
pub fn new(env: Env<'a>, size: usize) -> Self {
let (buf, term) = unsafe { new_binary(env, size) };
NewBinary { buf, term, size }
}
pub fn as_slice(&self) -> &[u8] {
unsafe { ::std::slice::from_raw_parts(self.buf, self.size) }
}
pub fn as_mut_slice(&mut self) -> &mut [u8] {
unsafe { ::std::slice::from_raw_parts_mut(self.buf, self.size) }
}
}
impl<'a> From<NewBinary<'a>> for Binary<'a> {
fn from(new_binary: NewBinary<'a>) -> Self {
Binary::from_term(new_binary.term).unwrap()
}
}
impl<'a> From<NewBinary<'a>> for Term<'a> {
fn from(new_binary: NewBinary<'a>) -> Self {
new_binary.term
}
}
impl<'a> Deref for NewBinary<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.as_slice()
}
}
impl<'a> DerefMut for NewBinary<'a> {
fn deref_mut(&mut self) -> &mut [u8] {
self.as_mut_slice()
}
}