[][src]Crate serde_hashkey

GitHub Actions Build Status Documentation

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 typical for things like caches or dependency injection frameworks.

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 serde_derive::{Deserialize, Serialize};
use serde_hashkey::{from_key, to_key, Error, Key};
use std::{collections::HashMap, error};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Author {
    name: String,
    age: u32,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Book {
    title: String,
    author: Author,
}

fn main() -> Result<(), Box<dyn error::Error>> {
    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)?
    );

    Ok(())
}

Structs

OrderedFloat

An opaque floating-point representation which has a total ordering. This is used by OrderedFloatPolicy.

OrderedFloatPolicy

A float serialization policy which delegates decisions to the ordered-float crate. This policy is used by the to_key_with_ordered_float function.

RejectFloatPolicy

A float serialization policy which rejects any attempt to serialize a float with an error. This policy is used by the to_key function.

Enums

Error

Errors that can occur during serialization and deserialization of a Key.

Float

An opaque float derived from a given policy.

Integer

An opaque integer.

Key

The central key type, which is an in-memory representation of all supported serde-serialized values.

NeverFloat

An uninhabitable type for float policies that cannot produce a value of the corresponding type. This is used by RejectFloatPolicy.

Traits

FloatPolicy

A policy for handling floating point types in a Key.

FloatRepr

Trait implemented by floating point types which can be used in a FloatPolicy. This is implemented by the type representing a float, typically a wrapper, and defines the protocol necessary to incorporate the floating point type T into the Key protocol.

Functions

from_key

Deserialize the given type from a Key.

to_key

Serialize the given value to a Key.

to_key_with_ordered_float

Serialize the given value to a Key using OrderedFloatPolicy.

Type Definitions

Result

Helper alias for a Result which already represents our local Error type.