From 394fc44f844363b0df63c385c468979fdd076507 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Sun, 13 Oct 2024 09:12:28 +0200 Subject: [PATCH] Add comments and in_env variant returning a ref again --- rustler/src/types/reference.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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()))) } }