#serialization #serde #key-value-store #serialize-deserialize #hashing #serde-derive #in-memory

serde-hashkey

Space efficient, in-memory serde serialization which supports hashing

11 releases

0.4.5 Jan 2, 2023
0.4.4 Nov 24, 2022
0.4.3 Oct 11, 2022
0.4.0 Dec 29, 2020
0.2.2 Oct 4, 2019

#304 in Encoding

Download history 665/week @ 2024-06-15 555/week @ 2024-06-22 235/week @ 2024-06-29 481/week @ 2024-07-06 506/week @ 2024-07-13 1614/week @ 2024-07-20 1602/week @ 2024-07-27 867/week @ 2024-08-03 673/week @ 2024-08-10 1408/week @ 2024-08-17 785/week @ 2024-08-24 826/week @ 2024-08-31 708/week @ 2024-09-07 702/week @ 2024-09-14 459/week @ 2024-09-21 663/week @ 2024-09-28

2,844 downloads per month
Used in 9 crates (6 directly)

MIT/Apache

60KB
1.5K SLoC

serde-hashkey

github crates.io docs.rs build status

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:

[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:


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

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)?
);

Dependencies

~110–400KB