Item

Struct Item 

Source
pub struct Item<T>(/* private fields */);
Expand description

TODO Docuemnt

Implementations§

Source§

impl<T> Item<T>

Source

pub fn try_get(&self) -> Option<&T>
where T: for<'de> Deserialize<'de> + 'static,

Attempt to get an immutable reference to the deserialized value of type T. Will return None if the value has not yet been deserialized, as we need a mutable reference to mutate the entry to save the deserialized value.

§Example
use anymap_serde::SerializableAnyMap;
use serde_json::json;

let mut map: SerializableAnyMap = serde_json::from_value(json!({ "u16": 42 })).unwrap();
let mut old = map.insert(43u16).unwrap();

assert_eq!(old.try_get(), None); // not deserialized yet
old.get(); // Trigger deserialization
assert_eq!(old.try_get(), Some(&42));
Source

pub fn get(&mut self) -> Result<&T, DeserializerError>
where T: Serialize + for<'de> Deserialize<'de> + 'static,

Attempt to get an immutable reference to the deserialized value of type T, lazily deserializing if necessary. We need a mutable reference in order to save the deserialized value to the entry

§Example
use anymap_serde::SerializableAnyMap;
use serde_json::json;

let mut map: SerializableAnyMap = serde_json::from_value(json!({ "u32": 42 })).unwrap();
let mut old = map.insert(43u32).unwrap();

assert_eq!(old.get().ok(), Some(&42));

let mut map: SerializableAnyMap = serde_json::from_value(json!({ "u32": "boom" })).unwrap();
let mut old = map.insert(43u32).unwrap();

assert_eq!(old.get().is_err(), true);
assert_eq!(old.get().is_err(), true);
Source

pub fn get_mut<'a>(&'a mut self) -> Result<WriteGuard<'a, T>, DeserializerError>
where T: for<'de> Deserialize<'de> + Serialize + 'static,

Attempt to an mutable reference to the deserialized value of type T, lazily deserializing if necessary.

§Example
use anymap_serde::SerializableAnyMap;
use serde_json::json;
use std::ops::DerefMut;
let mut map: SerializableAnyMap = serde_json::from_value(json!({ "u16": 42 })).unwrap();

let mut old = map.insert(43u16).unwrap();

*old.get_mut().unwrap() += 100;

assert_eq!(old.get_mut().unwrap().deref_mut(), &mut 142u16);
Source

pub fn into_inner(self) -> Result<T, DeserializerError>
where T: for<'de> Deserialize<'de> + 'static,

Attempt to extract the inner value by deserializing if necessary.

§Example
use anymap_serde::SerializableAnyMap;
use serde_json::json;

let mut map: SerializableAnyMap = serde_json::from_value(json!({ "u32": 42 })).unwrap();
let item = map.insert(41u32).unwrap();

assert_eq!(item.into_inner().unwrap(), 42u32);

Auto Trait Implementations§

§

impl<T> Freeze for Item<T>

§

impl<T> !RefUnwindSafe for Item<T>

§

impl<T> !Send for Item<T>

§

impl<T> !Sync for Item<T>

§

impl<T> Unpin for Item<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Item<T>

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> 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, 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.