diff --git a/rustler/src/types/reference.rs b/rustler/src/types/reference.rs index 6ec526db..013e3263 100644 --- a/rustler/src/types/reference.rs +++ b/rustler/src/types/reference.rs @@ -4,9 +4,20 @@ use crate::{Decoder, Encoder, Env, Error, NifResult, Term}; use crate::sys::enif_make_ref; +/// Wrapper for BEAM reference terms. #[derive(PartialEq, Eq, Clone, Copy)] pub struct Reference<'a>(Term<'a>); +impl<'a> Reference<'a> { + /// Returns a representation of self in the given Env. + /// + /// If the term is already is in the provided env, it will be directly returned. Otherwise + /// the term will be copied over. + pub fn in_env<'b>(&self, env: Env<'b>) -> Reference<'b> { + Reference(self.0.in_env(env)) + } +} + impl<'a> Deref for Reference<'a> { type Target = Term<'a>; @@ -46,6 +57,7 @@ impl<'a> Encoder for Reference<'a> { } impl<'a> Env<'a> { + /// Create a new reference in this environment pub fn make_ref(self) -> Reference<'a> { unsafe { Reference(Term::new(self, enif_make_ref(self.as_c_arg()))) } }