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,
Deserialize implementation for a Key.
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,
Source§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> PartialOrd for Key<F>where
F: FloatPolicy + PartialOrd,
impl<F> PartialOrd for Key<F>where
F: FloatPolicy + PartialOrd,
Source§impl<F> Serialize for Key<F>where
F: FloatPolicy,
Serialize implementation for a Key.
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(())
impl<F> Eq for Key<F>where
F: FloatPolicy + Eq,
impl<F> StructuralPartialEq for Key<F>where
F: FloatPolicy,
Auto Trait Implementations§
impl<F> Freeze for Key<F>
impl<F> RefUnwindSafe for Key<F>
impl<F> Send for Key<F>
impl<F> Sync for Key<F>
impl<F> Unpin for Key<F>
impl<F> UnwindSafe for Key<F>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more