Skip to content

Commit

Permalink
Add 128bit integer support
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Mar 20, 2024
1 parent 3af5e13 commit f3ca390
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
18 changes: 18 additions & 0 deletions rustler/src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ impl<'de, 'a: 'de> de::Deserializer<'de> for Deserializer<'a> {
TermType::Integer => {
try_parse_number!(self.term, u64, visitor, visit_u64);
try_parse_number!(self.term, i64, visitor, visit_i64);
try_parse_number!(self.term, u128, visitor, visit_u128);
try_parse_number!(self.term, i128, visitor, visit_i128);

Err(Error::ExpectedNumber)
}
Expand Down Expand Up @@ -166,6 +168,14 @@ impl<'de, 'a: 'de> de::Deserializer<'de> for Deserializer<'a> {
visitor.visit_i64(util::parse_number(&self.term)?)
}

#[inline]
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_i128(util::parse_number(&self.term)?)
}

#[inline]
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
Expand Down Expand Up @@ -198,6 +208,14 @@ impl<'de, 'a: 'de> de::Deserializer<'de> for Deserializer<'a> {
visitor.visit_u64(util::parse_number(&self.term)?)
}

#[inline]
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, self::Error>
where
V: Visitor<'de>,
{
visitor.visit_u128(util::parse_number(&self.term)?)
}

#[inline]
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
Expand Down
18 changes: 6 additions & 12 deletions rustler/src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use std::io::Write;
use crate::serde::{atoms, error::Error, util};
use crate::wrapper::list::make_list;
use crate::{types::tuple, Encoder, Env, OwnedBinary, Term};
use serde::{
ser::{self, Serialize},
serde_if_integer128,
};
use serde::ser::{self, Serialize};

#[inline]
/// Converts a native Rust type into a native Elixir term. See [conversion table](https://github.com/sunny-g/serde_rustler/tree/master/serde_rustler#conversion-table) for details about serialization behavior.
Expand Down Expand Up @@ -147,15 +144,12 @@ impl<'a> ser::Serializer for Serializer<'a> {
Ok(res)
}

// TODO: update rustler to support u128 and i128
serde_if_integer128! {
fn serialize_i128(self, _v: i128) -> Result<Self::Ok, Self::Error> {
unimplemented!("awaiting rustler support for i128s")
}
fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
Ok(v.encode(self.env))
}

fn serialize_u128(self, _v: u128) -> Result<Self::Ok, Self::Error> {
unimplemented!("awaiting rustler support for u128s")
}
fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
Ok(v.encode(self.env))
}

#[inline]
Expand Down
10 changes: 4 additions & 6 deletions rustler_tests/test/serde_rustler_tests_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,10 @@ defmodule SerdeRustlerTests.NifTest do
run_tests("i64 (max)", 9_223_372_036_854_775_807, ctx)
end

@tag :skip
test "i128", ctx do
run_tests("i128 (min)", 0, ctx)
run_tests("128 (0)", 0, ctx)
run_tests("i128 (max)", 0, ctx)
run_tests("i128 (min)", -170141183460469231731687303715884105728, ctx)
run_tests("i128 (0)", 0, ctx)
run_tests("i128 (max)", 170141183460469231731687303715884105727, ctx)
end

test "u8", ctx do
Expand All @@ -136,10 +135,9 @@ defmodule SerdeRustlerTests.NifTest do
run_tests("u64 (max)", 18_446_744_073_709_551_615, ctx)
end

@tag :skip
test "u128", ctx do
run_tests("u128 (min)", 0, ctx)
run_tests("u128 (max)", 0, ctx)
run_tests("u128 (max)", 340282366920938463463374607431768211455, ctx)
end

test "f32", ctx do
Expand Down

0 comments on commit f3ca390

Please sign in to comment.