Skip to content

Commit

Permalink
Make i128 support automatic for supporting Rustc versions
Browse files Browse the repository at this point in the history
Uses autocfg
  • Loading branch information
dhardy authored and TyOverby committed Jan 28, 2019
1 parent 0c519bb commit 76b4439
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
9 changes: 8 additions & 1 deletion Cargo.toml
Expand Up @@ -11,6 +11,7 @@ documentation = "https://docs.rs/bincode"
readme = "./readme.md"
categories = ["network-programming"]
keywords = ["binary", "encode", "decode", "serialize", "deserialize"]
build = "build.rs"

license = "MIT"
description = "A binary serialization / deserialization strategy that uses Serde for transforming structs into bytes and vice versa!"
Expand All @@ -23,8 +24,14 @@ serde = "^1.0.63"
serde_bytes = "^0.10.3"
serde_derive = "^1.0.27"

[build-dependencies]
autocfg = "0.1"

[features]
i128 = ["byteorder/i128"]
# This feature is no longer used and is DEPRECATED. This crate now
# automatically enables i128 support for Rust compilers that support it. The
# feature will be removed if and when a new major version is released.
i128 = []

[badges]
travis-ci = { repository = "TyOverby/bincode" }
8 changes: 8 additions & 0 deletions build.rs
@@ -0,0 +1,8 @@
extern crate autocfg;

fn main() {
autocfg::rerun_path(file!());

let ac = autocfg::new();
ac.emit_has_type("i128");
}
12 changes: 6 additions & 6 deletions src/de/mod.rs
Expand Up @@ -108,29 +108,29 @@ where
impl_nums!(f32, deserialize_f32, visit_f32, read_f32);
impl_nums!(f64, deserialize_f64, visit_f64, read_f64);

#[cfg(feature = "i128")]
#[cfg(has_i128)]
impl_nums!(u128, deserialize_u128, visit_u128, read_u128);

#[cfg(feature = "i128")]
#[cfg(has_i128)]
impl_nums!(i128, deserialize_i128, visit_i128, read_i128);

serde_if_integer128! {
#[cfg(not(feature = "i128"))]
#[cfg(not(has_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`"))
Err(DeError::custom("u128 is not supported. Use Rustc ≥ 1.26."))
}

#[cfg(not(feature = "i128"))]
#[cfg(not(has_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`"))
Err(DeError::custom("i128 is not supported. Use Rustc ≥ 1.26."))
}
}

Expand Down
10 changes: 2 additions & 8 deletions src/lib.rs
Expand Up @@ -22,14 +22,8 @@
//!
//! ### 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"]
//! ```
//! Support for `i128` and `u128` is automatically enabled on Rust toolchains
//! greater than or equal to `1.26.0`.

#![crate_name = "bincode"]
#![crate_type = "rlib"]
Expand Down
12 changes: 6 additions & 6 deletions src/ser/mod.rs
Expand Up @@ -88,31 +88,31 @@ 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)
}

#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn serialize_u128(self, v: u128) -> Result<()> {
self.writer.write_u128::<O::Endian>(v).map_err(Into::into)
}

#[cfg(feature = "i128")]
#[cfg(has_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"))]
#[cfg(not(has_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`"))
Err(Error::custom("u128 is not supported. Use Rustc ≥ 1.26."))
}

#[cfg(not(feature = "i128"))]
#[cfg(not(has_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`"))
Err(Error::custom("i128 is not supported. Use Rustc ≥ 1.26."))
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test.rs
Expand Up @@ -72,7 +72,7 @@ fn test_numbers() {
the_same(5f64);
}

#[cfg(feature = "i128")]
#[cfg(has_i128)]
#[test]
fn test_numbers_128bit() {
// unsigned positive
Expand Down

0 comments on commit 76b4439

Please sign in to comment.