Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Add support for i128 and u128
Browse files Browse the repository at this point in the history
i128 and u128 can now be serialized and deserialized.  Support is
conditionalized on the disabled-by-default `i128` feature flag, and
requires rustc 1.26.0 or later.  The minimum required version of Serde
is now 1.0.60.

Fixes #108
  • Loading branch information
asomers committed Oct 11, 2018
1 parent 29d54cc commit 443577d
Show file tree
Hide file tree
Showing 10 changed files with 644 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Expand Up @@ -8,6 +8,9 @@ matrix:
- rust: 1.17.0
script: cargo check
- rust: 1.21.0
script:
- cargo build
- cargo test
- rust: nightly
env: CLIPPY
script: |
Expand All @@ -18,3 +21,4 @@ matrix:
script:
- cargo build
- cargo test
- caro test --features "i128"
6 changes: 5 additions & 1 deletion Cargo.toml
Expand Up @@ -10,15 +10,19 @@ readme = "README.md"
keywords = ["yaml", "serde"]

[dependencies]
cfg-if = "0.1"
dtoa = "0.4"
linked-hash-map = "0.5"
serde = "1.0"
serde = "1.0.60"
yaml-rust = "0.4"

[dev-dependencies]
serde_derive = "1.0"
unindent = "0.1"
version-sync = "0.5"

[features]
i128 = []

[badges]
travis-ci = { repository = "dtolnay/serde-yaml" }
44 changes: 44 additions & 0 deletions src/de.rs
Expand Up @@ -565,9 +565,19 @@ where
if let Ok(n) = v.parse() {
return visitor.visit_u64(n);
}
serde_if_integer128! {
if let Ok(n) = v.parse() {
return visitor.visit_u128(n);
}
}
if let Ok(n) = v.parse() {
return visitor.visit_i64(n);
}
serde_if_integer128! {
if let Ok(n) = v.parse() {
return visitor.visit_i128(n);
}
}
match v.trim_left_matches('+') {
".inf" | ".Inf" | ".INF" => return visitor.visit_f64(f64::INFINITY),
_ => (),
Expand Down Expand Up @@ -685,6 +695,23 @@ impl<'de, 'a, 'r> de::Deserializer<'de> for &'r mut Deserializer<'a> {
self.deserialize_scalar(visitor)
}

serde_if_integer128! {
#[cfg(feature = "i128")]
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.deserialize_scalar(visitor)
}
#[cfg(not(feature = "i128"))]
fn deserialize_i128<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
Err(de::Error::custom("i128 is not supported. Enable the `i128` feature of `serde-yaml`"))
}
}

fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
Expand Down Expand Up @@ -713,6 +740,23 @@ impl<'de, 'a, 'r> de::Deserializer<'de> for &'r mut Deserializer<'a> {
self.deserialize_scalar(visitor)
}

serde_if_integer128! {
#[cfg(feature = "i128")]
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.deserialize_scalar(visitor)
}
#[cfg(not(feature = "i128"))]
fn deserialize_u128<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
Err(de::Error::custom("u128 is not supported. Enable the `i128` feature of `serde-yaml`"))
}
}

fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Expand Up @@ -59,6 +59,16 @@
//! assert_eq!(point, deserialized_point);
//! # }
//! ```
//!
//! ## 128 bit numbers
//!
//! Support for `i128` and `u128` on Rust toolchains after `1.26.0` is enabled
//! through the `i128` feature. Add the following to `Cargo.toml`:
//!
//! ```toml,ignore
//! [dependencies.serde-yaml]
//! features = ["i128"]
//! ```

#![doc(html_root_url = "https://docs.rs/serde_yaml/0.8.5")]
#![deny(missing_docs)]
Expand Down Expand Up @@ -88,6 +98,8 @@
result_unwrap_used,
))]

#[macro_use]
extern crate cfg_if;
extern crate dtoa;
extern crate linked_hash_map;
#[macro_use]
Expand Down

0 comments on commit 443577d

Please sign in to comment.