Env

Struct Env 

Source
pub struct Env<'a> { /* private fields */ }
Expand description

On each NIF call, a Env is passed in. The Env is used for most operations that involve communicating with the BEAM, like decoding and encoding terms.

There is no way to allocate a Env at the moment, but this may be possible in the future.

Implementations§

Source§

impl Env<'_>

Source

pub fn pid(self) -> LocalPid

Return the calling process’s pid.

§Panics

Panics if this environment is process-independent. (The only way to get such an environment is to use OwnedEnv. The Env that Rustler passes to NIFs when they’re called is always associated with the calling Erlang process.)

Source

pub fn is_process_alive(self, pid: LocalPid) -> bool

Checks whether the given process is alive

Source§

impl<'a> Env<'a>

Source

pub fn make_ref(self) -> Reference<'a>

Create a new reference in this environment

Source§

impl<'a> Env<'a>

Source

pub fn monitor<T: Resource>( &self, resource: &ResourceArc<T>, pid: &LocalPid, ) -> Option<Monitor>

Source

pub fn demonitor<T: Resource>( &self, resource: &ResourceArc<T>, mon: &Monitor, ) -> bool

Source

pub unsafe fn dynamic_resource_call( self, module: Atom, name: Atom, resource: Term<'a>, call_data: *mut c_void, ) -> Result<(), DynamicResourceCallError>

Source§

impl Env<'_>

Source

pub fn register<T: Resource>(&self) -> Result<(), ResourceInitError>

Register a resource type, see Registration::register.

Source§

impl<'a> Env<'a>

Source

pub unsafe fn new<T>(_lifetime_marker: &'a T, env: NIF_ENV) -> Env<'a>

Create a new Env. For the _lifetime_marker argument, pass a reference to any local variable that has its own lifetime, different from all other Env values. The purpose of the argument is to make it easier to know for sure that the Env you’re creating has a unique lifetime (i.e. that you’re following the most important safety rule of Rustler).

§Unsafe

Don’t create multiple Envs with the same lifetime.

Source

pub fn as_c_arg(self) -> NIF_ENV

Source

pub fn error_tuple(self, reason: impl Encoder) -> Term<'a>

Convenience method for building a tuple {error, Reason}.

Source

pub fn send( self, pid: &LocalPid, message: impl Encoder, ) -> Result<(), SendError>

Send a message to a process.

The Erlang VM imposes some odd restrictions on sending messages. You can send messages in either of these situations:

  • The current thread is managed by the Erlang VM, and self is the environment of the calling process (that is, the environment that Rustler passed in to your NIF); or

  • The current thread is not managed by the Erlang VM.

The result indicates whether the send was successful, see also enif_send.

Source

pub fn whereis_pid(self, name_or_pid: impl Encoder) -> Option<LocalPid>

Attempts to find the PID of a process registered by name_or_pid

Safe wrapper around enif_whereis_pid.

§Returns
  • Some(pid) if name_or_pid is already a PID.
  • Some(pid) if name_or_pid is an atom and an alive process is currently registered under the given name.
  • None if name_or_pid is an atom but there is no alive process registered under this name.
  • None if name_or_pid is not a PID or atom.
Source

pub fn binary_to_term(self, data: &[u8]) -> Option<(Term<'a>, usize)>

Decodes binary data to a term.

Follows the erlang External Term Format.

Source

pub unsafe fn binary_to_term_trusted( self, data: &[u8], ) -> Option<(Term<'a>, usize)>

Like binary_to_term, but can only be called on valid and trusted data.

Trait Implementations§

Source§

impl<'a> Clone for Env<'a>

Source§

fn clone(&self) -> Env<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> From<Env<'a>> for Serializer<'a>

Source§

fn from(env: Env<'a>) -> Serializer<'a>

Converts to this type from the input type.
Source§

impl<'b> PartialEq<Env<'b>> for Env<'_>

Two environments are equal if they’re the same NIF_ENV value.

A Env<'a> is equal to a Env<'b> if and only if 'a and 'b are the same lifetime.

Source§

fn eq(&self, other: &Env<'b>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Copy for Env<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Env<'a>

§

impl<'a> RefUnwindSafe for Env<'a>

§

impl<'a> !Send for Env<'a>

§

impl<'a> !Sync for Env<'a>

§

impl<'a> Unpin for Env<'a>

§

impl<'a> UnwindSafe for Env<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.