Giter VIP home page Giter VIP logo

Comments (6)

hcpl avatar hcpl commented on July 2, 2024 3

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 common Hasher/BuildHasher API) and HashMap implementation. Both are designed to be independently replaceable by third-party means.
  • fnv and fxhash provide alternative hasher implementations (which use faster hashing algorithms). For convenience, they provide type aliases FnvHashMap<K, V> and FxHashMap<K, V> that use the standard HashMap implementation so that you didn't have to write HashMap<K, V, FnvBuildHasher> and HashMap<K, V, FxBuildHasher> every single time.
  • hashbrown, on the other hand, provides an alternative HashMap implementation (which is faster than the standard one).
  • serde's definition of Deserialize for the standard HashMap implementation is generic over any hasher. That's why your std_map, fx_map and fnv_map cases all worked. But the definition of Deserialize for hashbrown::HashMap is in hashbrown and needs to be manually switched on.

Hope the explanation is able to clear your confusion!

from hashbrown.

hcpl avatar hcpl commented on July 2, 2024 1

See https://github.com/Amanieu/hashbrown/issues/9 as an alternative solution to this issue, because serde already provides:

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.

clbarnes avatar clbarnes commented on July 2, 2024

#14 was merged before the 0.1.5 release, but I still can't #[derive(Deserialize)] on structs containing hashbrown::HashMaps 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.

hcpl avatar hcpl commented on July 2, 2024

@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.

clbarnes avatar clbarnes commented on July 2, 2024

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.

clbarnes avatar clbarnes commented on July 2, 2024

Got it, didn't think about the feature flag. Thanks!

from hashbrown.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.