Skip to content

Commit

Permalink
use proper self convention
Browse files Browse the repository at this point in the history
  • Loading branch information
nitsky committed Jun 23, 2021
1 parent f98b4d2 commit 94cb9b2
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 77 deletions.
8 changes: 5 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ license = "MIT"
name = "erl_nif"
publish = true
repository = "https://github.com/tangramxyz/erl_nif"
version = "0.2.0"
version = "0.3.0"

[lib]
path = "lib.rs"
Expand All @@ -25,7 +25,7 @@ default = []
serde_1 = ["serde"]

[dependencies]
erl_nif_macro = { version = "0.2", path = "macro" }
erl_nif_sys = { version = "0.2", path = "sys" }
erl_nif_macro = { version = "0.3", path = "macro" }
erl_nif_sys = { version = "0.3", path = "sys" }
num = "0.4"
serde = { version = "1", optional = true }
102 changes: 51 additions & 51 deletions convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use crate::{
use num::{FromPrimitive, ToPrimitive};

#[allow(clippy::wrong_self_convention, clippy::upper_case_acronyms)]
pub trait ToErlNif<'a>: 'a {
fn to_erl_nif(self, env: Env<'a>) -> Result<Term<'a>>;
pub trait IntoErlNif<'a>: 'a {
fn into_erl_nif(self, env: Env<'a>) -> Result<Term<'a>>;
}

#[allow(clippy::wrong_self_convention, clippy::upper_case_acronyms)]
pub trait FromErlNif<'a>: 'a + Sized {
fn from_erl_nif(term: Term<'a>) -> Result<Self>;
}

impl<'a> ToErlNif<'a> for () {
fn to_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for () {
fn into_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
Ok(Atom::new(env, "nil")?.term())
}
}
Expand All @@ -30,8 +30,8 @@ impl<'a> FromErlNif<'a> for () {
}
}

impl<'a> ToErlNif<'a> for bool {
fn to_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for bool {
fn into_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
let atom = match self {
true => "true",
false => "false",
Expand All @@ -54,8 +54,8 @@ impl<'a> FromErlNif<'a> for bool {

macro_rules! impl_to_from_for_integer_type {
($ty:ty) => {
impl<'a> ToErlNif<'a> for $ty {
fn to_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for $ty {
fn into_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
let value =
<$ty>::to_i64(&self).ok_or_else(|| Error::message("integer out of bounds"))?;
let integer = Integer::new(env, value);
Expand Down Expand Up @@ -87,8 +87,8 @@ impl_to_from_for_integer_type!(i64);

macro_rules! impl_to_from_for_float_type {
($ty:ty) => {
impl<'a> ToErlNif<'a> for $ty {
fn to_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for $ty {
fn into_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
let value =
<$ty>::to_f64(&self).ok_or_else(|| Error::message("float out of bounds"))?;
let float = Float::new(env, value);
Expand All @@ -110,9 +110,9 @@ macro_rules! impl_to_from_for_float_type {
impl_to_from_for_float_type!(f32);
impl_to_from_for_float_type!(f64);

impl<'a> ToErlNif<'a> for char {
fn to_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
self.to_string().to_erl_nif(env)
impl<'a> IntoErlNif<'a> for char {
fn into_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
self.to_string().into_erl_nif(env)
}
}

Expand All @@ -132,14 +132,14 @@ impl<'a> FromErlNif<'a> for char {
}
}

impl<'a> ToErlNif<'a> for &'a str {
fn to_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for &'a str {
fn into_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
Ok(BinaryTerm::from_str(env, self)?.term())
}
}

impl<'a> ToErlNif<'a> for std::string::String {
fn to_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for std::string::String {
fn into_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
Ok(BinaryTerm::from_str(env, &self)?.term())
}
}
Expand All @@ -162,14 +162,14 @@ impl<'a> FromErlNif<'a> for std::string::String {
}
}

impl<'a, T> ToErlNif<'a> for Option<T>
impl<'a, T> IntoErlNif<'a> for Option<T>
where
T: ToErlNif<'a>,
T: IntoErlNif<'a>,
{
fn to_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
fn into_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
match self {
None => Ok(Atom::new(env, "nil")?.term()),
Some(term) => Ok(term.to_erl_nif(env)?),
Some(term) => Ok(term.into_erl_nif(env)?),
}
}
}
Expand All @@ -194,14 +194,14 @@ where
}
}

impl<'a, T> ToErlNif<'a> for Vec<T>
impl<'a, T> IntoErlNif<'a> for Vec<T>
where
T: ToErlNif<'a>,
T: IntoErlNif<'a>,
{
fn to_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
fn into_erl_nif(self, env: Env<'a>) -> Result<Term<'a>> {
let terms = self
.into_iter()
.map(|value| value.to_erl_nif(env))
.map(|value| value.into_erl_nif(env))
.collect::<Result<Vec<_>>>()?;
let list = List::new(env, terms)?;
Ok(list.term())
Expand All @@ -220,8 +220,8 @@ where
}
}

impl<'a> ToErlNif<'a> for Term<'a> {
fn to_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for Term<'a> {
fn into_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
Ok(self)
}
}
Expand All @@ -232,8 +232,8 @@ impl<'a> FromErlNif<'a> for Term<'a> {
}
}

impl<'a> ToErlNif<'a> for Atom<'a> {
fn to_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for Atom<'a> {
fn into_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
Ok(self.term())
}
}
Expand All @@ -244,8 +244,8 @@ impl<'a> FromErlNif<'a> for Atom<'a> {
}
}

impl<'a> ToErlNif<'a> for BinaryTerm<'a> {
fn to_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for BinaryTerm<'a> {
fn into_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
Ok(self.term())
}
}
Expand All @@ -256,8 +256,8 @@ impl<'a> FromErlNif<'a> for BinaryTerm<'a> {
}
}

impl<'a> ToErlNif<'a> for BitString<'a> {
fn to_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for BitString<'a> {
fn into_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
Ok(self.term())
}
}
Expand All @@ -268,8 +268,8 @@ impl<'a> FromErlNif<'a> for BitString<'a> {
}
}

impl<'a> ToErlNif<'a> for Float<'a> {
fn to_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for Float<'a> {
fn into_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
Ok(self.term())
}
}
Expand All @@ -280,8 +280,8 @@ impl<'a> FromErlNif<'a> for Float<'a> {
}
}

impl<'a> ToErlNif<'a> for Fun<'a> {
fn to_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for Fun<'a> {
fn into_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
Ok(self.term())
}
}
Expand All @@ -292,8 +292,8 @@ impl<'a> FromErlNif<'a> for Fun<'a> {
}
}

impl<'a> ToErlNif<'a> for Integer<'a> {
fn to_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for Integer<'a> {
fn into_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
Ok(self.term())
}
}
Expand All @@ -304,8 +304,8 @@ impl<'a> FromErlNif<'a> for Integer<'a> {
}
}

impl<'a> ToErlNif<'a> for List<'a> {
fn to_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for List<'a> {
fn into_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
Ok(self.term())
}
}
Expand All @@ -316,8 +316,8 @@ impl<'a> FromErlNif<'a> for List<'a> {
}
}

impl<'a> ToErlNif<'a> for Map<'a> {
fn to_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for Map<'a> {
fn into_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
Ok(self.term())
}
}
Expand All @@ -328,8 +328,8 @@ impl<'a> FromErlNif<'a> for Map<'a> {
}
}

impl<'a> ToErlNif<'a> for Pid<'a> {
fn to_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for Pid<'a> {
fn into_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
Ok(self.term())
}
}
Expand All @@ -340,8 +340,8 @@ impl<'a> FromErlNif<'a> for Pid<'a> {
}
}

impl<'a> ToErlNif<'a> for Port<'a> {
fn to_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for Port<'a> {
fn into_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
Ok(self.term())
}
}
Expand All @@ -352,8 +352,8 @@ impl<'a> FromErlNif<'a> for Port<'a> {
}
}

// impl<T> ToErlNif for ResourceTerm<T> {
// fn to_erl_nif(self, _env: Env) -> Result<Term> {
// impl<T> IntoErlNif for ResourceTerm<T> {
// fn into_erl_nif(self, _env: Env) -> Result<Term> {
// Ok(self.term())
// }
// }
Expand All @@ -364,8 +364,8 @@ impl<'a> FromErlNif<'a> for Port<'a> {
// }
// }

impl<'a> ToErlNif<'a> for Tuple<'a> {
fn to_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
impl<'a> IntoErlNif<'a> for Tuple<'a> {
fn into_erl_nif(self, _env: Env<'a>) -> Result<Term<'a>> {
Ok(self.term())
}
}
Expand Down
4 changes: 2 additions & 2 deletions env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
sys::{enif_raise_exception, ErlNifEnv, ERL_NIF_TERM},
ToErlNif,
IntoErlNif,
};
use std::marker::PhantomData;

Expand All @@ -18,7 +18,7 @@ impl<'a> Env<'a> {

pub fn raise_exception(&self, message: &str) -> ERL_NIF_TERM {
let message = message
.to_erl_nif(*self)
.into_erl_nif(*self)
.expect("failed to create exception message");
unsafe { enif_raise_exception(self.raw(), message.raw()) }
}
Expand Down
2 changes: 1 addition & 1 deletion lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod serde;
pub mod term;

pub use self::{
convert::{FromErlNif, ToErlNif},
convert::{FromErlNif, IntoErlNif},
entry::Entry,
env::Env,
error::{Error, Result},
Expand Down
2 changes: 1 addition & 1 deletion macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
name = "erl_nif_macro"
publish = true
repository = "https://github.com/tangramxyz/erl_nif"
version = "0.2.0"
version = "0.3.0"

[lib]
path = "lib.rs"
Expand Down
2 changes: 1 addition & 1 deletion macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn nif_impl(input: proc_macro2::TokenStream) -> syn::Result<proc_macro2::TokenSt
let argv = unsafe { std::slice::from_raw_parts(argv, argc as usize) };
#(#from_erl_nif_statements)*
let output = nif_impl(env, #(#args),*).map_err(|error| erl_nif::Error::message(error.to_string()))?;
let output = erl_nif::ToErlNif::to_erl_nif(output, env)?;
let output = erl_nif::IntoErlNif::into_erl_nif(output, env)?;
Ok(output)
});
let result = match result {
Expand Down
Loading

0 comments on commit 94cb9b2

Please sign in to comment.