Struct rustler::resource::ResourceArc
source · 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,
impl<T> ResourceArc<T>where
T: ResourceTypeProvider,
sourcepub fn new(data: T) -> Self
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.
sourcepub fn make_binary<'env, 'a, F>(&self, env: Env<'env>, f: F) -> Binary<'env>where
F: FnOnce(&'a T) -> &'a [u8],
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.
sourcepub unsafe fn make_binary_unsafe<'env, 'a, 'b, F>(
&self,
env: Env<'env>,
f: F
) -> Binary<'env>where
F: FnOnce(&'a T) -> &'b [u8],
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,
impl<T> Clone for ResourceArc<T>where
T: ResourceTypeProvider,
source§impl<'a, T> Decoder<'a> for ResourceArc<T>where
T: ResourceTypeProvider + 'a,
impl<'a, T> Decoder<'a> for ResourceArc<T>where
T: ResourceTypeProvider + 'a,
source§impl<T> Deref for ResourceArc<T>where
T: ResourceTypeProvider,
impl<T> Deref for ResourceArc<T>where
T: ResourceTypeProvider,
source§impl<T> Drop for ResourceArc<T>where
T: ResourceTypeProvider,
impl<T> Drop for ResourceArc<T>where
T: ResourceTypeProvider,
source§fn drop(&mut self)
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.