Enum serde_hashkey::Key

source ·
pub enum Key<F = RejectFloatPolicy>where
    F: FloatPolicy,
{ Unit, Bool(bool), Integer(Integer), Float(Float<F>), Bytes(Box<[u8]>), String(Box<str>), Seq(Box<[Key<F>]>), Map(Box<[(Key<F>, Key<F>)]>), }
Expand description

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

This can be serialized to a type implementing serde::Deserialize using from_key, and deserialized from a type implementing serde::Serialize using to_key. See the corresponding function for documentation.

The type parameter F corresponds to the FloatPolicy in used. It defaults to RejectFloatPolicy which will cause floats to be rejected.

Examples

use serde_derive::{Deserialize, Serialize};
use serde_hashkey::{to_key, to_key_with_ordered_float};

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

let key = to_key(&Author {
    name: String::from("Jane Doe"),
    age: 42,
})?;

// Note: serializing floats will fail under the default policy, but succeed
// under one supporting floats.
assert!(to_key(&42.0f32).is_err());
assert!(to_key_with_ordered_float(&42.0f32).is_ok());

Variants§

§

Unit

A unit value.

§

Bool(bool)

A boolean value.

§

Integer(Integer)

An integer.

§

Float(Float<F>)

A 32-bit floating-point number.

§

Bytes(Box<[u8]>)

A byte array.

§

String(Box<str>)

A string.

§

Seq(Box<[Key<F>]>)

A vector.

§

Map(Box<[(Key<F>, Key<F>)]>)

A map.

Implementations§

Normalize the key, making sure that all contained maps are sorted.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Deserialize implementation for a Key.

This allows keys to be serialized immediately.

Examples

use serde_derive::Deserialize;
use serde_hashkey::{Key, OrderedFloatPolicy};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize)]
struct Foo {
    key: Key<OrderedFloatPolicy>,
}

let foo: Foo = serde_json::from_str("{\"key\": 42.42}")?;

assert!(matches!(foo.key, Key::Float(..)));
Ok(())
Deserialize this value from the given Serde deserializer. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
The type of the deserializer being converted into.
Convert this value into a deserializer.
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Serialize implementation for a Key.

This allows keys to be serialized immediately.

Examples

use serde_derive::Serialize;
use serde_hashkey::{Key, OrderedFloatPolicy, OrderedFloat, Float};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
struct Foo {
    key: Key<OrderedFloatPolicy>,
}

let foo: String = serde_json::to_string(&Foo { key: Key::Float(Float::F64(OrderedFloat(42.42f64))) })?;

assert_eq!(foo, "{\"key\":42.42}");
Ok(())
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.