Skip to content

Commit f386930

Browse files
committed
feat: extract serde_core out of serde
1 parent babafa5 commit f386930

27 files changed

+898
-219
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
22
members = [
33
"serde",
4+
"serde_core",
45
"serde_derive",
56
"serde_derive_internals",
67
"test_suite",

serde/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ repository = "https://github.com/serde-rs/serde"
1515
rust-version = "1.56"
1616

1717
[dependencies]
18+
serde_core = { version = "=1.0.219", path = "../serde_core", default-features = false }
1819
serde_derive = { version = "1", optional = true, path = "../serde_derive" }
1920

2021
[dev-dependencies]
@@ -52,20 +53,20 @@ derive = ["serde_derive"]
5253

5354
# Provide impls for common standard library types like Vec<T> and HashMap<K, V>.
5455
# Requires a dependency on the Rust standard library.
55-
std = []
56+
std = ["serde_core/std"]
5657

5758
# Provide impls for types that require unstable functionality. For tracking and
5859
# discussion of unstable functionality please refer to this issue:
5960
#
6061
# https://github.com/serde-rs/serde/issues/812
61-
unstable = []
62+
unstable = ["serde_core/unstable"]
6263

6364
# Provide impls for types in the Rust core allocation and collections library
6465
# including String, Box<T>, Vec<T>, and Cow<T>. This is a subset of std but may
6566
# be enabled without depending on all of std.
66-
alloc = []
67+
alloc = ["serde_core/alloc"]
6768

6869
# Opt into impls for Rc<T> and Arc<T>. Serializing and deserializing these types
6970
# does not preserve identity and may result in multiple copies of the same data.
7071
# Be sure that this is what you want before enabling this feature.
71-
rc = []
72+
rc = ["serde_core/rc"]

serde/build.rs

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,75 +14,13 @@ fn main() {
1414
};
1515

1616
if minor >= 77 {
17-
println!("cargo:rustc-check-cfg=cfg(no_core_cstr)");
18-
println!("cargo:rustc-check-cfg=cfg(no_core_error)");
19-
println!("cargo:rustc-check-cfg=cfg(no_core_net)");
20-
println!("cargo:rustc-check-cfg=cfg(no_core_num_saturating)");
21-
println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)");
2217
println!("cargo:rustc-check-cfg=cfg(no_serde_derive)");
23-
println!("cargo:rustc-check-cfg=cfg(no_std_atomic)");
24-
println!("cargo:rustc-check-cfg=cfg(no_std_atomic64)");
25-
println!("cargo:rustc-check-cfg=cfg(no_target_has_atomic)");
26-
}
27-
28-
let target = env::var("TARGET").unwrap();
29-
let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten";
30-
31-
// Support for #[cfg(target_has_atomic = "...")] stabilized in Rust 1.60.
32-
if minor < 60 {
33-
println!("cargo:rustc-cfg=no_target_has_atomic");
34-
// Allowlist of archs that support std::sync::atomic module. This is
35-
// based on rustc's compiler/rustc_target/src/spec/*.rs.
36-
let has_atomic64 = target.starts_with("x86_64")
37-
|| target.starts_with("i686")
38-
|| target.starts_with("aarch64")
39-
|| target.starts_with("powerpc64")
40-
|| target.starts_with("sparc64")
41-
|| target.starts_with("mips64el")
42-
|| target.starts_with("riscv64");
43-
let has_atomic32 = has_atomic64 || emscripten;
44-
if minor < 34 || !has_atomic64 {
45-
println!("cargo:rustc-cfg=no_std_atomic64");
46-
}
47-
if minor < 34 || !has_atomic32 {
48-
println!("cargo:rustc-cfg=no_std_atomic");
49-
}
5018
}
5119

5220
// Current minimum supported version of serde_derive crate is Rust 1.61.
5321
if minor < 61 {
5422
println!("cargo:rustc-cfg=no_serde_derive");
5523
}
56-
57-
// Support for core::ffi::CStr and alloc::ffi::CString stabilized in Rust 1.64.
58-
// https://blog.rust-lang.org/2022/09/22/Rust-1.64.0.html#c-compatible-ffi-types-in-core-and-alloc
59-
if minor < 64 {
60-
println!("cargo:rustc-cfg=no_core_cstr");
61-
}
62-
63-
// Support for core::num::Saturating and std::num::Saturating stabilized in Rust 1.74
64-
// https://blog.rust-lang.org/2023/11/16/Rust-1.74.0.html#stabilized-apis
65-
if minor < 74 {
66-
println!("cargo:rustc-cfg=no_core_num_saturating");
67-
}
68-
69-
// Support for core::net stabilized in Rust 1.77.
70-
// https://blog.rust-lang.org/2024/03/21/Rust-1.77.0.html
71-
if minor < 77 {
72-
println!("cargo:rustc-cfg=no_core_net");
73-
}
74-
75-
// Support for the `#[diagnostic]` tool attribute namespace
76-
// https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html#diagnostic-attributes
77-
if minor < 78 {
78-
println!("cargo:rustc-cfg=no_diagnostic_namespace");
79-
}
80-
81-
// The Error trait became available in core in 1.81.
82-
// https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#coreerrorerror
83-
if minor < 81 {
84-
println!("cargo:rustc-cfg=no_core_error");
85-
}
8624
}
8725

8826
fn rustc_minor_version() -> Option<u32> {

serde/src/lib.rs

Lines changed: 4 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -175,23 +175,18 @@ mod lib {
175175
}
176176

177177
pub use self::core::{f32, f64};
178-
pub use self::core::{iter, num, ptr, str};
178+
pub use self::core::{ptr, str};
179179

180180
#[cfg(any(feature = "std", feature = "alloc"))]
181-
pub use self::core::{cmp, mem, slice};
181+
pub use self::core::slice;
182182

183-
pub use self::core::cell::{Cell, RefCell};
184183
pub use self::core::clone;
185-
pub use self::core::cmp::Reverse;
186184
pub use self::core::convert;
187185
pub use self::core::default;
188186
pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite};
189187
pub use self::core::marker::{self, PhantomData};
190-
pub use self::core::num::Wrapping;
191-
pub use self::core::ops::{Bound, Range, RangeFrom, RangeInclusive, RangeTo};
192188
pub use self::core::option;
193189
pub use self::core::result;
194-
pub use self::core::time::Duration;
195190

196191
#[cfg(all(feature = "alloc", not(feature = "std")))]
197192
pub use alloc::borrow::{Cow, ToOwned};
@@ -213,83 +208,15 @@ mod lib {
213208
#[cfg(feature = "std")]
214209
pub use std::boxed::Box;
215210

216-
#[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
217-
pub use alloc::rc::{Rc, Weak as RcWeak};
218-
#[cfg(all(feature = "rc", feature = "std"))]
219-
pub use std::rc::{Rc, Weak as RcWeak};
220-
221-
#[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
222-
pub use alloc::sync::{Arc, Weak as ArcWeak};
223-
#[cfg(all(feature = "rc", feature = "std"))]
224-
pub use std::sync::{Arc, Weak as ArcWeak};
225-
226-
#[cfg(all(feature = "alloc", not(feature = "std")))]
227-
pub use alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
228-
#[cfg(feature = "std")]
229-
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
230-
231-
#[cfg(all(not(no_core_cstr), not(feature = "std")))]
232-
pub use self::core::ffi::CStr;
233-
#[cfg(feature = "std")]
234-
pub use std::ffi::CStr;
235-
236-
#[cfg(all(not(no_core_cstr), feature = "alloc", not(feature = "std")))]
237-
pub use alloc::ffi::CString;
238-
#[cfg(feature = "std")]
239-
pub use std::ffi::CString;
240-
241-
#[cfg(all(not(no_core_net), not(feature = "std")))]
242-
pub use self::core::net;
243-
#[cfg(feature = "std")]
244-
pub use std::net;
245-
246211
#[cfg(feature = "std")]
247212
pub use std::error;
248-
249-
#[cfg(feature = "std")]
250-
pub use std::collections::{HashMap, HashSet};
251-
#[cfg(feature = "std")]
252-
pub use std::ffi::{OsStr, OsString};
253-
#[cfg(feature = "std")]
254-
pub use std::hash::{BuildHasher, Hash};
255-
#[cfg(feature = "std")]
256-
pub use std::io::Write;
257-
#[cfg(feature = "std")]
258-
pub use std::path::{Path, PathBuf};
259-
#[cfg(feature = "std")]
260-
pub use std::sync::{Mutex, RwLock};
261-
#[cfg(feature = "std")]
262-
pub use std::time::{SystemTime, UNIX_EPOCH};
263-
264-
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
265-
pub use std::sync::atomic::{
266-
AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8,
267-
AtomicUsize, Ordering,
268-
};
269-
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))]
270-
pub use std::sync::atomic::{AtomicI64, AtomicU64};
271-
272-
#[cfg(all(feature = "std", not(no_target_has_atomic)))]
273-
pub use std::sync::atomic::Ordering;
274-
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "8"))]
275-
pub use std::sync::atomic::{AtomicBool, AtomicI8, AtomicU8};
276-
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "16"))]
277-
pub use std::sync::atomic::{AtomicI16, AtomicU16};
278-
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "32"))]
279-
pub use std::sync::atomic::{AtomicI32, AtomicU32};
280-
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "64"))]
281-
pub use std::sync::atomic::{AtomicI64, AtomicU64};
282-
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "ptr"))]
283-
pub use std::sync::atomic::{AtomicIsize, AtomicUsize};
284-
285-
#[cfg(not(no_core_num_saturating))]
286-
pub use self::core::num::Saturating;
287213
}
288214

289215
// None of this crate's error handling needs the `From::from` error conversion
290216
// performed implicitly by the `?` operator or the standard library's `try!`
291217
// macro. This simplified macro gives a 5.5% improvement in compile time
292218
// compared to standard `try!`, and 9% improvement compared to `?`.
219+
#[allow(unused_macros)]
293220
macro_rules! tri {
294221
($expr:expr) => {
295222
match $expr {
@@ -301,33 +228,17 @@ macro_rules! tri {
301228

302229
////////////////////////////////////////////////////////////////////////////////
303230

304-
#[macro_use]
305-
mod macros;
306-
307-
#[macro_use]
308-
mod integer128;
309-
310-
pub mod de;
311-
pub mod ser;
312-
313-
mod format;
314-
315231
#[doc(inline)]
316232
pub use crate::de::{Deserialize, Deserializer};
317233
#[doc(inline)]
318234
pub use crate::ser::{Serialize, Serializer};
235+
pub use serde_core::*;
319236

320237
// Used by generated code and doc tests. Not public API.
321238
#[doc(hidden)]
322239
#[path = "private/mod.rs"]
323240
pub mod __private;
324241

325-
#[path = "de/seed.rs"]
326-
mod seed;
327-
328-
#[cfg(all(not(feature = "std"), no_core_error))]
329-
mod std_error;
330-
331242
// Re-export #[derive(Serialize, Deserialize)].
332243
//
333244
// The reason re-exporting is not enabled by default is that disabling it would

serde/src/private/de.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use self::content::{
1616
TagOrContentField, TagOrContentFieldVisitor, TaggedContentVisitor, UntaggedUnitVisitor,
1717
};
1818

19-
pub use crate::seed::InPlaceSeed;
19+
pub use serde_core::de::InPlaceSeed;
2020

2121
/// If the missing field is of type `Option<T>` then treat is as `None`,
2222
/// otherwise it is an error.
@@ -47,7 +47,7 @@ where
4747
visitor.visit_none()
4848
}
4949

50-
forward_to_deserialize_any! {
50+
serde_core::forward_to_deserialize_any! {
5151
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
5252
bytes byte_buf unit unit_struct newtype_struct seq tuple
5353
tuple_struct map struct enum identifier ignored_any
@@ -297,7 +297,7 @@ mod content {
297297
// Untagged and internally tagged enums are only supported in
298298
// self-describing formats.
299299
let visitor = ContentVisitor { value: PhantomData };
300-
deserializer.__deserialize_content(visitor)
300+
deserializer.deserialize_any(visitor)
301301
}
302302
}
303303

@@ -1496,14 +1496,6 @@ mod content {
14961496
drop(self);
14971497
visitor.visit_unit()
14981498
}
1499-
1500-
fn __deserialize_content<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1501-
where
1502-
V: Visitor<'de, Value = Content<'de>>,
1503-
{
1504-
let _ = visitor;
1505-
Ok(self.content)
1506-
}
15071499
}
15081500

15091501
impl<'de, E> ContentDeserializer<'de, E> {
@@ -2085,14 +2077,6 @@ mod content {
20852077
{
20862078
visitor.visit_unit()
20872079
}
2088-
2089-
fn __deserialize_content<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2090-
where
2091-
V: Visitor<'de, Value = Content<'de>>,
2092-
{
2093-
let _ = visitor;
2094-
Ok(self.content.clone())
2095-
}
20962080
}
20972081

20982082
impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E> {
@@ -2397,7 +2381,7 @@ where
23972381
visitor.visit_str(self.value)
23982382
}
23992383

2400-
forward_to_deserialize_any! {
2384+
serde_core::forward_to_deserialize_any! {
24012385
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
24022386
bytes byte_buf option unit unit_struct newtype_struct seq tuple
24032387
tuple_struct map struct enum identifier ignored_any
@@ -2422,7 +2406,7 @@ where
24222406
visitor.visit_borrowed_str(self.value)
24232407
}
24242408

2425-
forward_to_deserialize_any! {
2409+
serde_core::forward_to_deserialize_any! {
24262410
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
24272411
bytes byte_buf option unit unit_struct newtype_struct seq tuple
24282412
tuple_struct map struct enum identifier ignored_any

serde/src/private/mod.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,5 @@ pub use self::string::from_utf8_lossy;
2121
pub use crate::lib::{ToString, Vec};
2222

2323
mod string {
24-
use crate::lib::*;
25-
26-
#[cfg(any(feature = "std", feature = "alloc"))]
27-
pub fn from_utf8_lossy(bytes: &[u8]) -> Cow<str> {
28-
String::from_utf8_lossy(bytes)
29-
}
30-
31-
// The generated code calls this like:
32-
//
33-
// let value = &_serde::__private::from_utf8_lossy(bytes);
34-
// Err(_serde::de::Error::unknown_variant(value, VARIANTS))
35-
//
36-
// so it is okay for the return type to be different from the std case as long
37-
// as the above works.
38-
#[cfg(not(any(feature = "std", feature = "alloc")))]
39-
pub fn from_utf8_lossy(bytes: &[u8]) -> &str {
40-
// Three unicode replacement characters if it fails. They look like a
41-
// white-on-black question mark. The user will recognize it as invalid
42-
// UTF-8.
43-
str::from_utf8(bytes).unwrap_or("\u{fffd}\u{fffd}\u{fffd}")
44-
}
24+
pub use serde_core::from_utf8_lossy;
4525
}

0 commit comments

Comments
 (0)