pub struct ResourceArc<T>where
    T: ResourceTypeProvider,{ /* private fields */ }
Expand description

A reference to a resource of type T.

This type is like std::sync::Arc: it provides thread-safe, reference-counted storage for Rust data that can be shared across threads. Data stored this way is immutable by default. If you need to modify data in a resource, use a std::sync::Mutex or RwLock.

Rust code and Erlang code can both have references to the same resource at the same time. Rust code uses ResourceArc; in Erlang, a reference to a resource is a kind of term. You can convert back and forth between the two using Encoder and Decoder.

Implementations§

source§

impl<T> ResourceArc<T>where T: ResourceTypeProvider,

source

pub fn new(data: T) -> Self

Makes a new ResourceArc from the given type. Note that the type must have ResourceTypeProvider implemented for it. See module documentation for info on this.

source

pub fn make_binary<'env, 'a, F>(&self, env: Env<'env>, f: F) -> Binary<'env>where F: FnOnce(&'a T) -> &'a [u8],

Make a resource binary associated with the given resource

The closure f is called with the referenced object and must return a slice with the same lifetime as the object. This means that the slice either has to be derived directly from the instance or that it has to have static lifetime.

source

pub unsafe fn make_binary_unsafe<'env, 'a, 'b, F>( &self, env: Env<'env>, f: F ) -> Binary<'env>where F: FnOnce(&'a T) -> &'b [u8],

Make a resource binary without strict lifetime checking

The user must ensure that the lifetime of the returned slice is at least as long as the lifetime of the referenced instance.

Safety

This function is only safe if the slice that is returned from the closure is guaranteed to live at least as long as the ResourceArc instance. If in doubt, use the safe version ResourceArc::make_binary which enforces this bound through its signature.

Trait Implementations§

source§

impl<T> Clone for ResourceArc<T>where T: ResourceTypeProvider,

source§

fn clone(&self) -> Self

Cloning a ResourceArc simply increments the reference count for the resource. The T value is not cloned.

1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<'a, T> Decoder<'a> for ResourceArc<T>where T: ResourceTypeProvider + 'a,

source§

fn decode(term: Term<'a>) -> NifResult<Self>

source§

impl<T> Deref for ResourceArc<T>where T: ResourceTypeProvider,

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &T

Dereferences the value.
source§

impl<T> Drop for ResourceArc<T>where T: ResourceTypeProvider,

source§

fn drop(&mut self)

When a ResourceArc is dropped, the reference count is decremented. If there are no other references to the resource, the T value is dropped.

However, note that in general, the Rust value in a resource is dropped at an unpredictable time: whenever the VM decides to do garbage collection.

source§

impl<T> Encoder for ResourceArc<T>where T: ResourceTypeProvider,

source§

fn encode<'a>(&self, env: Env<'a>) -> Term<'a>

source§

impl<T> Send for ResourceArc<T>where T: ResourceTypeProvider,

source§

impl<T> Sync for ResourceArc<T>where T: ResourceTypeProvider,

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for ResourceArc<T>where T: RefUnwindSafe,

§

impl<T> Unpin for ResourceArc<T>

§

impl<T> UnwindSafe for ResourceArc<T>where T: RefUnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.