Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove no_std hashbrown dep #104

Merged
merged 3 commits into from
May 30, 2024
Merged
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
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.12.3", optional = true }
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.12.3", optional = true }
no_std = []
63 changes: 46 additions & 17 deletions src/serde_bin.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
use core::convert::TryInto;
use core::hash::Hash;

use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::collections::{BTreeSet, LinkedList};
use alloc::collections::{BTreeMap, 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 +287,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 +301,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,
T: DeBin + core::hash::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 +529,41 @@ 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,
{
fn ser_bin(&self, s: &mut Vec<u8>) {
let len = self.len();
len.ser_bin(s);
for (k, v) in self {
k.ser_bin(s);
v.ser_bin(s);
}
}
}

#[cfg(not(feature = "no_std"))]
impl<K, V> DeBin for std::collections::HashMap<K, V>
where
K: DeBin + core::cmp::Eq + core::hash::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 = std::collections::HashMap::with_capacity(len);
for _ in 0..len {
let k = DeBin::de_bin(o, d)?;
let v = DeBin::de_bin(o, d)?;
h.insert(k, v);
}
Ok(h)
}
}

impl<K, V> SerBin for BTreeMap<K, V>
where
K: SerBin,
V: SerBin,
Expand All @@ -549,14 +578,14 @@ where
}
}

impl<K, V> DeBin for HashMap<K, V>
impl<K, V> DeBin for BTreeMap<K, V>
where
K: DeBin + core::cmp::Eq + Hash,
K: DeBin + core::cmp::Eq + Ord,
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 = BTreeMap::new();
for _ in 0..len {
let k = DeBin::de_bin(o, d)?;
let v = DeBin::de_bin(o, d)?;
Expand Down
75 changes: 58 additions & 17 deletions src/serde_json.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
use core::hash::Hash;
use core::str::Chars;

use alloc::boxed::Box;
use alloc::collections::{BTreeSet, LinkedList};
use alloc::collections::{BTreeMap, BTreeSet, LinkedList};
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 +878,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 +899,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,
T: DeJson + core::hash::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 +1172,53 @@ 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,
{
fn ser_json(&self, d: usize, s: &mut SerJsonState) {
s.out.push('{');
let len = self.len();
let mut index = 0;
for (k, v) in self {
s.indent(d + 1);
k.ser_json(d + 1, s);
s.out.push(':');
v.ser_json(d + 1, s);
if (index + 1) < len {
s.conl();
}
index += 1;
}
s.indent(d);
s.out.push('}');
}
}

#[cfg(not(feature = "no_std"))]
impl<K, V> DeJson for std::collections::HashMap<K, V>
where
K: DeJson + Eq + core::hash::Hash,
V: DeJson,
{
fn de_json(s: &mut DeJsonState, i: &mut Chars) -> Result<Self, DeJsonErr> {
let mut h = std::collections::HashMap::new();
s.curly_open(i)?;
while s.tok != DeJsonTok::CurlyClose {
let k = DeJson::de_json(s, i)?;
s.colon(i)?;
let v = DeJson::de_json(s, i)?;
s.eat_comma_curly(i)?;
h.insert(k, v);
}
s.curly_close(i)?;
Ok(h)
}
}

impl<K, V> SerJson for BTreeMap<K, V>
where
K: SerJson,
V: SerJson,
Expand All @@ -1201,13 +1242,13 @@ where
}
}

impl<K, V> DeJson for HashMap<K, V>
impl<K, V> DeJson for BTreeMap<K, V>
where
K: DeJson + Eq + Hash,
K: DeJson + Eq + Ord,
V: DeJson,
{
fn de_json(s: &mut DeJsonState, i: &mut Chars) -> Result<Self, DeJsonErr> {
let mut h = HashMap::new();
let mut h = BTreeMap::new();
s.curly_open(i)?;
while s.tok != DeJsonTok::CurlyClose {
let k = DeJson::de_json(s, i)?;
Expand Down
70 changes: 53 additions & 17 deletions src/serde_ron.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
use core::hash::Hash;
use core::str::Chars;

use alloc::boxed::Box;
use alloc::collections::{BTreeSet, LinkedList};
use alloc::collections::{BTreeMap, BTreeSet, LinkedList};
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 +876,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 +897,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,
T: DeRon + core::hash::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 +1184,48 @@ 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,
{
fn ser_ron(&self, d: usize, s: &mut SerRonState) {
s.out.push_str("{\n");
for (k, v) in self {
s.indent(d + 1);
k.ser_ron(d + 1, s);
s.out.push_str(":");
v.ser_ron(d + 1, s);
s.conl();
}
s.indent(d);
s.out.push('}');
}
}

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

impl<K, V> SerRon for BTreeMap<K, V>
where
K: SerRon,
V: SerRon,
Expand All @@ -1208,13 +1244,13 @@ where
}
}

impl<K, V> DeRon for HashMap<K, V>
impl<K, V> DeRon for BTreeMap<K, V>
where
K: DeRon + Eq + Hash,
K: DeRon + Eq + Ord,
V: DeRon,
{
fn de_ron(s: &mut DeRonState, i: &mut Chars) -> Result<Self, DeRonErr> {
let mut h = HashMap::new();
let mut h = BTreeMap::new();
s.curly_open(i)?;
while s.tok != DeRonTok::CurlyClose {
let k = DeRon::de_ron(s, i)?;
Expand Down
Loading
Loading