Skip to content

Commit

Permalink
remove hashbrown
Browse files Browse the repository at this point in the history
  • Loading branch information
knickish committed May 28, 2024
1 parent db0d90f commit 334b573
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 45 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ json = ["dep:nanoserde-derive", "nanoserde-derive/json"]
binary = ["dep:nanoserde-derive", "nanoserde-derive/binary"]
ron = ["dep:nanoserde-derive", "nanoserde-derive/ron"]
toml = []
no_std = ["dep:hashbrown"]
no_std = []

[dependencies]
hashbrown = { version = "0.14", optional = true, default-features = false, features = ["ahash"] }
nanoserde-derive = { path = "derive", version = "=0.2.0", optional = true }
5 changes: 1 addition & 4 deletions derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ default = []
json = []
binary = []
ron = []
no_std = ["dep:hashbrown"]

[dependencies]
hashbrown = { version = "0.14", optional = true, default-features = false, features = ["ahash"] }
no_std = []
24 changes: 11 additions & 13 deletions src/serde_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ use alloc::collections::{BTreeSet, LinkedList};
use alloc::string::String;
use alloc::vec::Vec;

#[cfg(feature = "no_std")]
use hashbrown::{HashMap, HashSet};

#[cfg(not(feature = "no_std"))]
use std::collections::{HashMap, HashSet};

/// A trait for objects that can be serialized to binary.
pub trait SerBin {
/// Serialize Self to bytes.
Expand Down Expand Up @@ -294,7 +288,8 @@ where
}
}

impl<T> SerBin for HashSet<T>
#[cfg(not(feature = "no_std"))]
impl<T> SerBin for std::collections::HashSet<T>
where
T: SerBin,
{
Expand All @@ -307,13 +302,14 @@ where
}
}

impl<T> DeBin for HashSet<T>
#[cfg(not(feature = "no_std"))]
impl<T> DeBin for std::collections::HashSet<T>
where
T: DeBin + Hash + Eq,
{
fn de_bin(o: &mut usize, d: &[u8]) -> Result<HashSet<T>, DeBinErr> {
fn de_bin(o: &mut usize, d: &[u8]) -> Result<Self, DeBinErr> {
let len: usize = DeBin::de_bin(o, d)?;
let mut out = HashSet::with_capacity(len);
let mut out = std::collections::HashSet::with_capacity(len);
for _ in 0..len {
out.insert(DeBin::de_bin(o, d)?);
}
Expand Down Expand Up @@ -534,7 +530,8 @@ where
}
}

impl<K, V> SerBin for HashMap<K, V>
#[cfg(not(feature = "no_std"))]
impl<K, V> SerBin for std::collections::HashMap<K, V>
where
K: SerBin,
V: SerBin,
Expand All @@ -549,14 +546,15 @@ where
}
}

impl<K, V> DeBin for HashMap<K, V>
#[cfg(not(feature = "no_std"))]
impl<K, V> DeBin for std::collections::HashMap<K, V>
where
K: DeBin + core::cmp::Eq + Hash,
V: DeBin,
{
fn de_bin(o: &mut usize, d: &[u8]) -> Result<Self, DeBinErr> {
let len: usize = DeBin::de_bin(o, d)?;
let mut h = HashMap::with_capacity(len);
let mut h = std::collections::HashMap::with_capacity(len);
for _ in 0..len {
let k = DeBin::de_bin(o, d)?;
let v = DeBin::de_bin(o, d)?;
Expand Down
24 changes: 11 additions & 13 deletions src/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

#[cfg(feature = "no_std")]
use hashbrown::{HashMap, HashSet};

#[cfg(not(feature = "no_std"))]
use std::collections::{HashMap, HashSet};

/// The internal state of a JSON serialization.
pub struct SerJsonState {
pub out: String,
Expand Down Expand Up @@ -885,7 +879,8 @@ where
}
}

impl<T> SerJson for HashSet<T>
#[cfg(not(feature = "no_std"))]
impl<T> SerJson for std::collections::HashSet<T>
where
T: SerJson,
{
Expand All @@ -905,12 +900,13 @@ where
}
}

impl<T> DeJson for HashSet<T>
#[cfg(not(feature = "no_std"))]
impl<T> DeJson for std::collections::HashSet<T>
where
T: DeJson + Hash + Eq,
{
fn de_json(s: &mut DeJsonState, i: &mut Chars) -> Result<HashSet<T>, DeJsonErr> {
let mut out = HashSet::new();
fn de_json(s: &mut DeJsonState, i: &mut Chars) -> Result<Self, DeJsonErr> {
let mut out = std::collections::HashSet::new();
s.block_open(i)?;

while s.tok != DeJsonTok::BlockClose {
Expand Down Expand Up @@ -1177,7 +1173,8 @@ where
}
}

impl<K, V> SerJson for HashMap<K, V>
#[cfg(not(feature = "no_std"))]
impl<K, V> SerJson for std::collections::HashMap<K, V>
where
K: SerJson,
V: SerJson,
Expand All @@ -1201,13 +1198,14 @@ where
}
}

impl<K, V> DeJson for HashMap<K, V>
#[cfg(not(feature = "no_std"))]
impl<K, V> DeJson for std::collections::HashMap<K, V>
where
K: DeJson + Eq + Hash,
V: DeJson,
{
fn de_json(s: &mut DeJsonState, i: &mut Chars) -> Result<Self, DeJsonErr> {
let mut h = HashMap::new();
let mut h = std::collections::HashMap::new();
s.curly_open(i)?;
while s.tok != DeJsonTok::CurlyClose {
let k = DeJson::de_json(s, i)?;
Expand Down
24 changes: 11 additions & 13 deletions src/serde_ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

#[cfg(feature = "no_std")]
use hashbrown::{HashMap, HashSet};

#[cfg(not(feature = "no_std"))]
use std::collections::{HashMap, HashSet};

/// The internal state of a RON serialization.
pub struct SerRonState {
pub out: String,
Expand Down Expand Up @@ -883,7 +877,8 @@ where
}
}

impl<T> SerRon for HashSet<T>
#[cfg(not(feature = "no_std"))]
impl<T> SerRon for std::collections::HashSet<T>
where
T: SerRon,
{
Expand All @@ -903,12 +898,13 @@ where
}
}

impl<T> DeRon for HashSet<T>
#[cfg(not(feature = "no_std"))]
impl<T> DeRon for std::collections::HashSet<T>
where
T: DeRon + Hash + Eq,
{
fn de_ron(s: &mut DeRonState, i: &mut Chars) -> Result<HashSet<T>, DeRonErr> {
let mut out = HashSet::new();
fn de_ron(s: &mut DeRonState, i: &mut Chars) -> Result<Self, DeRonErr> {
let mut out = std::collections::HashSet::new();
s.block_open(i)?;

while s.tok != DeRonTok::BlockClose {
Expand Down Expand Up @@ -1189,7 +1185,8 @@ where
}
}

impl<K, V> SerRon for HashMap<K, V>
#[cfg(not(feature = "no_std"))]
impl<K, V> SerRon for std::collections::HashMap<K, V>
where
K: SerRon,
V: SerRon,
Expand All @@ -1208,13 +1205,14 @@ where
}
}

impl<K, V> DeRon for HashMap<K, V>
#[cfg(not(feature = "no_std"))]
impl<K, V> DeRon for std::collections::HashMap<K, V>
where
K: DeRon + Eq + Hash,
V: DeRon,
{
fn de_ron(s: &mut DeRonState, i: &mut Chars) -> Result<Self, DeRonErr> {
let mut h = HashMap::new();
let mut h = std::collections::HashMap::new();
s.curly_open(i)?;
while s.tok != DeRonTok::CurlyClose {
let k = DeRon::de_ron(s, i)?;
Expand Down

0 comments on commit 334b573

Please sign in to comment.