Skip to content
This repository was archived by the owner on Aug 15, 2021. It is now read-only.

Support 128-bit integers #145

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,33 @@ where
Ok(BigEndian::read_u64(&buf))
}

fn parse_bigint<V>(&mut self, signed: bool, visitor: V) -> Result<V::Value>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to separate this as a function rather than just keeping it down?

where
V: de::Visitor<'de>,
{
let desc = match self.peek()? {
Some(desc) => desc,
None => return Err(self.error(ErrorCode::EofWhileParsingValue)),
};
let ty = desc >> 5;
if ty != 2 {
return self.parse_value(visitor);
}
self.consume();
let len = desc & 0x1f;
if len > 16 {
return Err(self.error(ErrorCode::LengthOutOfRange));
}
let mut buf = [0; 16];
self.read.read_into(&mut buf[usize::from(16 - len)..])?;
let num = BigEndian::read_u128(&buf);
if !signed {
visitor.visit_u128(num)
} else {
visitor.visit_i128(-1 - num as i128)
}
}

fn parse_bytes<V>(&mut self, len: usize, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
Expand Down Expand Up @@ -743,8 +770,7 @@ where
}
0xfc..=0xfe => Err(self.error(ErrorCode::UnassignedCode)),
0xff => Err(self.error(ErrorCode::UnexpectedCode)),

_ => unreachable!(),
_ => unreachable!(), // Remove this once minimum supported rustc version is 1.33.0.
}
}
}
Expand Down Expand Up @@ -848,13 +874,35 @@ where
}
}

#[inline]
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.deserialize_u128(visitor)
}

#[inline]
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
match self.peek()? {
Some(tag @ 0xc2..=0xc3) => {
self.consume();
self.parse_bigint(tag == 0xc3 /* signed */, visitor)
}
_ => self.parse_value(visitor),
}
}

#[inline]
fn is_human_readable(&self) -> bool {
false
}

serde::forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string unit
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string unit
unit_struct seq tuple tuple_struct map struct identifier ignored_any
bytes byte_buf
}
Expand Down
32 changes: 20 additions & 12 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ where
}
}

#[inline]
fn write_u128(&mut self, tag: u8, value: u128) -> Result<()> {
let len = 16 - (value.leading_zeros() >> 3);
if len <= 8 {
self.write_u64(tag - 2, value as u64)
} else {
let mut buf = [0u8; 2 + 16];
BigEndian::write_u128(&mut buf[2..], value);
let hdr_offset = 16 - len as usize;
buf[hdr_offset] = 6 << 5 | tag;
buf[hdr_offset + 1] = 2 << 5 | len as u8;
self.writer
.write_all(&buf[hdr_offset..])
.map_err(|e| e.into())
}
}

#[inline]
fn serialize_collection<'a>(
&'a mut self,
Expand Down Expand Up @@ -263,15 +280,9 @@ where
#[inline]
fn serialize_i128(self, value: i128) -> Result<()> {
if value < 0 {
if -(value + 1) > i128::from(u64::max_value()) {
return Err(Error::message("The number can't be stored in CBOR"));
}
self.write_u64(1, -(value + 1) as u64)
self.write_u128(3, -(value + 1) as u128)
} else {
if value > i128::from(u64::max_value()) {
return Err(Error::message("The number can't be stored in CBOR"));
}
self.write_u64(0, value as u64)
self.write_u128(2, value as u128)
}
}

Expand All @@ -297,10 +308,7 @@ where

#[inline]
fn serialize_u128(self, value: u128) -> Result<()> {
if value > u128::from(u64::max_value()) {
return Err(Error::message("The number can't be stored in CBOR"));
}
self.write_u64(0, value as u64)
self.write_u128(2, value)
}

#[inline]
Expand Down
8 changes: 8 additions & 0 deletions src/value/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ impl<'de> de::Deserialize<'de> for Value {
Ok(Value::Integer(v.into()))
}

#[inline]
fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Value::Unsigned(v))
}

#[inline]
fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E>
where
Expand Down
35 changes: 22 additions & 13 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ pub enum Value {
Null,
/// Represents a boolean value.
Bool(bool),
/// Integer CBOR numbers.
///
/// The biggest value that can be represented is 2^64 - 1.
/// While the smallest value is -2^64.
/// Values outside this range can't be serialized
/// and will cause an error.
/// Signed integer CBOR numbers.
Integer(i128),
/// Unsigned integer CBOR numbers.
Unsigned(u128),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like to have two overlapping integer variants (again) because a postive number can now be stored in two different ways.
Ist there something special about 128-bits that you need u128 numbers that do not fit into i128?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there something special about 128-bits that you need u128 numbers that do not fit into i128?

The only issue is with the canonical sort order. Before, i128 could record the sign and magnitude of the largest supported number. Since i128 is the largest data type, the sign isn't recorded independently of magnitude.

/// Represents a floating point value.
Float(f64),
/// Represents a byte string.
Expand Down Expand Up @@ -85,6 +82,9 @@ impl Ord for Value {
}
match (self, other) {
(Integer(a), Integer(b)) => a.abs().cmp(&b.abs()),
(Unsigned(a), Unsigned(b)) => a.cmp(&b),
(Integer(a), Unsigned(b)) => (a.abs() as u128).cmp(&b),
(Unsigned(a), Integer(b)) => a.cmp(&(b.abs() as u128)),
(Bytes(a), Bytes(b)) if a.len() != b.len() => a.len().cmp(&b.len()),
(Text(a), Text(b)) if a.len() != b.len() => a.len().cmp(&b.len()),
(Array(a), Array(b)) if a.len() != b.len() => a.len().cmp(&b.len()),
Expand Down Expand Up @@ -115,12 +115,12 @@ impl_from!(Value::Integer, i8);
impl_from!(Value::Integer, i16);
impl_from!(Value::Integer, i32);
impl_from!(Value::Integer, i64);
// i128 omitted because not all numbers fit in CBOR serialization
impl_from!(Value::Integer, u8);
impl_from!(Value::Integer, u16);
impl_from!(Value::Integer, u32);
impl_from!(Value::Integer, u64);
// u128 omitted because not all numbers fit in CBOR serialization
impl_from!(Value::Integer, i128);
impl_from!(Value::Unsigned, u8);
impl_from!(Value::Unsigned, u16);
impl_from!(Value::Unsigned, u32);
impl_from!(Value::Unsigned, u64);
impl_from!(Value::Unsigned, u128);
impl_from!(Value::Float, f32);
impl_from!(Value::Float, f64);
impl_from!(Value::Bytes, Vec<u8>);
Expand All @@ -136,12 +136,21 @@ impl Value {
Null => 7,
Bool(_) => 7,
Integer(v) => {
if *v >= 0 {
if v.abs() > i128::from(u64::max_value()) {
6
} else if *v >= 0 {
0
} else {
1
}
}
Unsigned(v) => {
if *v > u128::from(u64::max_value()) {
6
} else {
0
}
}
Float(_) => 7,
Bytes(_) => 2,
Text(_) => 3,
Expand Down
14 changes: 10 additions & 4 deletions src/value/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl serde::Serialize for Value {
{
match *self {
Value::Integer(v) => serializer.serialize_i128(v),
Value::Unsigned(v) => serializer.serialize_u128(v),
Value::Bytes(ref v) => serializer.serialize_bytes(&v),
Value::Text(ref v) => serializer.serialize_str(&v),
Value::Array(ref v) => v.serialize(serializer),
Expand Down Expand Up @@ -78,22 +79,27 @@ impl serde::Serializer for Serializer {

#[inline]
fn serialize_u8(self, value: u8) -> Result<Value, Error> {
self.serialize_u64(u64::from(value))
self.serialize_u128(u128::from(value))
}

#[inline]
fn serialize_u16(self, value: u16) -> Result<Value, Error> {
self.serialize_u64(u64::from(value))
self.serialize_u128(u128::from(value))
}

#[inline]
fn serialize_u32(self, value: u32) -> Result<Value, Error> {
self.serialize_u64(u64::from(value))
self.serialize_u128(u128::from(value))
}

#[inline]
fn serialize_u64(self, value: u64) -> Result<Value, Error> {
Ok(Value::Integer(value.into()))
self.serialize_u128(u128::from(value))
}

#[inline]
fn serialize_u128(self, value: u128) -> Result<Value, Error> {
Ok(Value::Unsigned(value))
}

#[inline]
Expand Down
20 changes: 19 additions & 1 deletion tests/std_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,23 @@ mod std_tests {
-18446744073709551616i128,
"3BFFFFFFFFFFFFFFFF"
);
testcase!(test_u128, u128, 17, "11");
testcase!(
test_i128_c,
i128,
-18446744073709551617i128,
"C349010000000000000000"
);
testcase!(test_u128_a, u128, 17, "11");
testcase!(
test_u128_b,
u128,
u128::max_value(),
"C250FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
);
testcase!(
test_u128_c,
u128,
u128::from(u64::max_value()),
"1BFFFFFFFFFFFFFFFF"
);
}