Skip to content

Commit

Permalink
bincode and revision support
Browse files Browse the repository at this point in the history
  • Loading branch information
simensgreen committed May 26, 2024
1 parent aea39b4 commit 25a579c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ edition = "2018"
[dependencies]
serde = { version = "1.0.136", optional = true, default_features = false }
arbitrary = { version = "1.1.0", optional = true }
revision = { version = "0.7.0", optional = true }
bincode = { version = "1.3.3", optional = true }

[dev-dependencies]
proptest = "1.0.0"
Expand All @@ -19,3 +21,5 @@ serde = { version = "1.0.136", features = ["derive"] }
[features]
default = ["std"]
std = ["serde?/std"]
bincode = ["dep:bincode", "std", "serde"]
revision = ["dep:revision", "bincode"]
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![no_std]
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;

use alloc::{borrow::Cow, boxed::Box, string::String, sync::Arc};
Expand All @@ -12,6 +12,9 @@ use core::{
str::FromStr,
};

#[cfg(feature = "revision")]
mod revision;

/// A `SmolStr` is a string type that has the following properties:
///
/// * `size_of::<SmolStr>() == 24` (therefor `== size_of::<String>()` on 64 bit platforms)
Expand Down
35 changes: 35 additions & 0 deletions src/revision.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use bincode::Options;
use revision::Revisioned;
use std::io::{Read, Write};

use crate::SmolStr;

impl Revisioned for SmolStr {
#[inline]
fn revision() -> u16 {
1
}

fn serialize_revisioned<W: Write>(&self, w: &mut W) -> Result<(), revision::Error> {
bincode::options()
.with_no_limit()
.with_little_endian()
.with_varint_encoding()
.reject_trailing_bytes()
.serialize_into(w, self)
.map_err(|ref err| revision::Error::Serialize(format!("{:?}", err)))
}

fn deserialize_revisioned<R: Read>(r: &mut R) -> Result<Self, revision::Error>
where
Self: Sized,
{
bincode::options()
.with_no_limit()
.with_little_endian()
.with_varint_encoding()
.reject_trailing_bytes()
.deserialize_from(r)
.map_err(|ref err| revision::Error::Deserialize(format!("{:?}", err)))
}
}
29 changes: 29 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,32 @@ mod test_str_ext {
assert!(!result.is_heap_allocated());
}
}

#[cfg(feature = "bincode")]
mod bincode_test {
use crate::SmolStr;

#[test]
fn test_smol_str_bincode() {
let val = SmolStr::new("this is a test");
let bincoded = bincode::serialize(&val).unwrap();
let decoded: SmolStr = bincode::deserialize(&bincoded[..]).unwrap();
assert_eq!(val, decoded);
}
}

#[cfg(feature = "revision")]
mod revision_test {
use crate::SmolStr;
use revision::Revisioned;

#[test]
fn test_smol_str_revision() {
let val = SmolStr::new("this is a test");
let mut mem: Vec<u8> = vec![];
val.serialize_revisioned(&mut mem).unwrap();
assert_eq!(mem.len(), 15);
let out = <SmolStr as Revisioned>::deserialize_revisioned(&mut mem.as_slice()).unwrap();
assert_eq!(val, out);
}
}

0 comments on commit 25a579c

Please sign in to comment.