Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project
adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.1.0] - 2023-09-20

### ADDED

- Added support for [Bincode](https://docs.rs/bincode/2.0.0-rc.3/bincode/). Use `bincode`
feature to enable it.

## [1.0.1] - 2022-03-24

### FIXED
Expand Down
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "smartstring"
version = "1.0.1"
version = "1.1.0"
authors = ["Bodil Stokke <bodil@bodil.org>"]
edition = "2021"
license = "MPL-2.0+"
Expand All @@ -11,11 +11,10 @@ readme = "./README.md"
categories = ["data-structures"]
keywords = ["cache-local", "cpu-cache", "small-string", "sso", "inline-string"]
exclude = ["release.toml", "proptest-regressions/**"]
rust-version = "1.57"
build = "./build.rs"

[package.metadata.docs.rs]
features = ["arbitrary", "proptest", "serde"]
features = ["arbitrary", "proptest", "serde", "bincode"]

[badges]
travis-ci = { repository = "bodil/smartstring", branch = "master" }
Expand All @@ -25,19 +24,21 @@ name = "smartstring"
harness = false

[features]
default = ["std"]
default = ["std", "bincode"]
std = []
test = ["std", "arbitrary", "arbitrary/derive"]
bincode = ["dep:bincode"]

[dependencies]
static_assertions = "1"
serde = { version = "1", optional = true }
arbitrary = { version = "1", optional = true }
proptest = { version = "1", optional = true }
bincode = { version = "2.0.1", features=["alloc"], optional = true }

[dev-dependencies]
proptest = "1"
proptest-derive = "0.3"
proptest-derive = "0.5"
criterion = "0.3"
rand = "0.8"
serde_test = "1"
Expand Down
3 changes: 3 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use version_check as rustc;

fn main() {
println!("cargo:rustc-check-cfg=cfg(has_allocator)");
println!("cargo:rustc-check-cfg=cfg(needs_allocator_feature)");

let ac = autocfg::new();
let has_feature = Some(true) == rustc::supports_feature("allocator_api");
let has_api = ac.probe_trait("alloc::alloc::Allocator");
Expand Down
77 changes: 77 additions & 0 deletions src/bincode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//! Support for Bincode integration. Enable this with the `bincode` feature.

use crate::{Compact, LazyCompact, SmartString, SmartStringMode};
use std::borrow::Cow;

use bincode::{
de::Decoder,
enc::Encoder,
error::{DecodeError, EncodeError},
impl_borrow_decode, Decode, Encode,
};

impl<T: SmartStringMode> Encode for SmartString<T> {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
self.as_str().encode(encoder)
}
}

impl<T: SmartStringMode, Context> Decode<Context> for SmartString<T> {
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> {
// Decode as a Cow to borrow from the decoder's buffer
let s = <Cow<'_, str> as Decode<Context>>::decode(decoder)?;
Ok(Self::from(s.as_ref()))
}
}

impl_borrow_decode!(SmartString<Compact>);
impl_borrow_decode!(SmartString<LazyCompact>);

#[cfg(test)]
mod test {
use crate::{Compact, LazyCompact, SmartString};

#[test]
fn test_bincode_compact() {
let mut buf: [u8; 64] = [0; 64];
let short_str = "Hello world";
let long_str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit";

let config = bincode::config::standard();
let smartstring = SmartString::<Compact>::from(short_str);
let len = bincode::encode_into_slice(smartstring, &mut buf, config).unwrap();
let smartstring: SmartString<Compact> =
bincode::decode_from_slice(&buf[..len], config).unwrap().0;
assert_eq!(smartstring, short_str);

let smartstring = SmartString::<Compact>::from(long_str);
let len = bincode::encode_into_slice(smartstring, &mut buf, config).unwrap();
let smartstring: SmartString<Compact> =
bincode::decode_from_slice(&buf[..len], config).unwrap().0;
assert_eq!(smartstring, long_str);
}

#[test]
fn test_bincode_lazy_compact() {
let mut buf: [u8; 64] = [0; 64];
let short_str = "Hello world";
let long_str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit";

let config = bincode::config::standard();
let smartstring = SmartString::<LazyCompact>::from(short_str);
let len = bincode::encode_into_slice(smartstring, &mut buf, config).unwrap();
let smartstring: SmartString<Compact> =
bincode::decode_from_slice(&buf[..len], config).unwrap().0;
assert_eq!(smartstring, short_str);

let smartstring = SmartString::<LazyCompact>::from(long_str);
let len = bincode::encode_into_slice(smartstring, &mut buf, config).unwrap();
let smartstring: SmartString<Compact> =
bincode::decode_from_slice(&buf[..len], config).unwrap().0;
assert_eq!(smartstring, long_str);
}
}
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,20 @@
//!
//! ```no_compile
//! [dependencies]
//! smartstring = { version = "*", features = ["proptest", "serde"] }
//! smartstring = { version = "*", features = ["proptest", "serde", "bincode"] }
//! ```
//!
//! | Feature | Description |
//! | ------- | ----------- |
//! | [`arbitrary`](https://crates.io/crates/arbitrary) | [`Arbitrary`][Arbitrary] implementation for [`SmartString`]. |
//! | [`proptest`](https://crates.io/crates/proptest) | A strategy for generating [`SmartString`]s from a regular expression. |
//! | [`serde`](https://crates.io/crates/serde) | [`Serialize`][Serialize] and [`Deserialize`][Deserialize] implementations for [`SmartString`]. |
//! | [`bincode`](https://crates.io/crates/bincode) | [`Encode`][Encode] and [`Decode`][Decode] implementations for [`SmartString`]. |
//!
//! [Serialize]: https://docs.rs/serde/latest/serde/trait.Serialize.html
//! [Deserialize]: https://docs.rs/serde/latest/serde/trait.Deserialize.html
//! [Encode]: https://docs.rs/bincode/2.0.0-rc.3/bincode/enc/trait.Encode.html
//! [Decode]: https://docs.rs/bincode/2.0.0-rc.3/bincode/de/trait.Decode.html
//! [Arbitrary]: https://docs.rs/arbitrary/latest/arbitrary/trait.Arbitrary.html

// Ensure all unsafe blocks get flagged for manual validation.
Expand Down Expand Up @@ -159,6 +162,9 @@ mod arbitrary;
#[cfg(feature = "proptest")]
pub mod proptest;

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

/// Convenient type aliases.
pub mod alias {
use super::*;
Expand Down