serde_hashkey

Enum 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§

Source§

impl<F> Key<F>
where F: FloatPolicy + Ord,

Source

pub fn normalize(self) -> Self

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

Trait Implementations§

Source§

impl<F> Clone for Key<F>
where F: FloatPolicy + Clone,

Source§

fn clone(&self) -> Key<F>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F> Debug for Key<F>
where F: FloatPolicy + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Key

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, F> Deserialize<'de> for Key<F>
where F: FloatPolicy,

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(())
Source§

fn deserialize<D>(deserializer: D) -> Result<Key<F>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<F> From<String> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: String) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<Vec<(Key<F>, Key<F>)>> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: Vec<(Key<F>, Key<F>)>) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<Vec<Key<F>>> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: Vec<Key<F>>) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<Vec<u8>> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: Vec<u8>) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<bool> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: bool) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<i128> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: i128) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<i16> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: i16) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<i32> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: i32) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<i64> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: i64) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<i8> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: i8) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<u128> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: u128) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<u16> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: u16) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<u32> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: u32) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<u64> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: u64) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> From<u8> for Key<F>
where F: FloatPolicy,

Source§

fn from(v: u8) -> Key<F>

Converts to this type from the input type.
Source§

impl<F> Hash for Key<F>
where F: FloatPolicy + Hash,

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'de, F> IntoDeserializer<'de, Error> for &'de Key<F>
where F: FloatPolicy,

Source§

type Deserializer = Deserializer<'de, F>

The type of the deserializer being converted into.
Source§

fn into_deserializer(self) -> Self::Deserializer

Convert this value into a deserializer.
Source§

impl<F> Ord for Key<F>
where F: FloatPolicy + Ord,

Source§

fn cmp(&self, other: &Key<F>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<F> PartialEq for Key<F>

Source§

fn eq(&self, other: &Key<F>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<F> PartialOrd for Key<F>

Source§

fn partial_cmp(&self, other: &Key<F>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<F> Serialize for Key<F>
where F: FloatPolicy,

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(())
Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<F> Eq for Key<F>
where F: FloatPolicy + Eq,

Source§

impl<F> StructuralPartialEq for Key<F>
where F: FloatPolicy,

Auto Trait Implementations§

§

impl<F> Freeze for Key<F>
where <F as FloatPolicy>::F32: Freeze, <F as FloatPolicy>::F64: Freeze,

§

impl<F> RefUnwindSafe for Key<F>

§

impl<F> Send for Key<F>
where <F as FloatPolicy>::F32: Send, <F as FloatPolicy>::F64: Send,

§

impl<F> Sync for Key<F>
where <F as FloatPolicy>::F32: Sync, <F as FloatPolicy>::F64: Sync,

§

impl<F> Unpin for Key<F>
where <F as FloatPolicy>::F32: Unpin, <F as FloatPolicy>::F64: Unpin,

§

impl<F> UnwindSafe for Key<F>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,