Skip to content

Merge crate collections into alloc #957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions serde/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,15 @@ std = []
# https://github.com/serde-rs/serde/issues/812
unstable = []

# Provide impls for types that require memory allocation like Box<T> and Rc<T>.
# This is a subset of std but may be enabled without depending on all of std.
# Provide impls for types in the Rust core allocation and collections library
# including String, Box<T>, Vec<T>, and Cow<T>. This is a subset of std but may
# be enabled without depending on all of std.
#
# Requires a dependency on the unstable core allocation library:
#
# https://doc.rust-lang.org/alloc/
alloc = ["unstable"]

# Provide impls for collection types like String and Cow<T>. This is a subset of
# std but may be enabled without depending on all of std.
#
# Requires a dependency on the unstable collections library:
#
# https://doc.rust-lang.org/collections/
collections = ["alloc"]

# Opt into impls for Rc<T> and Arc<T>. Serializing and deserializing these types
# does not preserve identity and may result in multiple copies of the same data.
# Be sure that this is what you want before enabling this feature.
Expand Down
32 changes: 16 additions & 16 deletions serde/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use lib::*;
use de::{Deserialize, Deserializer, EnumAccess, Error, SeqAccess, Unexpected, VariantAccess,
Visitor};

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
use de::MapAccess;

use de::from_primitive::FromPrimitive;

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
use private::de::size_hint;

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -208,10 +208,10 @@ impl<'de> Deserialize<'de> for char {

////////////////////////////////////////////////////////////////////////////////

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
struct StringVisitor;

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de> Visitor<'de> for StringVisitor {
type Value = String;

Expand Down Expand Up @@ -254,7 +254,7 @@ impl<'de> Visitor<'de> for StringVisitor {
}
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de> Deserialize<'de> for String {
fn deserialize<D>(deserializer: D) -> Result<String, D::Error>
where
Expand Down Expand Up @@ -497,7 +497,7 @@ impl<'de, T> Deserialize<'de> for PhantomData<T> {

////////////////////////////////////////////////////////////////////////////////

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
macro_rules! seq_impl {
(
$ty:ident < T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)* >,
Expand Down Expand Up @@ -552,23 +552,23 @@ macro_rules! seq_impl {
}
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(
BinaryHeap<T: Ord>,
seq,
BinaryHeap::new(),
BinaryHeap::with_capacity(size_hint::cautious(seq.size_hint())),
BinaryHeap::push);

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(
BTreeSet<T: Eq + Ord>,
seq,
BTreeSet::new(),
BTreeSet::new(),
BTreeSet::insert);

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(
LinkedList<T>,
seq,
Expand All @@ -584,15 +584,15 @@ seq_impl!(
HashSet::with_capacity_and_hasher(size_hint::cautious(seq.size_hint()), S::default()),
HashSet::insert);

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(
Vec<T>,
seq,
Vec::new(),
Vec::with_capacity(size_hint::cautious(seq.size_hint())),
Vec::push);

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(
VecDeque<T>,
seq,
Expand Down Expand Up @@ -790,7 +790,7 @@ tuple_impls! {

////////////////////////////////////////////////////////////////////////////////

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
macro_rules! map_impl {
(
$ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)* >,
Expand Down Expand Up @@ -846,7 +846,7 @@ macro_rules! map_impl {
}
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
map_impl!(
BTreeMap<K: Ord, V>,
map,
Expand Down Expand Up @@ -1110,7 +1110,7 @@ where
}
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, T> Deserialize<'de> for Box<[T]>
where
T: Deserialize<'de>,
Expand All @@ -1123,7 +1123,7 @@ where
}
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de> Deserialize<'de> for Box<str> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down Expand Up @@ -1159,7 +1159,7 @@ where
}
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, 'a, T: ?Sized> Deserialize<'de> for Cow<'a, T>
where
T: ToOwned,
Expand Down
4 changes: 2 additions & 2 deletions serde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ pub trait Visitor<'de>: Sized {
/// The default implementation forwards to `visit_str` and then drops the
/// `String`.
#[inline]
#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: Error,
Expand Down Expand Up @@ -1321,7 +1321,7 @@ pub trait Visitor<'de>: Sized {
///
/// The default implementation forwards to `visit_bytes` and then drops the
/// `Vec<u8>`.
#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
where
E: Error,
Expand Down
34 changes: 17 additions & 17 deletions serde/src/de/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ pub struct Error {
err: ErrorImpl,
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
type ErrorImpl = Box<str>;
#[cfg(not(any(feature = "std", feature = "collections")))]
#[cfg(not(any(feature = "std", feature = "alloc")))]
type ErrorImpl = ();

impl de::Error for Error {
#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
fn custom<T>(msg: T) -> Self
where
T: Display,
{
Error { err: msg.to_string().into_boxed_str() }
}

#[cfg(not(any(feature = "std", feature = "collections")))]
#[cfg(not(any(feature = "std", feature = "alloc")))]
fn custom<T>(msg: T) -> Self
where
T: Display,
Expand All @@ -85,12 +85,12 @@ impl ser::Error for Error {
}

impl Display for Error {
#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
formatter.write_str(&self.err)
}

#[cfg(not(any(feature = "std", feature = "collections")))]
#[cfg(not(any(feature = "std", feature = "alloc")))]
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
formatter.write_str("Serde deserialization error")
}
Expand Down Expand Up @@ -425,14 +425,14 @@ where
////////////////////////////////////////////////////////////////////////////////

/// A deserializer holding a `String`.
#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
#[derive(Clone, Debug)]
pub struct StringDeserializer<E> {
value: String,
marker: PhantomData<E>,
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, E> IntoDeserializer<'de, E> for String
where
E: de::Error,
Expand All @@ -447,7 +447,7 @@ where
}
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, E> de::Deserializer<'de> for StringDeserializer<E>
where
E: de::Error,
Expand Down Expand Up @@ -482,7 +482,7 @@ where
}
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, 'a, E> de::EnumAccess<'de> for StringDeserializer<E>
where
E: de::Error,
Expand All @@ -501,14 +501,14 @@ where
////////////////////////////////////////////////////////////////////////////////

/// A deserializer holding a `Cow<str>`.
#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
#[derive(Clone, Debug)]
pub struct CowStrDeserializer<'a, E> {
value: Cow<'a, str>,
marker: PhantomData<E>,
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, str>
where
E: de::Error,
Expand All @@ -523,7 +523,7 @@ where
}
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, 'a, E> de::Deserializer<'de> for CowStrDeserializer<'a, E>
where
E: de::Error,
Expand Down Expand Up @@ -561,7 +561,7 @@ where
}
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, 'a, E> de::EnumAccess<'de> for CowStrDeserializer<'a, E>
where
E: de::Error,
Expand Down Expand Up @@ -727,7 +727,7 @@ impl Expected for ExpectedInSeq {

////////////////////////////////////////////////////////////////////////////////

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, T, E> IntoDeserializer<'de, E> for Vec<T>
where
T: IntoDeserializer<'de, E>,
Expand All @@ -740,7 +740,7 @@ where
}
}

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, T, E> IntoDeserializer<'de, E> for BTreeSet<T>
where
T: IntoDeserializer<'de, E> + Eq + Ord,
Expand Down Expand Up @@ -1145,7 +1145,7 @@ impl Expected for ExpectedInMap {

////////////////////////////////////////////////////////////////////////////////

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'de, K, V, E> IntoDeserializer<'de, E> for BTreeMap<K, V>
where
K: IntoDeserializer<'de, E> + Eq + Ord,
Expand Down
4 changes: 2 additions & 2 deletions serde/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use self::string::from_utf8_lossy;
mod string {
use lib::*;

#[cfg(any(feature = "std", feature = "collections"))]
#[cfg(any(feature = "std", feature = "alloc"))]
pub fn from_utf8_lossy(bytes: &[u8]) -> Cow<str> {
String::from_utf8_lossy(bytes)
}
Expand All @@ -31,7 +31,7 @@ mod string {
//
// so it is okay for the return type to be different from the std case as long
// as the above works.
#[cfg(not(any(feature = "std", feature = "collections")))]
#[cfg(not(any(feature = "std", feature = "alloc")))]
pub fn from_utf8_lossy(bytes: &[u8]) -> &str {
// Three unicode replacement characters if it fails. They look like a
// white-on-black question mark. The user will recognize it as invalid
Expand Down
Loading