Skip to content

Commit c213733

Browse files
committed
Current state
1 parent d7cc993 commit c213733

File tree

14 files changed

+491
-393
lines changed

14 files changed

+491
-393
lines changed

rustler/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ mod alloc;
3535
pub mod types;
3636

3737
mod term;
38+
mod wrapped_types;
39+
pub use crate::wrapped_types::{
40+
ListIterator, Map
41+
};
3842

3943
pub use crate::term::Term;
4044
pub use crate::types::{
41-
Atom, Binary, Decoder, Encoder, ErlOption, ListIterator, LocalPid, MapIterator, NewBinary,
45+
Atom, Binary, Decoder, Encoder, ErlOption, LocalPid, MapIterator, NewBinary,
4246
OwnedBinary, Reference,
4347
};
4448

rustler/src/types/elixir_struct.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@
88
//! `#[module = "Elixir.TheStructModule"]`.
99
1010
use super::atom::{self, Atom};
11-
use super::map::map_new;
12-
use crate::{Env, NifResult, Term};
11+
use super::map::Map;
12+
use crate::{Env, Error, NifResult, Term};
1313

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

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

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

25-
map.map_put(struct_atom, module_atom)
28+
map.put(struct_atom, module_atom)
2629
}

rustler/src/types/list.rs

-204
This file was deleted.

rustler/src/types/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::{Env, Error, NifResult, Term};
22

33
#[macro_use]
44
mod wrapper;
5-
pub(crate) use self::wrapper::wrapper;
65

76
#[macro_use]
87
pub mod atom;

rustler/src/types/reference.rs

+5-56
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,15 @@
1-
use std::ops::Deref;
2-
3-
use crate::{Decoder, Encoder, Env, Error, NifResult, Term, TermType};
1+
use crate::{Env, Term, TermType};
42

53
use crate::sys::enif_make_ref;
64

7-
wrapper!(Reference, TermType::Ref);
8-
9-
/// Wrapper for BEAM reference terms.
10-
// #[derive(PartialEq, Eq, Clone, Copy)]
11-
// pub struct Reference<'a>(Term<'a>);
12-
//
13-
// impl<'a> Reference<'a> {
14-
// /// Returns a representation of self in the given Env.
15-
// ///
16-
// /// If the term is already is in the provided env, it will be directly returned. Otherwise
17-
// /// the term will be copied over.
18-
// pub fn in_env<'b>(&self, env: Env<'b>) -> Reference<'b> {
19-
// Reference(self.0.in_env(env))
20-
// }
21-
// }
22-
23-
impl<'a> Deref for Reference<'a> {
24-
type Target = Term<'a>;
25-
26-
fn deref(&self) -> &Self::Target {
27-
&self.0
28-
}
29-
}
30-
31-
impl<'a> From<Reference<'a>> for Term<'a> {
32-
fn from(term: Reference<'a>) -> Self {
33-
term.0
34-
}
35-
}
36-
37-
impl<'a> TryFrom<Term<'a>> for Reference<'a> {
38-
type Error = Error;
39-
40-
fn try_from(term: Term<'a>) -> Result<Self, Self::Error> {
41-
if term.is_ref() {
42-
Ok(Reference(term))
43-
} else {
44-
Err(Error::BadArg)
45-
}
46-
}
47-
}
48-
49-
impl<'a> Decoder<'a> for Reference<'a> {
50-
fn decode(term: Term<'a>) -> NifResult<Self> {
51-
term.try_into()
52-
}
53-
}
54-
55-
impl Encoder for Reference<'_> {
56-
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
57-
self.0.encode(env)
58-
}
5+
wrapper!{
6+
struct Reference(TermType::Ref)
597
}
608

619
impl<'a> Env<'a> {
6210
/// Create a new reference in this environment
6311
pub fn make_ref(self) -> Reference<'a> {
64-
unsafe { Reference(Term::new(self, enif_make_ref(self.as_c_arg()))) }
12+
let term = unsafe { Term::new(self, enif_make_ref(self.as_c_arg())) };
13+
unsafe { Reference::wrap_unchecked(term) }
6514
}
6615
}

rustler/src/types/wrapper.rs

-50
This file was deleted.

0 commit comments

Comments
 (0)