mirror of https://git.sr.ht/~stygianentity/bincode
add support for 128bit numbers
This commit is contained in:
parent
10bdbbff33
commit
2ef9a06894
|
|
@ -3,3 +3,7 @@ rust:
|
||||||
- stable
|
- stable
|
||||||
- beta
|
- beta
|
||||||
- nightly
|
- nightly
|
||||||
|
|
||||||
|
script:
|
||||||
|
- cargo test
|
||||||
|
- cargo test --features "i128"
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,11 @@ description = "A binary serialization / deserialization strategy that uses Serde
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
byteorder = "1.2.0"
|
byteorder = "1.2.0"
|
||||||
serde = "^1.0.27"
|
serde = "^1.0.63"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
serde_bytes = "^0.10.3"
|
serde_bytes = "^0.10.3"
|
||||||
serde_derive = "^1.0.27"
|
serde_derive = "^1.0.27"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
i128 = ["byteorder/i128"]
|
||||||
|
|
|
||||||
|
|
@ -108,6 +108,31 @@ where
|
||||||
impl_nums!(f32, deserialize_f32, visit_f32, read_f32);
|
impl_nums!(f32, deserialize_f32, visit_f32, read_f32);
|
||||||
impl_nums!(f64, deserialize_f64, visit_f64, read_f64);
|
impl_nums!(f64, deserialize_f64, visit_f64, read_f64);
|
||||||
|
|
||||||
|
#[cfg(feature = "i128")]
|
||||||
|
impl_nums!(u128, deserialize_u128, visit_u128, read_u128);
|
||||||
|
|
||||||
|
#[cfg(feature = "i128")]
|
||||||
|
impl_nums!(i128, deserialize_i128, visit_i128, read_i128);
|
||||||
|
|
||||||
|
serde_if_integer128! {
|
||||||
|
#[cfg(not(feature = "i128"))]
|
||||||
|
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
|
||||||
|
where
|
||||||
|
V: serde::de::Visitor<'de>
|
||||||
|
{
|
||||||
|
let _ = visitor;
|
||||||
|
Err(DeError::custom("u128 is not supported. Enable the `i128` feature of `bincode`"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "i128"))]
|
||||||
|
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
|
||||||
|
where
|
||||||
|
V: serde::de::Visitor<'de>
|
||||||
|
{
|
||||||
|
let _ = visitor;
|
||||||
|
Err(DeError::custom("i128 is not supported. Enable the `i128` feature of `bincode`"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
|
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
|
||||||
|
|
|
||||||
12
src/lib.rs
12
src/lib.rs
|
|
@ -19,12 +19,24 @@
|
||||||
//! assert_eq!(target, decoded);
|
//! assert_eq!(target, decoded);
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
|
//!
|
||||||
|
//! ### 128bit numbers
|
||||||
|
//!
|
||||||
|
//! Support for `i128` and `u128` on Rust toolchains after `1.26.0` is
|
||||||
|
//! enabled through the `i128` feature. Add the following to your
|
||||||
|
//! `Cargo.toml`:
|
||||||
|
//!
|
||||||
|
//! ```toml,ignore
|
||||||
|
//! [dependencies.bincode]
|
||||||
|
//! features = ["i128"]
|
||||||
|
//! ```
|
||||||
|
|
||||||
#![crate_name = "bincode"]
|
#![crate_name = "bincode"]
|
||||||
#![crate_type = "rlib"]
|
#![crate_type = "rlib"]
|
||||||
#![crate_type = "dylib"]
|
#![crate_type = "dylib"]
|
||||||
|
|
||||||
extern crate byteorder;
|
extern crate byteorder;
|
||||||
|
#[macro_use]
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,34 @@ impl<'a, W: Write, O: Options> serde::Serializer for &'a mut Serializer<W, O> {
|
||||||
self.writer.write_i64::<O::Endian>(v).map_err(Into::into)
|
self.writer.write_i64::<O::Endian>(v).map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "i128")]
|
||||||
|
fn serialize_u128(self, v: u128) -> Result<()> {
|
||||||
|
self.writer.write_u128::<O::Endian>(v).map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "i128")]
|
||||||
|
fn serialize_i128(self, v: i128) -> Result<()> {
|
||||||
|
self.writer.write_i128::<O::Endian>(v).map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
serde_if_integer128! {
|
||||||
|
#[cfg(not(feature = "i128"))]
|
||||||
|
fn serialize_u128(self, v: u128) -> Result<()> {
|
||||||
|
use serde::ser::Error;
|
||||||
|
|
||||||
|
let _ = v;
|
||||||
|
Err(Error::custom("u128 is not supported. Enable the `i128` feature of `bincode`"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "i128"))]
|
||||||
|
fn serialize_i128(self, v: i128) -> Result<()> {
|
||||||
|
use serde::ser::Error;
|
||||||
|
|
||||||
|
let _ = v;
|
||||||
|
Err(Error::custom("i128 is not supported. Enable the `i128` feature of `bincode`"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn serialize_f32(self, v: f32) -> Result<()> {
|
fn serialize_f32(self, v: f32) -> Result<()> {
|
||||||
self.writer.write_f32::<O::Endian>(v).map_err(Into::into)
|
self.writer.write_f32::<O::Endian>(v).map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
@ -283,6 +311,16 @@ impl<'a, O: Options> serde::Serializer for &'a mut SizeChecker<O> {
|
||||||
self.add_value(v)
|
self.add_value(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
serde_if_integer128! {
|
||||||
|
fn serialize_u128(self, v: u128) -> Result<()> {
|
||||||
|
self.add_value(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_i128(self, v: i128) -> Result<()> {
|
||||||
|
self.add_value(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn serialize_f32(self, v: f32) -> Result<()> {
|
fn serialize_f32(self, v: f32) -> Result<()> {
|
||||||
self.add_value(v)
|
self.add_value(v)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,20 @@ fn test_numbers() {
|
||||||
the_same(5f64);
|
the_same(5f64);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "i128")]
|
||||||
|
#[test]
|
||||||
|
fn test_numbers_128bit() {
|
||||||
|
// unsigned positive
|
||||||
|
the_same(5u128);
|
||||||
|
the_same(u128::max_value());
|
||||||
|
// signed positive
|
||||||
|
the_same(5i128);
|
||||||
|
the_same(i128::max_value());
|
||||||
|
// signed negative
|
||||||
|
the_same(-5i128);
|
||||||
|
the_same(i128::min_value());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_string() {
|
fn test_string() {
|
||||||
the_same("".to_string());
|
the_same("".to_string());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue