Skip to content

Commit

Permalink
Add support for hashbrown v0.15 (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbb authored Oct 5, 2024
2 parents db290af + ef7403d commit a58cb33
Show file tree
Hide file tree
Showing 15 changed files with 535 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ jobs:
- name: "Check no_std+alloc (No Default Features / ${{ matrix.os }} / ${{ matrix.rust }})"
run: cargo check --package serde_with --no-default-features --features=alloc --target thumbv7em-none-eabihf
- name: "Check no_std+alloc+optional (No Default Features / ${{ matrix.os }} / ${{ matrix.rust }})"
run: cargo check --package serde_with --no-default-features --features=alloc,base64,chrono_0_4,hashbrown_0_14,hex,indexmap_1,indexmap_2,json,time_0_3 --target thumbv7em-none-eabihf
run: cargo check --package serde_with --no-default-features --features=alloc,base64,chrono_0_4,hashbrown_0_14,hashbrown_0_15,hex,indexmap_1,indexmap_2,json,time_0_3 --target thumbv7em-none-eabihf

# The tests are split into build and run steps, to see the time impact of each
# cargo test --all-targets does NOT run doctests
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions serde_with/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

* Add support for `hashbrown` v0.15 (#787/#790)

This extends the existing support for `hashbrown` v0.14 to the newly released version.

## [3.10.0] - 2024-10-01

### Added
Expand Down
12 changes: 12 additions & 0 deletions serde_with/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ chrono_0_4 = ["dep:chrono_0_4"]
## It enables the `alloc` feature.
## Some functionality is only available when `std` is enabled too.
hashbrown_0_14 = ["dep:hashbrown_0_14", "alloc"]
## The feature enables `hashbrown::{HashMap, HashSet}` as supported containers.
##
## This pulls in `hashbrown` v0.15 as a dependency.
## It enables the `alloc` feature.
## Some functionality is only available when `std` is enabled too.
hashbrown_0_15 = ["dep:hashbrown_0_15", "alloc"]
## The feature enables serializing data in hex format.
##
## This pulls in `hex` as a dependency.
Expand Down Expand Up @@ -126,6 +132,7 @@ chrono_0_4 = {package = "chrono", version = "0.4.20", optional = true, default-f
doc-comment = {version = "0.3.3", optional = true}
document-features = {version = "0.2.7", optional = true}
hashbrown_0_14 = {package = "hashbrown", version = "0.14.0", optional = true, default-features = false, features = ["serde"]}
hashbrown_0_15 = {package = "hashbrown", version = "0.15.0", optional = true, default-features = false, features = ["serde"]}
hex = {version = "0.4.3", optional = true, default-features = false}
indexmap_1 = {package = "indexmap", version = "1.8", optional = true, default-features = false, features = ["serde-1"]}
indexmap_2 = {package = "indexmap", version = "2.0", optional = true, default-features = false, features = ["serde"]}
Expand Down Expand Up @@ -175,6 +182,11 @@ name = "hashbrown_0_14"
path = "tests/hashbrown_0_14.rs"
required-features = ["hashbrown_0_14", "macros"]

[[test]]
name = "hashbrown_0_15"
path = "tests/hashbrown_0_15.rs"
required-features = ["hashbrown_0_15", "macros"]

[[test]]
name = "indexmap_1"
path = "tests/indexmap_1.rs"
Expand Down
2 changes: 2 additions & 0 deletions serde_with/src/de/duplicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::{
};
#[cfg(feature = "hashbrown_0_14")]
use hashbrown_0_14::{HashMap as HashbrownMap014, HashSet as HashbrownSet014};
#[cfg(feature = "hashbrown_0_15")]
use hashbrown_0_15::{HashMap as HashbrownMap015, HashSet as HashbrownSet015};
#[cfg(feature = "indexmap_1")]
use indexmap_1::{IndexMap, IndexSet};
#[cfg(feature = "indexmap_2")]
Expand Down
13 changes: 13 additions & 0 deletions serde_with/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub(crate) use self::macros::*;
use crate::{formats::*, prelude::*};
#[cfg(feature = "hashbrown_0_14")]
use hashbrown_0_14::{HashMap as HashbrownMap014, HashSet as HashbrownSet014};
#[cfg(feature = "hashbrown_0_15")]
use hashbrown_0_15::{HashMap as HashbrownMap015, HashSet as HashbrownSet015};
#[cfg(feature = "indexmap_1")]
use indexmap_1::{IndexMap, IndexSet};
#[cfg(feature = "indexmap_2")]
Expand Down Expand Up @@ -32,6 +34,11 @@ pub(crate) mod macros {
HashbrownMap014<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| HashbrownMap014::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "hashbrown_0_15")]
$m!(
HashbrownMap015<K: Eq + Hash, V, S: BuildHasher + Default>,
(|size| HashbrownMap015::with_capacity_and_hasher(size, Default::default()))
);
#[cfg(feature = "indexmap_1")]
$m!(
IndexMap<K: Eq + Hash, V, S: BuildHasher + Default>,
Expand Down Expand Up @@ -61,6 +68,12 @@ pub(crate) mod macros {
(|size| HashbrownSet014::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "hashbrown_0_15")]
$m!(
HashbrownSet015<T: Eq + Hash, S: BuildHasher + Default>,
(|size| HashbrownSet015::with_capacity_and_hasher(size, S::default())),
insert
);
#[cfg(feature = "indexmap_1")]
$m!(
IndexSet<T: Eq + Hash, S: BuildHasher + Default>,
Expand Down
2 changes: 2 additions & 0 deletions serde_with/src/de/skip_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use super::impls::macros::foreach_map;
use crate::prelude::*;
#[cfg(feature = "hashbrown_0_14")]
use hashbrown_0_14::HashMap as HashbrownMap014;
#[cfg(feature = "hashbrown_0_15")]
use hashbrown_0_15::HashMap as HashbrownMap015;
#[cfg(feature = "indexmap_1")]
use indexmap_1::IndexMap;
#[cfg(feature = "indexmap_2")]
Expand Down
40 changes: 40 additions & 0 deletions serde_with/src/duplicate_key_impls/error_on_duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ where
}
}

#[cfg(feature = "hashbrown_0_15")]
impl<T, S> PreventDuplicateInsertsSet<T> for hashbrown_0_15::HashSet<T, S>
where
T: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn new(size_hint: Option<usize>) -> Self {
match size_hint {
Some(size) => Self::with_capacity_and_hasher(size, S::default()),
None => Self::with_hasher(S::default()),
}
}

#[inline]
fn insert(&mut self, value: T) -> bool {
self.insert(value)
}
}

#[cfg(feature = "indexmap_1")]
impl<T, S> PreventDuplicateInsertsSet<T> for indexmap_1::IndexSet<T, S>
where
Expand Down Expand Up @@ -149,6 +169,26 @@ where
}
}

#[cfg(feature = "hashbrown_0_15")]
impl<K, V, S> PreventDuplicateInsertsMap<K, V> for hashbrown_0_15::HashMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn new(size_hint: Option<usize>) -> Self {
match size_hint {
Some(size) => Self::with_capacity_and_hasher(size, S::default()),
None => Self::with_hasher(S::default()),
}
}

#[inline]
fn insert(&mut self, key: K, value: V) -> bool {
self.insert(key, value).is_none()
}
}

#[cfg(feature = "indexmap_1")]
impl<K, V, S> PreventDuplicateInsertsMap<K, V> for indexmap_1::IndexMap<K, V, S>
where
Expand Down
28 changes: 28 additions & 0 deletions serde_with/src/duplicate_key_impls/first_value_wins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,34 @@ where
}
}

#[cfg(feature = "hashbrown_0_15")]
impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for hashbrown_0_15::HashMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn new(size_hint: Option<usize>) -> Self {
match size_hint {
Some(size) => Self::with_capacity_and_hasher(size, S::default()),
None => Self::with_hasher(S::default()),
}
}

#[inline]
fn insert(&mut self, key: K, value: V) {
use hashbrown_0_15::hash_map::Entry;

match self.entry(key) {
// we want to keep the first value, so do nothing
Entry::Occupied(_) => {}
Entry::Vacant(vacant) => {
vacant.insert(value);
}
}
}
}

#[cfg(feature = "indexmap_1")]
impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for indexmap_1::IndexMap<K, V, S>
where
Expand Down
21 changes: 21 additions & 0 deletions serde_with/src/duplicate_key_impls/last_value_wins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ where
}
}

#[cfg(feature = "hashbrown_0_15")]
impl<T, S> DuplicateInsertsLastWinsSet<T> for hashbrown_0_15::HashSet<T, S>
where
T: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn new(size_hint: Option<usize>) -> Self {
match size_hint {
Some(size) => Self::with_capacity_and_hasher(size, S::default()),
None => Self::with_hasher(S::default()),
}
}

#[inline]
fn replace(&mut self, value: T) {
// Hashset already fulfils the contract
self.replace(value);
}
}

#[cfg(feature = "indexmap_1")]
impl<T, S> DuplicateInsertsLastWinsSet<T> for indexmap_1::IndexSet<T, S>
where
Expand Down
2 changes: 2 additions & 0 deletions serde_with/src/schemars_0_8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,8 @@ map_first_last_wins_schema!(BTreeMap<K, V>);
map_first_last_wins_schema!(=> S HashMap<K, V, S>);
#[cfg(feature = "hashbrown_0_14")]
map_first_last_wins_schema!(=> S hashbrown_0_14::HashMap<K, V, S>);
#[cfg(feature = "hashbrown_0_15")]
map_first_last_wins_schema!(=> S hashbrown_0_15::HashMap<K, V, S>);
#[cfg(feature = "indexmap_1")]
map_first_last_wins_schema!(=> S indexmap_1::IndexMap<K, V, S>);
#[cfg(feature = "indexmap_2")]
Expand Down
2 changes: 2 additions & 0 deletions serde_with/src/ser/duplicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use super::impls::macros::{foreach_map, foreach_set};
use crate::prelude::*;
#[cfg(feature = "hashbrown_0_14")]
use hashbrown_0_14::{HashMap as HashbrownMap014, HashSet as HashbrownSet014};
#[cfg(feature = "hashbrown_0_15")]
use hashbrown_0_15::{HashMap as HashbrownMap015, HashSet as HashbrownSet015};
#[cfg(feature = "indexmap_1")]
use indexmap_1::{IndexMap, IndexSet};
#[cfg(feature = "indexmap_2")]
Expand Down
6 changes: 6 additions & 0 deletions serde_with/src/ser/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub(crate) use self::macros::*;
use crate::{formats::Strictness, prelude::*};
#[cfg(feature = "hashbrown_0_14")]
use hashbrown_0_14::{HashMap as HashbrownMap014, HashSet as HashbrownSet014};
#[cfg(feature = "hashbrown_0_15")]
use hashbrown_0_15::{HashMap as HashbrownMap015, HashSet as HashbrownSet015};
#[cfg(feature = "indexmap_1")]
use indexmap_1::{IndexMap, IndexSet};
#[cfg(feature = "indexmap_2")]
Expand Down Expand Up @@ -29,6 +31,8 @@ pub(crate) mod macros {
$m!(HashMap<K, V, H: Sized>);
#[cfg(feature = "hashbrown_0_14")]
$m!(HashbrownMap014<K, V, H: Sized>);
#[cfg(feature = "hashbrown_0_15")]
$m!(HashbrownMap015<K, V, H: Sized>);
#[cfg(feature = "indexmap_1")]
$m!(IndexMap<K, V, H: Sized>);
#[cfg(feature = "indexmap_2")]
Expand All @@ -44,6 +48,8 @@ pub(crate) mod macros {
$m!(HashSet<$T, H: Sized>);
#[cfg(feature = "hashbrown_0_14")]
$m!(HashbrownSet014<$T, H: Sized>);
#[cfg(feature = "hashbrown_0_15")]
$m!(HashbrownSet015<$T, H: Sized>);
#[cfg(feature = "indexmap_1")]
$m!(IndexSet<$T, H: Sized>);
#[cfg(feature = "indexmap_2")]
Expand Down
2 changes: 2 additions & 0 deletions serde_with/src/ser/skip_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use super::impls::macros::foreach_map;
use crate::prelude::*;
#[cfg(feature = "hashbrown_0_14")]
use hashbrown_0_14::HashMap as HashbrownMap014;
#[cfg(feature = "hashbrown_0_15")]
use hashbrown_0_15::HashMap as HashbrownMap015;
#[cfg(feature = "indexmap_1")]
use indexmap_1::IndexMap;
#[cfg(feature = "indexmap_2")]
Expand Down
Loading

0 comments on commit a58cb33

Please sign in to comment.