-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reference type and
make_ref
call (#657)
* Add reference type and `make_ref` call * Add comments and in_env variant returning a ref again * Add changelog entry
- Loading branch information
Showing
7 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::ops::Deref; | ||
|
||
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>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<'a> From<Reference<'a>> for Term<'a> { | ||
fn from(term: Reference<'a>) -> Self { | ||
term.0 | ||
} | ||
} | ||
|
||
impl<'a> TryFrom<Term<'a>> for Reference<'a> { | ||
type Error = Error; | ||
|
||
fn try_from(term: Term<'a>) -> Result<Self, Self::Error> { | ||
if term.is_ref() { | ||
Ok(Reference(term)) | ||
} else { | ||
Err(Error::BadArg) | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Decoder<'a> for Reference<'a> { | ||
fn decode(term: Term<'a>) -> NifResult<Self> { | ||
term.try_into() | ||
} | ||
} | ||
|
||
impl<'a> Encoder for Reference<'a> { | ||
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> { | ||
self.0.encode(env) | ||
} | ||
} | ||
|
||
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()))) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters