added fixint support
This commit is contained in:
parent
3a8b622ae7
commit
a48de88630
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bipack_ru"
|
name = "bipack_ru"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
description = "binary size-effective format used in Divan smart contracts, wasm bindings, network protocols, etc."
|
description = "binary size-effective format used in Divan smart contracts, wasm bindings, network protocols, etc."
|
||||||
|
@ -44,6 +44,10 @@ The autodoc documentation is good enough already, so we do not repeat it here no
|
|||||||
|
|
||||||
- just ad this package to your dependencies, it is on crates.io.
|
- just ad this package to your dependencies, it is on crates.io.
|
||||||
|
|
||||||
|
## Big thanks
|
||||||
|
|
||||||
|
- to https://github.com/jamesmunns for the brilliant postcard format which was used as a model.
|
||||||
|
|
||||||
# License
|
# License
|
||||||
|
|
||||||
For compliance with other modules this work is provided under APACHE 2.0 license a copy of which is included in the file `LICENSE`.
|
For compliance with other modules this work is provided under APACHE 2.0 license a copy of which is included in the file `LICENSE`.
|
@ -18,6 +18,7 @@ pub fn create_contrail(data: &[u8]) -> Vec<u8> {
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::contrail::{create_contrail, is_valid_contrail};
|
use crate::contrail::{create_contrail, is_valid_contrail};
|
||||||
|
|
||||||
|
@ -260,7 +260,11 @@ impl<'de, 'a> MapAccess<'de> for SimpleMap<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
// use std::collections::{HashMap, HashSet};
|
||||||
|
// use std::fmt::Debug;
|
||||||
|
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
|
189
src/fixint.rs
Normal file
189
src/fixint.rs
Normal file
@ -0,0 +1,189 @@
|
|||||||
|
//! # Fixed Size Integers
|
||||||
|
//!
|
||||||
|
//! In some cases, the use of variably length encoded data may not be
|
||||||
|
//! preferrable. These modules, for use with `#[serde(with = ...)]`
|
||||||
|
//! "opt out" of variable length encoding.
|
||||||
|
//!
|
||||||
|
//! Support explicitly not provided for `usize` or `isize`, as
|
||||||
|
//! these types would not be portable between systems of different
|
||||||
|
//! pointer widths.
|
||||||
|
//!
|
||||||
|
//! Although all data in Postcard is typically encoded in little-endian
|
||||||
|
//! order, these modules provide a choice to the user to encode the data
|
||||||
|
//! in either little or big endian form, which may be useful for zero-copy
|
||||||
|
//! applications.
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize, Serializer};
|
||||||
|
|
||||||
|
/// Use with the `#[serde(with = "bipack_ru::fixint::le")]` field attribute.
|
||||||
|
/// Disables smartint serialization/deserialization for the specified integer
|
||||||
|
/// field. The integer will always be serialized in the same way as a fixed
|
||||||
|
/// size array, in **Little Endian** order on the wire.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde::Serialize;
|
||||||
|
/// #[derive(Serialize)]
|
||||||
|
/// pub struct DefinitelyLittleEndian {
|
||||||
|
/// #[serde(with = "bipack_ru::fixint::le")]
|
||||||
|
/// x: u16,
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub mod le {
|
||||||
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||||
|
|
||||||
|
use super::LE;
|
||||||
|
|
||||||
|
/// Serialize the integer value as a little-endian fixed-size array.
|
||||||
|
pub fn serialize<S, T>(val: &T, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
T: Copy,
|
||||||
|
LE<T>: Serialize,
|
||||||
|
{
|
||||||
|
LE(*val).serialize(serializer)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deserialize the integer value from a little-endian fixed-size array.
|
||||||
|
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
LE<T>: Deserialize<'de>,
|
||||||
|
{
|
||||||
|
LE::<T>::deserialize(deserializer).map(|x| x.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Use with the `#[serde(with = "bipack_ru::fixint::be")]` field attribute.
|
||||||
|
/// Disables smartint serialization/deserialization for the specified integer
|
||||||
|
/// field. The integer will always be serialized in the same way as a fixed
|
||||||
|
/// size array, in **Big Endian** order on the wire.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde::Serialize;
|
||||||
|
/// #[derive(Serialize)]
|
||||||
|
/// pub struct DefinitelyBigEndian {
|
||||||
|
/// #[serde(with = "bipack_ru::fixint::be")]
|
||||||
|
/// x: u16,
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub mod be {
|
||||||
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||||
|
|
||||||
|
use super::BE;
|
||||||
|
|
||||||
|
/// Serialize the integer value as a big-endian fixed-size array.
|
||||||
|
pub fn serialize<S, T>(val: &T, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
T: Copy,
|
||||||
|
BE<T>: Serialize,
|
||||||
|
{
|
||||||
|
BE(*val).serialize(serializer)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deserialize the integer value from a big-endian fixed-size array.
|
||||||
|
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
BE<T>: Deserialize<'de>,
|
||||||
|
{
|
||||||
|
BE::<T>::deserialize(deserializer).map(|x| x.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub struct LE<T>(T);
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub struct BE<T>(T);
|
||||||
|
|
||||||
|
macro_rules! impl_fixint {
|
||||||
|
($( $int:ty ),*) => {
|
||||||
|
$(
|
||||||
|
impl Serialize for LE<$int> {
|
||||||
|
#[inline]
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
self.0.to_le_bytes().serialize(serializer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for LE<$int> {
|
||||||
|
#[inline]
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
<_ as Deserialize>::deserialize(deserializer)
|
||||||
|
.map(<$int>::from_le_bytes)
|
||||||
|
.map(Self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Serialize for BE<$int> {
|
||||||
|
#[inline]
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
self.0.to_be_bytes().serialize(serializer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for BE<$int> {
|
||||||
|
#[inline]
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
<_ as Deserialize>::deserialize(deserializer)
|
||||||
|
.map(<$int>::from_be_bytes)
|
||||||
|
.map(Self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_fixint![i16, i32, i64, i128, u16, u32, u64, u128];
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use crate::de::from_bytes;
|
||||||
|
use crate::ser::to_bytes;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_little_endian() {
|
||||||
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
|
||||||
|
pub struct DefinitelyLE {
|
||||||
|
#[serde(with = "crate::fixint::le")]
|
||||||
|
x: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
let input = DefinitelyLE { x: 0xABCD };
|
||||||
|
let serialized = to_bytes(&input).unwrap();
|
||||||
|
assert_eq!(serialized, &[0xCD, 0xAB]);
|
||||||
|
|
||||||
|
let deserialized: DefinitelyLE = from_bytes(&serialized).unwrap();
|
||||||
|
assert_eq!(deserialized, input);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_big_endian() {
|
||||||
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
|
||||||
|
pub struct DefinitelyBE {
|
||||||
|
#[serde(with = "crate::fixint::be")]
|
||||||
|
x: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
let input = DefinitelyBE { x: 0xABCD };
|
||||||
|
let serialized = to_bytes(&input).unwrap();
|
||||||
|
assert_eq!(serialized, &[0xAB, 0xCD]);
|
||||||
|
|
||||||
|
let deserialized: DefinitelyBE = from_bytes(&serialized).unwrap();
|
||||||
|
assert_eq!(deserialized, input);
|
||||||
|
}
|
||||||
|
}
|
11
src/lib.rs
11
src/lib.rs
@ -118,11 +118,12 @@ pub mod bipack_source;
|
|||||||
pub mod bipack_sink;
|
pub mod bipack_sink;
|
||||||
pub mod tools;
|
pub mod tools;
|
||||||
pub mod bipack;
|
pub mod bipack;
|
||||||
mod error;
|
pub mod error;
|
||||||
mod ser;
|
pub mod ser;
|
||||||
mod de;
|
pub mod de;
|
||||||
mod crc;
|
pub mod crc;
|
||||||
mod contrail;
|
pub mod contrail;
|
||||||
|
pub mod fixint;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
58
src/ser.rs
58
src/ser.rs
@ -1,12 +1,7 @@
|
|||||||
use std::collections::HashMap;
|
|
||||||
use std::string::FromUtf8Error;
|
|
||||||
|
|
||||||
use serde::{ser, Serialize};
|
use serde::{ser, Serialize};
|
||||||
|
|
||||||
use crate::bipack_sink::{BipackSink, IntoU64};
|
use crate::bipack_sink::{BipackSink, IntoU64};
|
||||||
use crate::bipack_source::{BipackError, BipackSource, SliceSource};
|
|
||||||
use crate::error::{Error, Result};
|
use crate::error::{Error, Result};
|
||||||
use crate::tools::{to_dump, to_hex};
|
|
||||||
|
|
||||||
pub struct Serializer {
|
pub struct Serializer {
|
||||||
// This string starts empty and JSON is appended as values are serialized.
|
// This string starts empty and JSON is appended as values are serialized.
|
||||||
@ -159,7 +154,8 @@ impl<'a> ser::Serializer for &'a mut Serializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
|
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
|
||||||
self.serialize_seq(Some(len))
|
Ok(self)
|
||||||
|
// self.serialize_seq(Some(len))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_struct(
|
fn serialize_tuple_struct(
|
||||||
@ -342,8 +338,20 @@ impl<'a> ser::SerializeStructVariant for &'a mut Serializer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn test_struct() -> std::result::Result<(), BipackError> {
|
mod test {
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::string::FromUtf8Error;
|
||||||
|
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
use crate::bipack_source::{BipackError, BipackSource, SliceSource};
|
||||||
|
use crate::error;
|
||||||
|
use crate::ser::to_bytes;
|
||||||
|
use crate::tools::{to_dump, to_hex};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_struct() -> std::result::Result<(), BipackError> {
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Test {
|
struct Test {
|
||||||
int: u32,
|
int: u32,
|
||||||
@ -364,10 +372,10 @@ fn test_struct() -> std::result::Result<(), BipackError> {
|
|||||||
assert_eq!(test.seq[0], src.get_str()?);
|
assert_eq!(test.seq[0], src.get_str()?);
|
||||||
assert_eq!(test.seq[1], src.get_str()?);
|
assert_eq!(test.seq[1], src.get_str()?);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_enum() -> std::result::Result<(), FromUtf8Error> {
|
fn test_enum() -> std::result::Result<(), FromUtf8Error> {
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
enum E {
|
enum E {
|
||||||
Unit,
|
Unit,
|
||||||
@ -378,19 +386,19 @@ fn test_enum() -> std::result::Result<(), FromUtf8Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let u = E::Unit;
|
let u = E::Unit;
|
||||||
println!("u:{}",to_dump(to_bytes(&u).unwrap().as_slice()));
|
println!("u:{}", to_dump(to_bytes(&u).unwrap().as_slice()));
|
||||||
assert_eq!("00",to_hex(to_bytes(&u).unwrap())?);
|
assert_eq!("00", to_hex(to_bytes(&u).unwrap())?);
|
||||||
let u2 = E::Unit2;
|
let u2 = E::Unit2;
|
||||||
println!("u:{}",to_dump(to_bytes(&u2).unwrap().as_slice()));
|
println!("u:{}", to_dump(to_bytes(&u2).unwrap().as_slice()));
|
||||||
let nt = E::Newtype(17);
|
let nt = E::Newtype(17);
|
||||||
println!("u:{}",to_dump(to_bytes(&nt).unwrap().as_slice()));
|
println!("u:{}", to_dump(to_bytes(&nt).unwrap().as_slice()));
|
||||||
assert_eq!("08 44",to_hex(to_bytes(&nt).unwrap())?);
|
assert_eq!("08 44", to_hex(to_bytes(&nt).unwrap())?);
|
||||||
let t = E::Tuple(7,17);
|
let t = E::Tuple(7, 17);
|
||||||
println!("u:{}",to_dump(to_bytes(&t).unwrap().as_slice()));
|
println!("u:{}", to_dump(to_bytes(&t).unwrap().as_slice()));
|
||||||
assert_eq!("0c 1c 44",to_hex(to_bytes(&t).unwrap())?);
|
assert_eq!("0c 1c 44", to_hex(to_bytes(&t).unwrap())?);
|
||||||
let t = E::Struct { a: 17 };
|
let t = E::Struct { a: 17 };
|
||||||
println!("u:{}",to_dump(to_bytes(&t).unwrap().as_slice()));
|
println!("u:{}", to_dump(to_bytes(&t).unwrap().as_slice()));
|
||||||
assert_eq!("10 44",to_hex(to_bytes(&t).unwrap())?);
|
assert_eq!("10 44", to_hex(to_bytes(&t).unwrap())?);
|
||||||
// let expected = r#""Unit""#;
|
// let expected = r#""Unit""#;
|
||||||
// assert_eq!(to_string(&u).unwrap(), expected);
|
// assert_eq!(to_string(&u).unwrap(), expected);
|
||||||
//
|
//
|
||||||
@ -406,11 +414,10 @@ fn test_enum() -> std::result::Result<(), FromUtf8Error> {
|
|||||||
// let expected = r#"{"Struct":{"a":1}}"#;
|
// let expected = r#"{"Struct":{"a":1}}"#;
|
||||||
// assert_eq!(to_string(&s).unwrap(), expected);
|
// assert_eq!(to_string(&s).unwrap(), expected);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_map() -> Result<()> {
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map() -> error::Result<()> {
|
||||||
let mut src = HashMap::new();
|
let mut src = HashMap::new();
|
||||||
src.insert("foo", 1);
|
src.insert("foo", 1);
|
||||||
src.insert("foo", 42);
|
src.insert("foo", 42);
|
||||||
@ -419,4 +426,5 @@ fn test_map() -> Result<()> {
|
|||||||
let packed = to_bytes(&src)?;
|
let packed = to_bytes(&src)?;
|
||||||
println!("{}", to_dump(&packed));
|
println!("{}", to_dump(&packed));
|
||||||
Ok(())
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user