pub struct Binary<'id> { /* private fields */ }Expand description
A byte-aligned binary (enif_is_binary returned true).
Data is on the BEAM heap. Nothing is copied until as_bytes/from_bytes is
called. The byte accessors take an impl Env<'id> (this brand) because the
term carries only its brand, not its env.
Implementations§
Source§impl<'id> Binary<'id>
impl<'id> Binary<'id>
pub fn from_raw(raw_term: Term) -> Self
raw only.Sourcepub fn as_bytes(self, env: impl Env<'id>) -> &'id [u8] ⓘ
pub fn as_bytes(self, env: impl Env<'id>) -> &'id [u8] ⓘ
View the binary data as a byte slice (enif_inspect_binary).
Zero-copy — the slice points into the BEAM heap and rides this binary’s
brand 'id, which cannot escape its env’s scope.
Sourcepub fn is_empty(self, env: impl Env<'id>) -> bool
pub fn is_empty(self, env: impl Env<'id>) -> bool
Returns true if the binary contains no bytes.
Sourcepub fn try_str(self, env: impl Env<'id>) -> Result<&'id str, Utf8Error>
pub fn try_str(self, env: impl Env<'id>) -> Result<&'id str, Utf8Error>
Interpret the binary as a UTF-8 string (zero-copy). Err if the bytes
are not valid UTF-8.
Sourcepub fn sub(self, env: impl Env<'id>, pos: usize, len: usize) -> Binary<'id>
pub fn sub(self, env: impl Env<'id>, pos: usize, len: usize) -> Binary<'id>
Create a zero-copy sub-binary term spanning pos..pos+len
(enif_make_sub_binary). Panics if out of bounds.
Sourcepub fn from_bytes(env: impl Env<'id>, data: &[u8]) -> Binary<'id>
pub fn from_bytes(env: impl Env<'id>, data: &[u8]) -> Binary<'id>
Allocate a new binary on the BEAM heap and copy data into it
(enif_make_new_binary).
Trait Implementations§
impl<'id> Copy for Binary<'id>
Source§impl<'id> Encoder<'id> for Binary<'id>
Encodes for free by rewrapping the term’s own machine word — an otter
term is already an Erlang term. Never fails.
impl<'id> Encoder<'id> for Binary<'id>
Encodes for free by rewrapping the term’s own machine word — an otter term is already an Erlang term. Never fails.
impl Eq for Binary<'_>
Source§impl Ord for Binary<'_>
impl Ord for Binary<'_>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Binary<'_>
impl PartialOrd for Binary<'_>
Source§impl<'id> Term<'id> for Binary<'id>
impl<'id> Term<'id> for Binary<'id>
Source§fn copy_to<'dst>(self, env: impl Env<'dst>) -> AnyTerm<'dst>where
Self: Sized,
fn copy_to<'dst>(self, env: impl Env<'dst>) -> AnyTerm<'dst>where
Self: Sized,
enif_make_copy), producing a
term branded to the destination. The general cross-env copy — distinct
from same-brand Encoder (which wraps for free)
and from OwnedEnvArena copy_out (the arena exit).