Add Serialize/Deserialize for std types that provide interior mutability
Fixes #179
This commit is contained in:
parent
1232cfd194
commit
1c9478bfa6
@ -43,6 +43,11 @@ use alloc::arc::Arc;
|
||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||
use alloc::boxed::Box;
|
||||
|
||||
use core::cell::{Cell, RefCell};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::sync::{Mutex, RwLock};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::time::Duration;
|
||||
|
||||
@ -1106,6 +1111,44 @@ impl<'a, T: ?Sized> Deserialize for Cow<'a, T>
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deserialize + Copy> Deserialize for Cell<T> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Cell<T>, D::Error>
|
||||
where D: Deserializer
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(Cell::new(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deserialize> Deserialize for RefCell<T> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<RefCell<T>, D::Error>
|
||||
where D: Deserializer
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(RefCell::new(val))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T: Deserialize> Deserialize for Mutex<T> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Mutex<T>, D::Error>
|
||||
where D: Deserializer
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(Mutex::new(val))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T: Deserialize> Deserialize for RwLock<T> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<RwLock<T>, D::Error>
|
||||
where D: Deserializer
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(RwLock::new(val))
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// This is a cleaned-up version of the impl generated by:
|
||||
|
@ -64,6 +64,10 @@
|
||||
//! - Rc\<T\>
|
||||
//! - Arc\<T\>
|
||||
//! - Cow\<'a, T\>
|
||||
//! - Cell\<T\>
|
||||
//! - RefCell\<T\>
|
||||
//! - Mutex\<T\>
|
||||
//! - RwLock\<T\>
|
||||
//! - **Collection types**:
|
||||
//! - BTreeMap\<K, V\>
|
||||
//! - BTreeSet\<T\>
|
||||
|
@ -36,6 +36,11 @@ use alloc::arc::Arc;
|
||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||
use alloc::boxed::Box;
|
||||
|
||||
use core::cell::{Cell, RefCell};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::sync::{Mutex, RwLock};
|
||||
|
||||
use core::marker::PhantomData;
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
@ -586,6 +591,58 @@ impl<'a, T: ?Sized> Serialize for Cow<'a, T>
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for Cell<T>
|
||||
where T: Serialize + Copy
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
{
|
||||
self.get().serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for RefCell<T>
|
||||
where T: Serialize
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
{
|
||||
self.borrow().serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T> Serialize for Mutex<T>
|
||||
where T: Serialize
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
{
|
||||
match self.lock() {
|
||||
Ok(locked) => locked.serialize(serializer),
|
||||
Err(_) => Err(S::Error::custom("lock poison error while serializing")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T> Serialize for RwLock<T>
|
||||
where T: Serialize
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer
|
||||
{
|
||||
match self.read() {
|
||||
Ok(locked) => locked.serialize(serializer),
|
||||
Err(_) => Err(S::Error::custom("lock poison error while serializing")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<T, E> Serialize for Result<T, E>
|
||||
|
@ -61,6 +61,10 @@
|
||||
//! - Rc\<T\>
|
||||
//! - Arc\<T\>
|
||||
//! - Cow\<'a, T\>
|
||||
//! - Cell\<T\>
|
||||
//! - RefCell\<T\>
|
||||
//! - Mutex\<T\>
|
||||
//! - RwLock\<T\>
|
||||
//! - **Collection types**:
|
||||
//! - BTreeMap\<K, V\>
|
||||
//! - BTreeSet\<T\>
|
||||
|
Loading…
Reference in New Issue
Block a user