Skip to content

Commit 25ab84d

Browse files
authored
Merge pull request #957 from serde-rs/alloc
Merge crate `collections` into `alloc`
2 parents b37d47c + e43d3f3 commit 25ab84d

File tree

11 files changed

+79
-91
lines changed

11 files changed

+79
-91
lines changed

serde/Cargo.toml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,15 @@ std = []
4848
# https://github.com/serde-rs/serde/issues/812
4949
unstable = []
5050

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

59-
# Provide impls for collection types like String and Cow<T>. This is a subset of
60-
# std but may be enabled without depending on all of std.
61-
#
62-
# Requires a dependency on the unstable collections library:
63-
#
64-
# https://doc.rust-lang.org/collections/
65-
collections = ["alloc"]
66-
6760
# Opt into impls for Rc<T> and Arc<T>. Serializing and deserializing these types
6861
# does not preserve identity and may result in multiple copies of the same data.
6962
# Be sure that this is what you want before enabling this feature.

serde/src/de/impls.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ use lib::*;
1111
use de::{Deserialize, Deserializer, EnumAccess, Error, SeqAccess, Unexpected, VariantAccess,
1212
Visitor};
1313

14-
#[cfg(any(feature = "std", feature = "collections"))]
14+
#[cfg(any(feature = "std", feature = "alloc"))]
1515
use de::MapAccess;
1616

1717
use de::from_primitive::FromPrimitive;
1818

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

2222
////////////////////////////////////////////////////////////////////////////////
@@ -208,10 +208,10 @@ impl<'de> Deserialize<'de> for char {
208208

209209
////////////////////////////////////////////////////////////////////////////////
210210

211-
#[cfg(any(feature = "std", feature = "collections"))]
211+
#[cfg(any(feature = "std", feature = "alloc"))]
212212
struct StringVisitor;
213213

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

@@ -254,7 +254,7 @@ impl<'de> Visitor<'de> for StringVisitor {
254254
}
255255
}
256256

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

498498
////////////////////////////////////////////////////////////////////////////////
499499

500-
#[cfg(any(feature = "std", feature = "collections"))]
500+
#[cfg(any(feature = "std", feature = "alloc"))]
501501
macro_rules! seq_impl {
502502
(
503503
$ty:ident < T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)* >,
@@ -552,23 +552,23 @@ macro_rules! seq_impl {
552552
}
553553
}
554554

555-
#[cfg(any(feature = "std", feature = "collections"))]
555+
#[cfg(any(feature = "std", feature = "alloc"))]
556556
seq_impl!(
557557
BinaryHeap<T: Ord>,
558558
seq,
559559
BinaryHeap::new(),
560560
BinaryHeap::with_capacity(size_hint::cautious(seq.size_hint())),
561561
BinaryHeap::push);
562562

563-
#[cfg(any(feature = "std", feature = "collections"))]
563+
#[cfg(any(feature = "std", feature = "alloc"))]
564564
seq_impl!(
565565
BTreeSet<T: Eq + Ord>,
566566
seq,
567567
BTreeSet::new(),
568568
BTreeSet::new(),
569569
BTreeSet::insert);
570570

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

587-
#[cfg(any(feature = "std", feature = "collections"))]
587+
#[cfg(any(feature = "std", feature = "alloc"))]
588588
seq_impl!(
589589
Vec<T>,
590590
seq,
591591
Vec::new(),
592592
Vec::with_capacity(size_hint::cautious(seq.size_hint())),
593593
Vec::push);
594594

595-
#[cfg(any(feature = "std", feature = "collections"))]
595+
#[cfg(any(feature = "std", feature = "alloc"))]
596596
seq_impl!(
597597
VecDeque<T>,
598598
seq,
@@ -790,7 +790,7 @@ tuple_impls! {
790790

791791
////////////////////////////////////////////////////////////////////////////////
792792

793-
#[cfg(any(feature = "std", feature = "collections"))]
793+
#[cfg(any(feature = "std", feature = "alloc"))]
794794
macro_rules! map_impl {
795795
(
796796
$ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)* >,
@@ -846,7 +846,7 @@ macro_rules! map_impl {
846846
}
847847
}
848848

849-
#[cfg(any(feature = "std", feature = "collections"))]
849+
#[cfg(any(feature = "std", feature = "alloc"))]
850850
map_impl!(
851851
BTreeMap<K: Ord, V>,
852852
map,
@@ -1110,7 +1110,7 @@ where
11101110
}
11111111
}
11121112

1113-
#[cfg(any(feature = "std", feature = "collections"))]
1113+
#[cfg(any(feature = "std", feature = "alloc"))]
11141114
impl<'de, T> Deserialize<'de> for Box<[T]>
11151115
where
11161116
T: Deserialize<'de>,
@@ -1123,7 +1123,7 @@ where
11231123
}
11241124
}
11251125

1126-
#[cfg(any(feature = "std", feature = "collections"))]
1126+
#[cfg(any(feature = "std", feature = "alloc"))]
11271127
impl<'de> Deserialize<'de> for Box<str> {
11281128
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
11291129
where
@@ -1159,7 +1159,7 @@ where
11591159
}
11601160
}
11611161

1162-
#[cfg(any(feature = "std", feature = "collections"))]
1162+
#[cfg(any(feature = "std", feature = "alloc"))]
11631163
impl<'de, 'a, T: ?Sized> Deserialize<'de> for Cow<'a, T>
11641164
where
11651165
T: ToOwned,

serde/src/de/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ pub trait Visitor<'de>: Sized {
12621262
/// The default implementation forwards to `visit_str` and then drops the
12631263
/// `String`.
12641264
#[inline]
1265-
#[cfg(any(feature = "std", feature = "collections"))]
1265+
#[cfg(any(feature = "std", feature = "alloc"))]
12661266
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
12671267
where
12681268
E: Error,
@@ -1321,7 +1321,7 @@ pub trait Visitor<'de>: Sized {
13211321
///
13221322
/// The default implementation forwards to `visit_bytes` and then drops the
13231323
/// `Vec<u8>`.
1324-
#[cfg(any(feature = "std", feature = "collections"))]
1324+
#[cfg(any(feature = "std", feature = "alloc"))]
13251325
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
13261326
where
13271327
E: Error,

serde/src/de/value.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,21 @@ pub struct Error {
5151
err: ErrorImpl,
5252
}
5353

54-
#[cfg(any(feature = "std", feature = "collections"))]
54+
#[cfg(any(feature = "std", feature = "alloc"))]
5555
type ErrorImpl = Box<str>;
56-
#[cfg(not(any(feature = "std", feature = "collections")))]
56+
#[cfg(not(any(feature = "std", feature = "alloc")))]
5757
type ErrorImpl = ();
5858

5959
impl de::Error for Error {
60-
#[cfg(any(feature = "std", feature = "collections"))]
60+
#[cfg(any(feature = "std", feature = "alloc"))]
6161
fn custom<T>(msg: T) -> Self
6262
where
6363
T: Display,
6464
{
6565
Error { err: msg.to_string().into_boxed_str() }
6666
}
6767

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

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

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

427427
/// A deserializer holding a `String`.
428-
#[cfg(any(feature = "std", feature = "collections"))]
428+
#[cfg(any(feature = "std", feature = "alloc"))]
429429
#[derive(Clone, Debug)]
430430
pub struct StringDeserializer<E> {
431431
value: String,
432432
marker: PhantomData<E>,
433433
}
434434

435-
#[cfg(any(feature = "std", feature = "collections"))]
435+
#[cfg(any(feature = "std", feature = "alloc"))]
436436
impl<'de, E> IntoDeserializer<'de, E> for String
437437
where
438438
E: de::Error,
@@ -447,7 +447,7 @@ where
447447
}
448448
}
449449

450-
#[cfg(any(feature = "std", feature = "collections"))]
450+
#[cfg(any(feature = "std", feature = "alloc"))]
451451
impl<'de, E> de::Deserializer<'de> for StringDeserializer<E>
452452
where
453453
E: de::Error,
@@ -482,7 +482,7 @@ where
482482
}
483483
}
484484

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

503503
/// A deserializer holding a `Cow<str>`.
504-
#[cfg(any(feature = "std", feature = "collections"))]
504+
#[cfg(any(feature = "std", feature = "alloc"))]
505505
#[derive(Clone, Debug)]
506506
pub struct CowStrDeserializer<'a, E> {
507507
value: Cow<'a, str>,
508508
marker: PhantomData<E>,
509509
}
510510

511-
#[cfg(any(feature = "std", feature = "collections"))]
511+
#[cfg(any(feature = "std", feature = "alloc"))]
512512
impl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, str>
513513
where
514514
E: de::Error,
@@ -523,7 +523,7 @@ where
523523
}
524524
}
525525

526-
#[cfg(any(feature = "std", feature = "collections"))]
526+
#[cfg(any(feature = "std", feature = "alloc"))]
527527
impl<'de, 'a, E> de::Deserializer<'de> for CowStrDeserializer<'a, E>
528528
where
529529
E: de::Error,
@@ -561,7 +561,7 @@ where
561561
}
562562
}
563563

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

728728
////////////////////////////////////////////////////////////////////////////////
729729

730-
#[cfg(any(feature = "std", feature = "collections"))]
730+
#[cfg(any(feature = "std", feature = "alloc"))]
731731
impl<'de, T, E> IntoDeserializer<'de, E> for Vec<T>
732732
where
733733
T: IntoDeserializer<'de, E>,
@@ -740,7 +740,7 @@ where
740740
}
741741
}
742742

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

11461146
////////////////////////////////////////////////////////////////////////////////
11471147

1148-
#[cfg(any(feature = "std", feature = "collections"))]
1148+
#[cfg(any(feature = "std", feature = "alloc"))]
11491149
impl<'de, K, V, E> IntoDeserializer<'de, E> for BTreeMap<K, V>
11501150
where
11511151
K: IntoDeserializer<'de, E> + Eq + Ord,

serde/src/export.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub use self::string::from_utf8_lossy;
1919
mod string {
2020
use lib::*;
2121

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

0 commit comments

Comments
 (0)