Skip to content

Commit a210919

Browse files
committed
Current state
1 parent c213733 commit a210919

File tree

9 files changed

+40
-45
lines changed

9 files changed

+40
-45
lines changed

rustler/src/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,14 @@ pub mod types;
3636

3737
mod term;
3838
mod wrapped_types;
39-
pub use crate::wrapped_types::{
40-
ListIterator, Map
41-
};
4239

4340
pub use crate::term::Term;
4441
pub use crate::types::{
45-
Atom, Binary, Decoder, Encoder, ErlOption, LocalPid, MapIterator, NewBinary,
46-
OwnedBinary, Reference,
42+
Atom, Binary, Decoder, Encoder, ErlOption, LocalPid, NewBinary, OwnedBinary
4743
};
4844

45+
pub use crate::wrapped_types::{ListIterator, Reference, MapIterator, Map, Tuple};
46+
4947
#[cfg(feature = "big_integer")]
5048
pub use crate::types::BigInt;
5149

rustler/src/types/elixir_struct.rs

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

1413
pub fn get_ex_struct_name(map: Term) -> NifResult<Atom> {
1514
// In an Elixir struct the value in the __struct__ field is always an atom.

rustler/src/types/mod.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1+
use crate::wrapped_types::MapIterator;
12
use crate::{Env, Error, NifResult, Term};
23

3-
#[macro_use]
4-
mod wrapper;
5-
64
#[macro_use]
75
pub mod atom;
86
pub use crate::types::atom::Atom;
@@ -15,28 +13,15 @@ pub mod big_int;
1513
#[cfg(feature = "big_integer")]
1614
pub use num_bigint::BigInt;
1715

18-
#[doc(hidden)]
19-
pub mod list;
20-
pub use crate::types::list::ListIterator;
21-
22-
#[doc(hidden)]
23-
pub mod map;
24-
pub use self::map::MapIterator;
25-
2616
#[doc(hidden)]
2717
pub mod primitive;
2818
#[doc(hidden)]
2919
pub mod string;
30-
pub mod tuple;
3120

3221
#[doc(hidden)]
3322
pub mod local_pid;
3423
pub use self::local_pid::LocalPid;
3524

36-
#[doc(hidden)]
37-
pub mod reference;
38-
pub use self::reference::Reference;
39-
4025
pub mod i128;
4126
pub mod path;
4227

rustler/src/wrapped_types/map.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//! Utilities used to access and create Erlang maps.
22
3-
use super::atom;
43
use crate::sys::{enif_get_map_value, enif_make_map_put, enif_make_new_map};
4+
use crate::types::atom;
55
use crate::wrapper::map;
66
use crate::{Decoder, Encoder, Env, Error, NifResult, Term, TermType};
77
use std::mem::MaybeUninit;
88
use std::ops::RangeInclusive;
99

10+
use super::wrapper;
11+
1012
wrapper!(
1113
/// A wrapper around an Erlang map term.
1214
struct Map(TermType::Map)

rustler/src/wrapped_types/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
mod list;
22
mod map;
3-
mod tuple;
4-
mod wrapper;
3+
mod reference;
4+
pub mod tuple;
5+
pub mod wrapper;
6+
7+
pub use list::ListIterator;
8+
pub use map::{Map, MapIterator};
9+
pub use reference::Reference;
10+
pub use tuple::Tuple;
511

612
pub(crate) use wrapper::wrapper;
7-
pub(crate) use list::ListIterator;
13+
pub(crate) use wrapper::Wrapper;

rustler/src/types/reference.rs renamed to rustler/src/wrapped_types/reference.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::{Env, Term, TermType};
22

33
use crate::sys::enif_make_ref;
44

5+
use super::wrapper;
6+
57
wrapper!{
68
struct Reference(TermType::Ref)
79
}

rustler/src/wrapped_types/tuple.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use crate::{
2-
sys::{enif_get_tuple, ERL_NIF_TERM},
3-
Decoder, Encoder, Env, Error, NifResult, Term, TermType,
4-
};
1+
use crate::{Decoder, Encoder, Env, Error, NifResult, Term, TermType};
2+
use crate::sys::{enif_get_tuple, enif_make_tuple_from_array, ERL_NIF_TERM};
53

6-
use std::{ffi::c_int, mem::MaybeUninit, ops::Index};
4+
use std::ffi::c_int;
5+
use std::mem::MaybeUninit;
6+
7+
use super::wrapper;
78
wrapper!(
89
struct Tuple(TermType::Tuple)
910
);
@@ -33,7 +34,7 @@ impl<'a> Tuple<'a> {
3334
pub fn get(&self, index: usize) -> Option<Term<'a>> {
3435
self.get_elements()
3536
.get(index)
36-
.map(|ptr| unsafe { Term::new(self.get_env(), ptr) })
37+
.map(|ptr| unsafe { Term::new(self.get_env(), *ptr) })
3738
}
3839

3940
/// Convert an Erlang tuple to a Rust vector. (To convert to a Rust tuple, use `term.decode()`
@@ -56,8 +57,13 @@ impl<'a> Tuple<'a> {
5657
/// Convert a vector of terms to an Erlang tuple. (To convert from a Rust tuple to an Erlang tuple,
5758
/// use `Encoder` instead.)
5859
pub fn make_tuple<'a>(env: Env<'a>, terms: &[Term]) -> Term<'a> {
59-
let c_terms: Vec<NIF_TERM> = terms.iter().map(|term| term.as_c_arg()).collect();
60-
unsafe { Term::new(env, tuple::make_tuple(env.as_c_arg(), &c_terms)) }
60+
let c_terms: Vec<ERL_NIF_TERM> = terms.iter().map(|term| term.as_c_arg()).collect();
61+
unsafe {
62+
let term =
63+
enif_make_tuple_from_array(env.as_c_arg(), c_terms.as_ptr(), c_terms.len() as u32);
64+
Term::new(env, term)
65+
}
66+
// unsafe { Term::new(env, tuple::make_tuple(env.as_c_arg(), &c_terms)) }
6167
}
6268

6369
/// Helper macro to emit tuple-like syntax. Wraps its arguments in parentheses, and adds a comma if
@@ -83,10 +89,8 @@ macro_rules! impl_nifencoder_nifdecoder_for_tuple {
8389
Encoder for tuple!( $( $tyvar ),* )
8490
{
8591
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
86-
let arr = [ $( Encoder::encode(&self.$index, env).as_c_arg() ),* ];
87-
unsafe {
88-
Term::new(env, tuple::make_tuple(env.as_c_arg(), &arr))
89-
}
92+
let arr = [ $( Encoder::encode(&self.$index, env) ),* ];
93+
make_tuple(env, &arr)
9094
}
9195
}
9296

@@ -95,7 +99,7 @@ macro_rules! impl_nifencoder_nifdecoder_for_tuple {
9599
{
96100
fn decode(term: Term<'a>) -> NifResult<tuple!( $( $tyvar ),* )>
97101
{
98-
match unsafe { tuple::get_tuple(term.get_env().as_c_arg(), term.as_c_arg()) } {
102+
match unsafe { get_tuple(term) } {
99103
Ok(elements) if elements.len() == count!( $( $index ),* ) =>
100104
Ok(tuple!( $(
101105
(<$tyvar as Decoder>::decode(

rustler/src/wrapped_types/wrapper.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ macro_rules! wrapper {
4646
#[derive(PartialEq, Eq, Clone, Copy)]
4747
pub struct $name<'a>(Term<'a>);
4848

49-
use $crate::types::wrapper::Wrapper;
49+
use $crate::wrapped_types::Wrapper;
5050

5151
impl<'a> $name<'a> {
5252
/// Returns a representation of self in the given Env.
@@ -83,14 +83,14 @@ macro_rules! wrapper {
8383
type Error = $crate::Error;
8484

8585
fn try_from(term: Term<'a>) -> Result<Self, Self::Error> {
86-
use $crate::types::wrapper::Wrapper;
86+
use $crate::wrapped_types::Wrapper;
8787
Self::wrap(term).or(Err($crate::Error::BadArg))
8888
}
8989
}
9090

9191
impl<'a> $crate::Decoder<'a> for $name<'a> {
9292
fn decode(term: Term<'a>) -> $crate::NifResult<Self> {
93-
use $crate::types::wrapper::Wrapper;
93+
use $crate::wrapped_types::Wrapper;
9494
Self::wrap(term).or(Err($crate::Error::BadArg))
9595
}
9696
}

rustler/src/wrapper/tuple.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::sys::enif_make_tuple_from_array;
2-
use crate::wrapper::{c_int, NIF_ENV, NIF_ERROR, NIF_TERM};
3-
use std::mem::MaybeUninit;
2+
use crate::wrapper::{NIF_ENV, NIF_TERM};
43

54
pub unsafe fn make_tuple(env: NIF_ENV, terms: &[NIF_TERM]) -> NIF_TERM {
65
enif_make_tuple_from_array(env, terms.as_ptr(), terms.len() as u32)

0 commit comments

Comments
 (0)