Skip to content

Commit c22f50a

Browse files
committed
Current state
1 parent 62878f8 commit c22f50a

File tree

8 files changed

+48
-11
lines changed

8 files changed

+48
-11
lines changed

rustler/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub use crate::types::{
4242
Atom, Binary, Decoder, Encoder, ErlOption, LocalPid, NewBinary, OwnedBinary,
4343
};
4444

45-
pub use crate::wrapped_types::{ListIterator, Map, MapIterator, Reference, Tuple};
45+
pub use crate::wrapped_types::{ListIterator, Map, MapIterator, Reference, Tuple, Wrapper};
4646

4747
#[cfg(feature = "big_integer")]
4848
pub use crate::types::BigInt;

rustler/src/serde/ser.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,11 @@ impl<'a> MapSerializer<'a> {
460460

461461
#[inline]
462462
fn to_map(&self) -> Result<Term<'a>, Error> {
463-
Term::map_from_arrays(self.ser.env, &self.keys, &self.values).or(Err(Error::InvalidMap))
463+
self.ser
464+
.env
465+
.map_from_arrays(&self.keys, &self.values)
466+
.map(|map| map.into())
467+
.or(Err(Error::InvalidMap))
464468
}
465469

466470
#[inline]

rustler/src/types/atom.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::wrapper::atom;
22
use crate::wrapper::NIF_TERM;
3+
use crate::Wrapper;
34
use crate::{Decoder, Encoder, Env, Error, NifResult, Term};
45

56
// Atoms are a special case of a term. They can be stored and used on all envs regardless of where
@@ -81,6 +82,18 @@ impl Atom {
8182
}
8283
}
8384

85+
impl<'a> Wrapper<'a> for Atom {
86+
const WRAPPED_TYPE: crate::TermType = crate::TermType::Atom;
87+
88+
fn unwrap(&self) -> Term<'a> {
89+
unimplemented!()
90+
}
91+
92+
unsafe fn wrap_unchecked(term: Term<'a>) -> Self {
93+
Atom::from_nif_term(term.as_c_arg())
94+
}
95+
}
96+
8497
use std::fmt;
8598
impl fmt::Debug for Atom {
8699
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {

rustler/src/wrapped_types/map.rs

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ impl<'a> Env<'a> {
1818
pub fn new_map(self) -> Map<'a> {
1919
unsafe { Map::wrap_unchecked(Term::new(self, enif_make_new_map(self.as_c_arg()))) }
2020
}
21+
22+
pub fn map_from_pairs(self, pairs: &[(impl Encoder, impl Encoder)]) -> NifResult<Map<'a>> {
23+
Term::map_from_pairs(self, pairs).map(|res| res.try_into().unwrap())
24+
}
25+
26+
pub fn map_from_arrays(
27+
self,
28+
keys: &[impl Encoder],
29+
values: &[impl Encoder],
30+
) -> NifResult<Map<'a>> {
31+
Term::map_from_arrays(self, keys, values).map(|res| res.try_into().unwrap())
32+
}
33+
34+
pub fn map_from_term_arrays(
35+
self,
36+
keys: &[Term<'a>],
37+
values: &[Term<'a>],
38+
) -> NifResult<Map<'a>> {
39+
Term::map_from_term_arrays(self, keys, values).map(|res| res.try_into().unwrap())
40+
}
2141
}
2242

2343
impl<'a> Map<'a> {

rustler/src/wrapped_types/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ pub use reference::Reference;
1010
pub use tuple::Tuple;
1111

1212
pub(crate) use wrapper::wrapper;
13-
pub(crate) use wrapper::Wrapper;
13+
pub use wrapper::Wrapper;

rustler/src/wrapped_types/wrapper.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl From<WrapperError> for crate::Error {
99
}
1010
}
1111

12-
pub(crate) trait Wrapper<'a>: Sized {
12+
pub trait Wrapper<'a>: Sized {
1313
const WRAPPED_TYPE: TermType;
1414

1515
unsafe fn wrap_ptr_unchecked(env: Env<'a>, ptr: ERL_NIF_TERM) -> Self {

rustler_codegen/src/map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ fn gen_decoder(ctx: &Context, fields: &[&Field], atoms_module_name: &Ident) -> T
8989
where
9090
T: rustler::Decoder<'a>,
9191
{
92-
use rustler::Encoder;
9392
match ::rustler::Decoder::decode(term.map_get(&field)?) {
9493
Err(_) => Err(::rustler::Error::RaiseTerm(Box::new(format!(
9594
"Could not decode field :{:?} on %{{}}",
@@ -123,7 +122,8 @@ fn gen_encoder(ctx: &Context, fields: &[&Field], atoms_module_name: &Ident) -> T
123122
ctx,
124123
quote! {
125124
use #atoms_module_name::*;
126-
::rustler::Term::map_from_term_arrays(env, &[#(#keys),*], &[#(#values),*]).unwrap()
125+
use ::rustler::Wrapper;
126+
env.map_from_term_arrays(&[#(#keys),*], &[#(#values),*]).unwrap().unwrap()
127127
},
128128
)
129129
}

rustler_tests/native/rustler_test/src/test_map.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustler::{Encoder, Env, Error, ListIterator, MapIterator, NifResult, Term, Tuple};
1+
use rustler::{Encoder, Env, Error, ListIterator, Map, MapIterator, NifResult, Term, Tuple};
22

33
#[rustler::nif]
44
pub fn sum_map_values(iter: MapIterator) -> NifResult<i64> {
@@ -43,15 +43,15 @@ pub fn map_from_arrays<'a>(
4343
env: Env<'a>,
4444
keys: Vec<Term<'a>>,
4545
values: Vec<Term<'a>>,
46-
) -> NifResult<Term<'a>> {
47-
Term::map_from_arrays(env, &keys, &values)
46+
) -> NifResult<Map<'a>> {
47+
env.map_from_arrays(&keys, &values)
4848
}
4949

5050
#[rustler::nif]
51-
pub fn map_from_pairs<'a>(env: Env<'a>, pairs: ListIterator<'a>) -> NifResult<Term<'a>> {
51+
pub fn map_from_pairs<'a>(env: Env<'a>, pairs: ListIterator<'a>) -> NifResult<Map<'a>> {
5252
let res: Result<Vec<(Term, Term)>, Error> = pairs.map(|x| x.decode()).collect();
5353

54-
res.and_then(|v| Term::map_from_pairs(env, &v))
54+
res.and_then(|v| env.map_from_pairs(&v))
5555
}
5656

5757
#[rustler::nif]

0 commit comments

Comments
 (0)