Comments (6)
Oh, Serde support was made optional for hashbrown to reduce compile times for those who don't need it. So to make your code compile, just replace
hashbrown = "0.1.6"
with
hashbrown = { version = "0.1.6", features = ["serde"] }
The reason why Serde support worked for other cases is thanks to the combination of distinct factors:
std
provides two building blocks for creating hash maps:RandomState
hasher implementation (which uses commonHasher
/BuildHasher
API) andHashMap
implementation. Both are designed to be independently replaceable by third-party means.fnv
andfxhash
provide alternative hasher implementations (which use faster hashing algorithms). For convenience, they provide type aliasesFnvHashMap<K, V>
andFxHashMap<K, V>
that use the standardHashMap
implementation so that you didn't have to writeHashMap<K, V, FnvBuildHasher>
andHashMap<K, V, FxBuildHasher>
every single time.hashbrown
, on the other hand, provides an alternativeHashMap
implementation (which is faster than the standard one).serde
's definition ofDeserialize
for the standardHashMap
implementation is generic over any hasher. That's why yourstd_map
,fx_map
andfnv_map
cases all worked. But the definition ofDeserialize
forhashbrown::HashMap
is inhashbrown
and needs to be manually switched on.
Hope the explanation is able to clear your confusion!
from hashbrown.
See https://github.com/Amanieu/hashbrown/issues/9 as an alternative solution to this issue, because serde
already provides:
impl<K, V, H> Serialize for HashMap<K, V, H>impl<T, H> Serialize for HashSet<T, H>impl<'de, K, V, S> Deserialize<'de> for HashMap<K, V, S>impl<'de, T, S> Deserialize<'de> for HashSet<T, S>
So hashbrown
only needs to expose its hasher for serde
(and many other crates) to work!
Nevermind, hashbrown
is a new implementation of HashMap
and HashSet
that is generic over any hasher as well. In that case I'll send a PR for serde
support sometime today.
from hashbrown.
#14 was merged before the 0.1.5 release, but I still can't #[derive(Deserialize)]
on structs containing hashbrown::HashMap
s in the same way that I can hashmaps from std, fxhash, or fnv. Is this expected?
error[E0277]: the trait bound `hashbrown::HashMap<T, T>: serde::Deserialize<'_>` is not satisfied
--> src/main.rs:32:10
|
32 | #[derive(Deserialize)]
| ^^^^^^^^^^^ the trait `serde::Deserialize<'_>` is not implemented for `hashbrown::HashMap<T, T>`
|
= note: required by `serde::de::SeqAccess::next_element`
error[E0277]: the trait bound `hashbrown::HashMap<T, T>: serde::Deserialize<'_>` is not satisfied
--> src/main.rs:32:10
|
32 | #[derive(Deserialize)]
| ^^^^^^^^^^^ the trait `serde::Deserialize<'_>` is not implemented for `hashbrown::HashMap<T, T>`
|
= note: required by `serde::de::MapAccess::next_value`
from hashbrown.
@clbarnes sorry for a very late response, but if your issue hasn't been resolved yet, could you please share code that produces these errors?
Is this expected?
The Deserialize
impl for hashbrown::HashMap
is a port from upstream implementation for std HashMap
(https://docs.rs/serde/1.0.80/src/serde/de/impls.rs.html#1192-1236), so if your code worked for std, fxhash, or fnv then hashbrown should have worked out of the box too.
from hashbrown.
Sure thing!
https://github.com/clbarnes/hash_test/tree/master
As the readme says, that repo is in a state which builds and runs (demonstrating that std, fx and fnv all work); uncomment L51 on src/main.rs
(which attempts to deserialize a JSON string into a hashbrown::HashMap
) and it'll break.
Can't wait to use this library as soon as I can get the deserializing to work (my application is a port from JS and very lookup-heavy, hashbrown gives a 2x speedup over fx); thanks very much for your work.
from hashbrown.
Got it, didn't think about the feature flag. Thanks!
from hashbrown.
Related Issues (20)
- `hashbrown` fails to compile as a transitive dependency HOT 2
- allocator-api2 default-feature? HOT 2
- Compiling hashbrown 0.14.2 for aarch64-unknown-linux-gnu with "target-cpu=cortex-a53" generates illegal instructions HOT 2
- Switching to GxHash? HOT 9
- Feature: increase capacity according to the actual size returned by the allocator HOT 2
- Hashbrown crash due to bad malloc HOT 1
- 0.14.3 - no method named `clear` found for struct `HashMap` in the current scope HOT 5
- Benchmark biaised due to no fence around input
- assertion failed: buckets.is_power_of_two() HOT 8
- Build breaks on nightly due to use of `stdsimd` rust feature in ahash 0.8.6 HOT 2
- Was swap-remove behavior ever considered when removing entries? HOT 10
- Consider returning to 1.63.0 MSRV HOT 1
- How to calculate the size of the hashbrown::HashMap at runtime? HOT 2
- LLVM failed to use the knowledge from a never-overflow assumption HOT 13
- Library test `map::test_map::test_clone_from_memory_leaks` errors with using uninitialized data under valgrind and miri
- update to ahash 0.8.7 or after to use new stdsimd feature portable_simd HOT 1
- Insertion performance with arena allocators HOT 3
- Do not grow the raw table when lots of deletion and insertion is performed HOT 3
- Unsound usages of unsafe implementation from `usize` to `T` HOT 8
- Support lower maximum size HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hashbrown.