mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-23 08:18:03 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d4d2061a2e | |||
| c202ce13dc | |||
| 1c9478bfa6 | |||
| 1232cfd194 | |||
| 2c49f9aad3 | |||
| 1b763da529 | |||
| b7d6c5d9f7 | |||
| bfabaf3789 | |||
| cf6c4ab7ec | |||
| 8eb50186e0 | |||
| 7d985ff3fd |
@@ -15,10 +15,15 @@ You may be looking for:
|
||||
|
||||
## Serde in action
|
||||
|
||||
<a href="http://play.integer32.com/?gist=9003c5b88c1f4989941925d7190c6eec" target="_blank">
|
||||
<img align="right" width="50" src="https://raw.githubusercontent.com/serde-rs/serde-rs.github.io/master/img/run.png">
|
||||
</a>
|
||||
|
||||
```rust
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde"
|
||||
version = "0.9.12"
|
||||
version = "0.9.14"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "A generic serialization/deserialization framework"
|
||||
@@ -29,7 +29,7 @@ unstable-testing = ["unstable", "std"]
|
||||
playground = ["serde_derive"]
|
||||
|
||||
[dependencies]
|
||||
serde_derive = { version = "0.9", optional = true }
|
||||
serde_derive = { version = "0.9", optional = true, path = "../serde_derive" }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_derive = "0.9"
|
||||
serde_derive = { version = "0.9", path = "../serde_derive" }
|
||||
|
||||
+60
-1
@@ -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;
|
||||
|
||||
@@ -1009,7 +1014,23 @@ impl Visitor for OsStringVisitor {
|
||||
variant.visit_newtype().map(OsString::from_vec)
|
||||
}
|
||||
(OsStringKind::Windows, _) => {
|
||||
Err(Error::custom("cannot deserialize windows os string on unix"))
|
||||
Err(Error::custom("cannot deserialize Windows OS string on Unix"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn visit_enum<V>(self, visitor: V) -> Result<OsString, V::Error>
|
||||
where V: EnumVisitor,
|
||||
{
|
||||
use std::os::windows::ffi::OsStringExt;
|
||||
|
||||
match try!(visitor.visit_variant()) {
|
||||
(OsStringKind::Windows, variant) => {
|
||||
variant.visit_newtype::<Vec<u16>>().map(|vec| OsString::from_wide(&vec))
|
||||
}
|
||||
(OsStringKind::Unix, _) => {
|
||||
Err(Error::custom("cannot deserialize Unix OS string on Windows"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1090,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\>
|
||||
|
||||
+1
-16
@@ -1,24 +1,9 @@
|
||||
#[cfg(feature = "std")]
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! forward_to_deserialize_method {
|
||||
($func:ident($($arg:ty),*)) => {
|
||||
#[inline]
|
||||
fn $func<__V>(self, $(_: $arg,)* visitor: __V) -> ::std::result::Result<__V::Value, Self::Error>
|
||||
where __V: $crate::de::Visitor
|
||||
{
|
||||
self.deserialize(visitor)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! forward_to_deserialize_method {
|
||||
($func:ident($($arg:ty),*)) => {
|
||||
#[inline]
|
||||
fn $func<__V>(self, $(_: $arg,)* visitor: __V) -> ::core::result::Result<__V::Value, Self::Error>
|
||||
fn $func<__V>(self, $(_: $arg,)* visitor: __V) -> $crate::export::Result<__V::Value, Self::Error>
|
||||
where __V: $crate::de::Visitor
|
||||
{
|
||||
self.deserialize(visitor)
|
||||
|
||||
@@ -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\>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_derive"
|
||||
version = "0.9.12"
|
||||
version = "0.9.14"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_test"
|
||||
version = "0.9.12"
|
||||
version = "0.9.14"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Token De/Serializer for testing De/Serialize implementations"
|
||||
|
||||
Reference in New Issue
Block a user