# serde-hashkey
[](https://github.com/udoprog/serde-hashkey)
[](https://crates.io/crates/serde-hashkey)
[](https://docs.rs/serde-hashkey)
[](https://github.com/udoprog/serde-hashkey/actions?query=branch%3Amain)
Serde-based in-memory key serialization which supports hashing.
This allows any serde-serializable type to be converted into a value which
implements `PartialEq`, `Eq`, `ParialOrd`, `Ord`, and `Hash`.
[Key] is useful because it allows for a form of type-erasure. Let's say you
want to build a generic in-memory key-value store where you want to store
arbitrary serde-serializable keys. This is useful for things like caches or
dependency injection frameworks.
## Usage
Add the following to your Cargo.toml:
```toml
[dependencies]
serde-hashkey = "0.4.5"
```
## Float policies
By default, [Key] can't include floating point types such as `f32` and
`f64`. Neither of these are [totally ordered nor hashable].
To enable the [Key] type to use `f32` and `f64` it can be constructed with a
specific float policy.
Available float policies are:
* [RejectFloatPolicy] - the default behavior when using [to_key].
* [OrderedFloat] - the behavior when using [to_key_with_ordered_float]. The
`ordered-float` feature must be enabled to use this. The behavior is
derived from the [`ordered-float` crate].
## Features
* `ordered-float` - Enables serializing floating point numbers through
behavior derived from the [`ordered-float` crate]
## Examples
> You can run this example with `cargo run --example book`
```rust
use std::collections::HashMap;
use serde_derive::{Deserialize, Serialize};
use serde_hashkey::{from_key, to_key, Error, Key};
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Author {
name: String,
age: u32,
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Book {
title: String,
author: Author,
}
let book = Book {
title: String::from("Birds of a feather"),
author: Author {
name: String::from("Noah"),
age: 42,
},
};
let key = to_key(&book)?;
let mut ratings = HashMap::new();
ratings.insert(key.clone(), 5);
println!("ratings: {:?}", ratings);
println!(
"book as json (through key): {}",
serde_json::to_string_pretty(&key)?
);
println!(
"book as json (through original object): {}",
serde_json::to_string_pretty(&book)?
);
```
[totally ordered nor hashable]: https://internals.rust-lang.org/t/f32-f64-should-implement-hash/5436
[Key]: https://docs.rs/serde-hashkey/latest/serde_hashkey/enum.Key.html
[to_key]: https://docs.rs/serde-hashkey/latest/serde_hashkey/fn.to_key.html
[RejectFloatPolicy]: https://docs.rs/serde-hashkey/latest/serde_hashkey/struct.RejectFloatPolicy.html
[OrderedFloat]: https://docs.rs/serde-hashkey/latest/serde_hashkey/struct.OrderedFloat.html
[to_key_with_ordered_float]: https://docs.rs/serde-hashkey/latest/serde_hashkey/fn.to_key_with_ordered_float.html
[`ordered-float` crate]: https://docs.rs/ordered-float/2/ordered_float/