Resource

Trait Resource 

Source
pub trait Resource:
    Sized
    + Send
    + Sync
    + 'static {
    const IMPLEMENTS_DESTRUCTOR: bool = false;
    const IMPLEMENTS_DOWN: bool = false;
    const IMPLEMENTS_DYNCALL: bool = false;

    // Provided methods
    fn destructor(self, env: Env<'_>) { ... }
    fn down<'a>(&'a self, env: Env<'a>, pid: LocalPid, monitor: Monitor) { ... }
    unsafe fn dyncall<'a>(&'a self, env: Env<'a>, call_data: *mut c_void) { ... }
}
Expand description

Trait that needs to be implemented to use a type as a NIF resource type.

The Erlang runtime provides the following guarantees:

  • An object will be valid as long as the associated environment is valid
  • destructor is the last function that is run on an object before it is freed

In particular, the type needs to handle all synchronization itself (thus we require it to implement Sync) and callbacks or NIFs can run on arbitrary threads (thus we require Send).

Currently only destructor and down callbacks are possible. If a callback is implemented, the respective associated constant IMPLEMENTS_... must be set to true for the registration to take it into account. All callbacks provide (empty) default implementations.

Provided Associated Constants§

Provided Methods§

Source

fn destructor(self, env: Env<'_>)

Callback function that is executed right before dropping a resource object.

This callback does not have to be implemented to release associated resources or run constructors. For that it is enough to implement Drop or rely on the generated Drop implementation which will be called in any case. The function is useful when the cleanup requires access to a NIF env, e.g. to send messages.

Source

fn down<'a>(&'a self, env: Env<'a>, pid: LocalPid, monitor: Monitor)

Callback function to handle process monitoring.

This callback is called when a process monitored using Env::monitor terminates and receives the pid of that process as well as the Monitor instance that was returned by ResourceArc<T>::monitor.

Source

unsafe fn dyncall<'a>(&'a self, env: Env<'a>, call_data: *mut c_void)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§