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§
Trait Implementations§
source§impl<'de, F> Deserialize<'de> for Key<F>where
F: FloatPolicy,
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>,
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<'de, F> IntoDeserializer<'de, Error> for &'de Key<F>where
F: FloatPolicy,
impl<'de, F> IntoDeserializer<'de, Error> for &'de Key<F>where
F: FloatPolicy,
§type Deserializer = Deserializer<'de, F>
type Deserializer = Deserializer<'de, F>
The type of the deserializer being converted into.
source§fn into_deserializer(self) -> Self::Deserializer
fn into_deserializer(self) -> Self::Deserializer
Convert this value into a deserializer.
source§impl<F> Ord for Key<F>where
F: FloatPolicy + Ord,
impl<F> Ord for Key<F>where
F: FloatPolicy + Ord,
source§impl<F> PartialEq<Key<F>> for Key<F>where
F: FloatPolicy + PartialEq,
impl<F> PartialEq<Key<F>> for Key<F>where
F: FloatPolicy + PartialEq,
source§impl<F> PartialOrd<Key<F>> for Key<F>where
F: FloatPolicy + PartialOrd,
impl<F> PartialOrd<Key<F>> for Key<F>where
F: FloatPolicy + PartialOrd,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl<F> Serialize for Key<F>where
F: FloatPolicy,
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(())