Skip to content

Commit

Permalink
Start implementing generic wrapper types
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Dec 17, 2024
1 parent 9522518 commit d7cc993
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
4 changes: 4 additions & 0 deletions rustler/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::{Env, Error, NifResult, Term};

#[macro_use]
mod wrapper;
pub(crate) use self::wrapper::wrapper;

#[macro_use]
pub mod atom;
pub use crate::types::atom::Atom;
Expand Down
28 changes: 15 additions & 13 deletions rustler/src/types/reference.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
use std::ops::Deref;

use crate::{Decoder, Encoder, Env, Error, NifResult, Term};
use crate::{Decoder, Encoder, Env, Error, NifResult, Term, TermType};

use crate::sys::enif_make_ref;

/// Wrapper for BEAM reference terms.
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct Reference<'a>(Term<'a>);
wrapper!(Reference, TermType::Ref);

impl Reference<'_> {
/// 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))
}
}
/// 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>;
Expand Down
50 changes: 50 additions & 0 deletions rustler/src/types/wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::TermType;

struct WrapperError;

pub trait Wrapper<'a>: Sized {
const WRAPPED_TYPE: TermType;

fn wrap<'b>(term: Term<'a>) -> Result<Self, WrapperError> {
if term.get_type() == Self::WRAPPED_TYPE {
unsafe { Ok(Self::wrap_unsafe(term)) }
} else {
Err(WrapperError)
}
}

fn unwrap(&self) -> Term<'a>;

unsafe fn wrap_unsafe(term: Term<'a>) -> Self;

/// 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.
fn in_env<'b>(&self, env: Env<'b>) -> impl Wrapper<'b, WRAPPED_TYPE = Self::WrappedType> {
self.unwrap().in_env(env)
}
}

macro_rules! wrapper {
($name:ident, $term_type:path) => {
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct $name<'a>(Term<'a>);

impl<'a> $crate::types::wrapper::Wrapper<'a> for $name<'a> {
const WrappedType: TermType = $term_type;

unsafe fn wrap_unsafe(term: Term<'a>) -> Self {
$name(term)
}

fn unwrap(&self) -> Term<'a> {
self.0
}
}
};
}

pub(crate) use wrapper;

use crate::{Env, Term};

0 comments on commit d7cc993

Please sign in to comment.