Skip to content

Commit

Permalink
Current state
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Dec 18, 2024
1 parent d7cc993 commit c213733
Show file tree
Hide file tree
Showing 14 changed files with 491 additions and 393 deletions.
6 changes: 5 additions & 1 deletion rustler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ mod alloc;
pub mod types;

mod term;
mod wrapped_types;
pub use crate::wrapped_types::{
ListIterator, Map
};

pub use crate::term::Term;
pub use crate::types::{
Atom, Binary, Decoder, Encoder, ErlOption, ListIterator, LocalPid, MapIterator, NewBinary,
Atom, Binary, Decoder, Encoder, ErlOption, LocalPid, MapIterator, NewBinary,
OwnedBinary, Reference,
};

Expand Down
15 changes: 9 additions & 6 deletions rustler/src/types/elixir_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
//! `#[module = "Elixir.TheStructModule"]`.
use super::atom::{self, Atom};
use super::map::map_new;
use crate::{Env, NifResult, Term};
use super::map::Map;
use crate::{Env, Error, NifResult, Term};

pub fn get_ex_struct_name(map: Term) -> NifResult<Atom> {
// In an Elixir struct the value in the __struct__ field is always an atom.
map.map_get(atom::__struct__()).and_then(Atom::from_term)
let map: Map<'_> = map.try_into()?;
map.get(atom::__struct__())
.ok_or(Error::BadArg)
.and_then(Atom::from_term)
}

pub fn make_ex_struct<'a>(env: Env<'a>, struct_module: &str) -> NifResult<Term<'a>> {
let map = map_new(env);
pub fn make_ex_struct<'a>(env: Env<'a>, struct_module: &str) -> NifResult<Map<'a>> {
let map = env.new_map();

let struct_atom = atom::__struct__();
let module_atom = Atom::from_str(env, struct_module)?;

map.map_put(struct_atom, module_atom)
map.put(struct_atom, module_atom)
}
204 changes: 0 additions & 204 deletions rustler/src/types/list.rs

This file was deleted.

1 change: 0 additions & 1 deletion rustler/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{Env, Error, NifResult, Term};

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

#[macro_use]
pub mod atom;
Expand Down
61 changes: 5 additions & 56 deletions rustler/src/types/reference.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,15 @@
use std::ops::Deref;

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

use crate::sys::enif_make_ref;

wrapper!(Reference, TermType::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 Encoder for Reference<'_> {
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
self.0.encode(env)
}
wrapper!{
struct Reference(TermType::Ref)
}

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()))) }
let term = unsafe { Term::new(self, enif_make_ref(self.as_c_arg())) };
unsafe { Reference::wrap_unchecked(term) }
}
}
50 changes: 0 additions & 50 deletions rustler/src/types/wrapper.rs

This file was deleted.

Loading

0 comments on commit c213733

Please sign in to comment.