forked from mongodb/bson-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disallow serde serialize/deserialize unsigned integer types (mongodb#72)
* [mongodb#70] Disallow serde serialize/deserialize unsigned integer types * add compat mod for backward compatibility * Added breaking changes in README
- Loading branch information
Showing
10 changed files
with
187 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//! Backward compatibility | ||
|
||
pub mod u2f; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//! Convert unsigned types to/from `Bson::FloatingPoint` | ||
|
||
use serde::{Serializer, Deserializer, Deserialize}; | ||
|
||
/// Converts primitive unsigned types to `f64` | ||
pub trait ToF64 { | ||
/// Converts to `f64` value | ||
fn to_f64(&self) -> f64; | ||
} | ||
|
||
impl ToF64 for u8 { | ||
fn to_f64(&self) -> f64 { | ||
*self as f64 | ||
} | ||
} | ||
|
||
impl ToF64 for u16 { | ||
fn to_f64(&self) -> f64 { | ||
*self as f64 | ||
} | ||
} | ||
|
||
impl ToF64 for u32 { | ||
fn to_f64(&self) -> f64 { | ||
*self as f64 | ||
} | ||
} | ||
|
||
impl ToF64 for u64 { | ||
fn to_f64(&self) -> f64 { | ||
*self as f64 | ||
} | ||
} | ||
|
||
/// Serialize unsigned types to `Bson::FloatingPoint` | ||
pub fn serialize<T, S>(v: &T, s: S) -> Result<S::Ok, S::Error> | ||
where T: ToF64, | ||
S: Serializer | ||
{ | ||
s.serialize_f64(v.to_f64()) | ||
} | ||
|
||
/// Converts from `f64` value | ||
pub trait FromF64 { | ||
/// Converts from `f64` value | ||
fn from_f64(v: f64) -> Self; | ||
} | ||
|
||
impl FromF64 for u8 { | ||
fn from_f64(v: f64) -> u8 { | ||
v as u8 | ||
} | ||
} | ||
|
||
impl FromF64 for u16 { | ||
fn from_f64(v: f64) -> u16 { | ||
v as u16 | ||
} | ||
} | ||
|
||
impl FromF64 for u32 { | ||
fn from_f64(v: f64) -> u32 { | ||
v as u32 | ||
} | ||
} | ||
|
||
impl FromF64 for u64 { | ||
fn from_f64(v: f64) -> u64 { | ||
v as u64 | ||
} | ||
} | ||
|
||
/// Deserialize unsigned types to `Bson::FloatingPoint` | ||
pub fn deserialize<'de, T, D>(d: D) -> Result<T, D::Error> | ||
where D: Deserializer<'de>, | ||
T: FromF64 | ||
{ | ||
f64::deserialize(d).map(T::from_f64) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,3 +71,4 @@ mod encoder; | |
mod decoder; | ||
pub mod ordered; | ||
pub mod decimal128; | ||
pub mod compat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters