From 4f2d020bc9095aa71c29ff19572b1a1483b417c9 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Sun, 23 Apr 2023 20:19:33 -0400 Subject: [PATCH 1/3] [num-format] add `bincode` to dev deps --- num-format/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/num-format/Cargo.toml b/num-format/Cargo.toml index cdc2eef..b1e1281 100644 --- a/num-format/Cargo.toml +++ b/num-format/Cargo.toml @@ -43,3 +43,4 @@ cfg-if = "1.0.0" lazy_static = "1.4.0" rand = "0.8.5" serde_json = "1.0.85" +bincode = "1.3.3" From 63249afbaa6e6f56435c7064f8912a30b33e687c Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Sun, 23 Apr 2023 20:20:14 -0400 Subject: [PATCH 2/3] [num-format] buffer: `.deserialize_bytes()` -> `deserialize_seq()` --- num-format/src/buffer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/num-format/src/buffer.rs b/num-format/src/buffer.rs index c600d35..2a71274 100644 --- a/num-format/src/buffer.rs +++ b/num-format/src/buffer.rs @@ -202,7 +202,7 @@ mod serialization { } } - deserializer.deserialize_bytes(BufferVisitor) + deserializer.deserialize_seq(BufferVisitor) } } From 64830609a24192fbe2f5588ade1c5d8b7cf2766d Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Sun, 23 Apr 2023 20:47:28 -0400 Subject: [PATCH 3/3] [num-format] buffer: add `bincode` serde tests --- num-format/src/buffer.rs | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/num-format/src/buffer.rs b/num-format/src/buffer.rs index 2a71274..e4bac6c 100644 --- a/num-format/src/buffer.rs +++ b/num-format/src/buffer.rs @@ -242,5 +242,52 @@ mod serialization { panic!("was somehow able to deserialize bytes that were too long") } } + + #[test] + fn test_buffer_bincode() { + // Control constants. + const TEST_STR1: &str = "1,000"; + const TEST_BUFFER1: &[u8] = TEST_STR1.as_bytes(); // [49,44,48,48,48] + const TEST_STR2: &str = "10,000"; + const TEST_BUFFER2: &[u8] = TEST_STR2.as_bytes(); // [49,48,44,48,48,48] + + // Create a struct with some byte padding. + #[derive(serde::Serialize, serde::Deserialize)] + struct Struct { + pad1: [u8; 1], + buf1: Buffer, + pad2: [u8; 3], + buf2: Buffer, + pad3: [u8; 5], + } + + let mut s = Struct { + pad1: [1_u8; 1], + buf1: Buffer::new(), + pad2: [0_u8; 3], + buf2: Buffer::new(), + pad3: [1_u8; 5], + }; + + // Write to buffers. + s.buf1.write_formatted(&1_000, &Locale::en); + s.buf2.write_formatted(&10_000, &Locale::en); + + // Serialize. + let bytes: Vec = bincode::serialize(&s).unwrap(); + + // Deserialize. + let s: Struct = bincode::deserialize(&bytes).unwrap(); + + // Assert the inner buffers made it through ok. + assert_eq!(0, s.buf1.pos); + assert_eq!(5, s.buf1.end); + assert_eq!(0, s.buf2.pos); + assert_eq!(6, s.buf2.end); + assert_eq!(TEST_BUFFER1, s.buf1.as_bytes()); + assert_eq!(TEST_STR1, s.buf1.as_str()); + assert_eq!(TEST_BUFFER2, s.buf2.as_bytes()); + assert_eq!(TEST_STR2, s.buf2.as_str()); + } } }