mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-23 08:18:03 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ae79451b7a | |||
| 1a2b3815ef | |||
| 1d6ecf3c2c | |||
| 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.15"
|
||||
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" }
|
||||
|
||||
+75
-11
@@ -20,10 +20,14 @@ use core::fmt;
|
||||
#[cfg(feature = "std")]
|
||||
use core::hash::{Hash, BuildHasher};
|
||||
use core::marker::PhantomData;
|
||||
#[cfg(all(feature="unstable"))]
|
||||
use core::mem;
|
||||
#[cfg(feature = "std")]
|
||||
use std::net;
|
||||
#[cfg(feature = "std")]
|
||||
use std::path;
|
||||
#[cfg(all(feature="unstable"))]
|
||||
use core::slice;
|
||||
use core::str;
|
||||
#[cfg(feature = "std")]
|
||||
use std::ffi::{CString, OsString};
|
||||
@@ -43,6 +47,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;
|
||||
|
||||
@@ -52,10 +61,6 @@ use std;
|
||||
#[cfg(feature = "unstable")]
|
||||
use core::nonzero::{NonZero, Zeroable};
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
#[allow(deprecated)] // required for impl Deserialize for NonZero<T>
|
||||
use core::num::Zero;
|
||||
|
||||
use de::{Deserialize, Deserializer, EnumVisitor, Error, MapVisitor, SeqVisitor, Unexpected,
|
||||
VariantVisitor, Visitor};
|
||||
use de::from_primitive::FromPrimitive;
|
||||
@@ -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:
|
||||
@@ -1350,18 +1409,23 @@ impl<Idx: Deserialize> Deserialize for std::ops::Range<Idx> {
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
#[allow(deprecated)] // num::Zero is deprecated but there is no replacement
|
||||
impl<T> Deserialize for NonZero<T>
|
||||
where T: Deserialize + PartialEq + Zeroable + Zero
|
||||
where T: Deserialize + Zeroable
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<NonZero<T>, D::Error>
|
||||
where D: Deserializer
|
||||
{
|
||||
let value = try!(Deserialize::deserialize(deserializer));
|
||||
if value == Zero::zero() {
|
||||
return Err(Error::custom("expected a non-zero value"));
|
||||
}
|
||||
unsafe { Ok(NonZero::new(value)) }
|
||||
unsafe {
|
||||
let ptr = &value as *const T as *const u8;
|
||||
if slice::from_raw_parts(ptr, mem::size_of::<T>()).iter().all(|&b| b == 0) {
|
||||
return Err(Error::custom("expected a non-zero value"));
|
||||
}
|
||||
// Waiting for a safe way to construct NonZero<T>:
|
||||
// https://github.com/rust-lang/rust/issues/27730#issuecomment-269726075
|
||||
Ok(NonZero::new(value))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
-1
@@ -61,7 +61,7 @@
|
||||
|
||||
#![doc(html_root_url="https://docs.serde.rs")]
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![cfg_attr(feature = "unstable", feature(nonzero, specialization, zero_one, into_boxed_c_str))]
|
||||
#![cfg_attr(feature = "unstable", feature(nonzero, specialization, into_boxed_c_str))]
|
||||
#![cfg_attr(feature = "alloc", feature(alloc))]
|
||||
#![cfg_attr(feature = "collections", feature(collections))]
|
||||
#![cfg_attr(feature = "cargo-clippy", allow(linkedlist, type_complexity, doc_markdown))]
|
||||
|
||||
+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.15"
|
||||
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.15"
|
||||
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