Struct emacs::GlobalRef

source ·
pub struct GlobalRef { /* private fields */ }
Expand description

A “global reference” that can live outside the scope of an Env. This is useful for sharing an otherwise short-lived Lisp Value across multiple invocations of Rust functions defined with defun. Examples include efficient access to interned symbols or Lisp functions, and Rust-based multi-threading.

§Implementation

Cloning this struct requires an Env, so it doesn’t implement Clone.

free_global_ref requires an Env. Therefore, to avoid leaking the underlying Value, free should be used to free a global reference, instead of drop. For the use case of accessing interned symbols and Lisp functions, this is a non-issue, as the values are supposed to be “static” anyway.

The above is a shortcoming in the design of emacs-module. There are 2 possible ways to fix it:

  • Make free_global_ref work without an env, like Erlang’s enif_release_resource.
  • Allow user_ptr’s finalizer to access the env, to properly free associated global refs.

Implementations§

source§

impl GlobalRef

source

pub fn call<'e, A>(&'e self, env: &'e Env, args: A) -> Result<Value<'_>>
where A: IntoLispArgs<'e>,

Calls this reference’s value with the given arguments. An error is signaled if it is actually not a Lisp’s callable.

args should be an array/slice of Value, or a tuple of different types, each implementing IntoLisp.

source

pub unsafe fn call_unprotected<'e, A>( &'e self, env: &'e Env, args: A ) -> Result<Value<'_>>
where A: IntoLispArgs<'e>,

Like call, except that the returned Value is not protected against Emacs GC’s bug #31238, which caused issue #2.

§Safety

This can be used as an optimization, in situations when the returned Value is unused, or when its usage is shorter than the lifespan of the underlying Lisp object.

source§

impl GlobalRef

source

pub fn new(value: Value<'_>) -> Self

Creates a new global reference for the given Value.

source

pub fn free(self, env: &Env) -> Result<()>

Frees this global reference.

source

pub fn bind<'e, 'g: 'e>(&'g self, env: &'e Env) -> Value<'e>

Returns the underlying Value, scoping its lifetime to the given Env.

source

pub fn clone(&self, env: &Env) -> Self

Returns a copy of this global reference.

Trait Implementations§

source§

impl Debug for GlobalRef

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'e> FromLisp<'e> for GlobalRef

source§

fn from_lisp(value: Value<'e>) -> Result<Self>

source§

impl<'e> IntoLisp<'e> for &'e GlobalRef

source§

fn into_lisp(self, env: &'e Env) -> Result<Value<'e>>

source§

impl Send for GlobalRef

source§

impl Sync for GlobalRef

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where 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 T
where 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.