pub fn from_key<T, F>(value: &Key<F>) -> Result<T, Error>where
T: DeserializeOwned,
F: FloatPolicy,
Expand description
Deserialize the given type from a Key.
ยงExamples
use serde_derive::{Deserialize, Serialize};
use serde_hashkey::{from_key, to_key, Key};
use std::collections::HashMap;
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Author {
name: String,
age: u32,
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Book {
title: String,
author: Author,
}
let book = Book {
title: String::from("Birds of a feather"),
author: Author {
name: String::from("Noah"),
age: 42,
},
};
let key = to_key(&book)?;
let book2 = from_key(&key)?;
assert_eq!(book, book2);
Using a non-standard float policy:
use serde_derive::{Deserialize, Serialize};
use serde_hashkey::{from_key, to_key_with_ordered_float, OrderedFloat, Key};
use std::collections::HashMap;
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Author {
name: String,
age: u32,
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Book {
title: String,
author: Author,
}
let book = Book {
title: String::from("Birds of a feather"),
author: Author {
name: String::from("Noah"),
age: 42,
},
};
let key = to_key_with_ordered_float(&book)?;
let book2 = from_key(&key)?;
assert_eq!(book, book2);