Skip to content

Commit

Permalink
feat: Better no_std support (#44)
Browse files Browse the repository at this point in the history
Signed-off-by: YXL <chenxin.lan.76@gmail.com>
  • Loading branch information
YXL76 authored Apr 14, 2022
1 parent d552e76 commit 95574e2
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ edition = "2021"

[dependencies]
cfg-if = "1.0"
hashbrown = { version = "0.11", default-features = false, features = ["ahash"] }
serde = { version = "1.0", optional = true }
hashbrown = { version = "0.12.0", default-features = false, features = ["ahash"] }
serde = { version = "1.0", default-features = false, features = ["alloc"], optional = true }

[dev-dependencies]
serde_json = "1.0"
Expand All @@ -29,7 +29,7 @@ harness = false

[features]
default = ["std", "serde-1", "inline-more", "backends"]
std = []
std = ["serde/std"]

# Enable this if you need `Serde` serialization and deserialization support.
#
Expand Down
2 changes: 2 additions & 0 deletions src/backend/bucket/fixed_str.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::InternedStr;
#[cfg(not(feature = "std"))]
use alloc::string::String;

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct FixedString {
Expand Down
2 changes: 1 addition & 1 deletion src/backend/bucket/interned_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ mod tests {

#[test]
fn size_of() {
use std::mem;
use core::mem;
assert_eq!(mem::size_of::<InternedStr>(), mem::size_of::<&str>());
}
}
3 changes: 2 additions & 1 deletion src/backend/bucket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ use crate::{
DefaultSymbol,
Symbol,
};
#[cfg(not(feature = "std"))]
use alloc::string::String;
use core::{
iter::Enumerate,
marker::PhantomData,
slice,
};

/// An interner backend that reduces memory allocations by using string buckets.
///
/// # Note
Expand Down
2 changes: 2 additions & 0 deletions src/backend/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ mod tests {
decode_var_usize,
encode_var_usize,
};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

#[test]
fn encode_var_usize_1_byte_works() {
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
//!
//! Never use this one for real use cases!

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(feature = "serde-1")]
mod serde_impl;

Expand Down
2 changes: 2 additions & 0 deletions src/serde_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::{
StringInterner,
Symbol,
};
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
use core::{
default::Default,
fmt,
Expand Down

0 comments on commit 95574e2

Please sign in to comment.