Skip to content

Commit 7c58427

Browse files
authored
Merge pull request #2991 from dtolnay/inlinecoredoc
Inline serde_core into serde in docsrs mode
2 parents 9d3410e + f8137c7 commit 7c58427

File tree

7 files changed

+269
-220
lines changed

7 files changed

+269
-220
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ jobs:
133133
- uses: dtolnay/rust-toolchain@nightly
134134
- uses: dtolnay/install@cargo-docs-rs
135135
- run: cargo docs-rs -p serde
136+
- run: cargo docs-rs -p serde_core
136137
- run: cargo docs-rs -p serde_derive
137138
- run: cargo docs-rs -p serde_derive_internals
138139

serde/build.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ use serde_core::__private$$ as serde_core_private;
1919
fn main() {
2020
println!("cargo:rerun-if-changed=build.rs");
2121

22+
if env::var_os("DOCS_RS").is_some() {
23+
println!("cargo:rustc-cfg=no_serde_core");
24+
}
25+
2226
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
2327
let patch_version = env::var("CARGO_PKG_VERSION_PATCH").unwrap();
2428
let module = PRIVATE.replace("$$", &patch_version);
@@ -30,8 +34,17 @@ fn main() {
3034
};
3135

3236
if minor >= 77 {
37+
println!("cargo:rustc-check-cfg=cfg(feature, values(\"result\"))");
38+
println!("cargo:rustc-check-cfg=cfg(no_core_cstr)");
39+
println!("cargo:rustc-check-cfg=cfg(no_core_error)");
40+
println!("cargo:rustc-check-cfg=cfg(no_core_net)");
41+
println!("cargo:rustc-check-cfg=cfg(no_core_num_saturating)");
3342
println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)");
43+
println!("cargo:rustc-check-cfg=cfg(no_serde_core)");
3444
println!("cargo:rustc-check-cfg=cfg(no_serde_derive)");
45+
println!("cargo:rustc-check-cfg=cfg(no_std_atomic)");
46+
println!("cargo:rustc-check-cfg=cfg(no_std_atomic64)");
47+
println!("cargo:rustc-check-cfg=cfg(no_target_has_atomic)");
3548
}
3649

3750
// Current minimum supported version of serde_derive crate is Rust 1.61.

serde/src/core

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../serde_core/src

serde/src/lib.rs

Lines changed: 82 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -168,79 +168,102 @@
168168
#[cfg(feature = "alloc")]
169169
extern crate alloc;
170170

171-
/// A facade around all the types we need from the `std`, `core`, and `alloc`
172-
/// crates. This avoids elaborate import wrangling having to happen in every
173-
/// module.
174-
mod lib {
175-
mod core {
176-
#[cfg(not(feature = "std"))]
177-
pub use core::*;
178-
#[cfg(feature = "std")]
179-
pub use std::*;
180-
}
171+
// Rustdoc has a lot of shortcomings related to cross-crate re-exports that make
172+
// the rendered documentation of serde_core traits in serde more challenging to
173+
// understand than the equivalent documentation of the same items in serde_core.
174+
// https://github.com/rust-lang/rust/labels/A-cross-crate-reexports
175+
// So, just for the purpose of docs.rs documentation, we inline the contents of
176+
// serde_core into serde. This sidesteps all the cross-crate rustdoc bugs.
177+
#[cfg(docsrs)]
178+
#[macro_use]
179+
#[path = "core/crate_root.rs"]
180+
mod crate_root;
181181

182-
pub use self::core::{f32, f64};
183-
pub use self::core::{ptr, str};
182+
#[cfg(docsrs)]
183+
#[macro_use]
184+
#[path = "core/macros.rs"]
185+
mod macros;
184186

185-
#[cfg(any(feature = "std", feature = "alloc"))]
186-
pub use self::core::slice;
187+
#[cfg(not(docsrs))]
188+
macro_rules! crate_root {
189+
() => {
190+
/// A facade around all the types we need from the `std`, `core`, and `alloc`
191+
/// crates. This avoids elaborate import wrangling having to happen in every
192+
/// module.
193+
mod lib {
194+
mod core {
195+
#[cfg(not(feature = "std"))]
196+
pub use core::*;
197+
#[cfg(feature = "std")]
198+
pub use std::*;
199+
}
187200

188-
pub use self::core::clone;
189-
pub use self::core::convert;
190-
pub use self::core::default;
191-
pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite};
192-
pub use self::core::marker::{self, PhantomData};
193-
pub use self::core::option;
194-
pub use self::core::result;
201+
pub use self::core::{f32, f64};
202+
pub use self::core::{ptr, str};
195203

196-
#[cfg(all(feature = "alloc", not(feature = "std")))]
197-
pub use alloc::borrow::{Cow, ToOwned};
198-
#[cfg(feature = "std")]
199-
pub use std::borrow::{Cow, ToOwned};
204+
#[cfg(any(feature = "std", feature = "alloc"))]
205+
pub use self::core::slice;
200206

201-
#[cfg(all(feature = "alloc", not(feature = "std")))]
202-
pub use alloc::string::{String, ToString};
203-
#[cfg(feature = "std")]
204-
pub use std::string::{String, ToString};
207+
pub use self::core::clone;
208+
pub use self::core::convert;
209+
pub use self::core::default;
210+
pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite};
211+
pub use self::core::marker::{self, PhantomData};
212+
pub use self::core::option;
213+
pub use self::core::result;
205214

206-
#[cfg(all(feature = "alloc", not(feature = "std")))]
207-
pub use alloc::vec::Vec;
208-
#[cfg(feature = "std")]
209-
pub use std::vec::Vec;
215+
#[cfg(all(feature = "alloc", not(feature = "std")))]
216+
pub use alloc::borrow::{Cow, ToOwned};
217+
#[cfg(feature = "std")]
218+
pub use std::borrow::{Cow, ToOwned};
210219

211-
#[cfg(all(feature = "alloc", not(feature = "std")))]
212-
pub use alloc::boxed::Box;
213-
#[cfg(feature = "std")]
214-
pub use std::boxed::Box;
215-
}
220+
#[cfg(all(feature = "alloc", not(feature = "std")))]
221+
pub use alloc::string::{String, ToString};
222+
#[cfg(feature = "std")]
223+
pub use std::string::{String, ToString};
224+
225+
#[cfg(all(feature = "alloc", not(feature = "std")))]
226+
pub use alloc::vec::Vec;
227+
#[cfg(feature = "std")]
228+
pub use std::vec::Vec;
216229

217-
// None of this crate's error handling needs the `From::from` error conversion
218-
// performed implicitly by the `?` operator or the standard library's `try!`
219-
// macro. This simplified macro gives a 5.5% improvement in compile time
220-
// compared to standard `try!`, and 9% improvement compared to `?`.
221-
#[cfg(not(no_serde_derive))]
222-
macro_rules! tri {
223-
($expr:expr) => {
224-
match $expr {
225-
Ok(val) => val,
226-
Err(err) => return Err(err),
230+
#[cfg(all(feature = "alloc", not(feature = "std")))]
231+
pub use alloc::boxed::Box;
232+
#[cfg(feature = "std")]
233+
pub use std::boxed::Box;
227234
}
228-
};
229-
}
230235

231-
////////////////////////////////////////////////////////////////////////////////
236+
// None of this crate's error handling needs the `From::from` error conversion
237+
// performed implicitly by the `?` operator or the standard library's `try!`
238+
// macro. This simplified macro gives a 5.5% improvement in compile time
239+
// compared to standard `try!`, and 9% improvement compared to `?`.
240+
#[cfg(not(no_serde_derive))]
241+
macro_rules! tri {
242+
($expr:expr) => {
243+
match $expr {
244+
Ok(val) => val,
245+
Err(err) => return Err(err),
246+
}
247+
};
248+
}
232249

233-
pub use serde_core::{
234-
de, forward_to_deserialize_any, ser, Deserialize, Deserializer, Serialize, Serializer,
235-
};
250+
////////////////////////////////////////////////////////////////////////////////
236251

237-
mod integer128;
252+
pub use serde_core::{
253+
de, forward_to_deserialize_any, ser, Deserialize, Deserializer, Serialize, Serializer,
254+
};
238255

239-
// Used by generated code and doc tests. Not public API.
240-
#[doc(hidden)]
241-
mod private;
256+
// Used by generated code and doc tests. Not public API.
257+
#[doc(hidden)]
258+
mod private;
259+
260+
include!(concat!(env!("OUT_DIR"), "/private.rs"));
261+
};
262+
}
263+
264+
crate_root!();
242265

243-
include!(concat!(env!("OUT_DIR"), "/private.rs"));
266+
mod integer128;
244267

245268
// Re-export #[derive(Serialize, Deserialize)].
246269
//

serde_core/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fn main() {
3434
println!("cargo:rustc-check-cfg=cfg(no_core_net)");
3535
println!("cargo:rustc-check-cfg=cfg(no_core_num_saturating)");
3636
println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)");
37+
println!("cargo:rustc-check-cfg=cfg(no_serde_core)");
3738
println!("cargo:rustc-check-cfg=cfg(no_serde_derive)");
3839
println!("cargo:rustc-check-cfg=cfg(no_std_atomic)");
3940
println!("cargo:rustc-check-cfg=cfg(no_std_atomic64)");

serde_core/src/crate_root.rs

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
macro_rules! crate_root {
2+
() => {
3+
/// A facade around all the types we need from the `std`, `core`, and `alloc`
4+
/// crates. This avoids elaborate import wrangling having to happen in every
5+
/// module.
6+
mod lib {
7+
mod core {
8+
#[cfg(not(feature = "std"))]
9+
pub use core::*;
10+
#[cfg(feature = "std")]
11+
pub use std::*;
12+
}
13+
14+
pub use self::core::{f32, f64};
15+
pub use self::core::{iter, num, str};
16+
17+
#[cfg(any(feature = "std", feature = "alloc"))]
18+
pub use self::core::{cmp, mem};
19+
20+
pub use self::core::cell::{Cell, RefCell};
21+
pub use self::core::cmp::Reverse;
22+
pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite};
23+
pub use self::core::marker::PhantomData;
24+
pub use self::core::num::Wrapping;
25+
pub use self::core::ops::{Bound, Range, RangeFrom, RangeInclusive, RangeTo};
26+
pub use self::core::result;
27+
pub use self::core::time::Duration;
28+
29+
#[cfg(all(feature = "alloc", not(feature = "std")))]
30+
pub use alloc::borrow::{Cow, ToOwned};
31+
#[cfg(feature = "std")]
32+
pub use std::borrow::{Cow, ToOwned};
33+
34+
#[cfg(all(feature = "alloc", not(feature = "std")))]
35+
pub use alloc::string::{String, ToString};
36+
#[cfg(feature = "std")]
37+
pub use std::string::{String, ToString};
38+
39+
#[cfg(all(feature = "alloc", not(feature = "std")))]
40+
pub use alloc::vec::Vec;
41+
#[cfg(feature = "std")]
42+
pub use std::vec::Vec;
43+
44+
#[cfg(all(feature = "alloc", not(feature = "std")))]
45+
pub use alloc::boxed::Box;
46+
#[cfg(feature = "std")]
47+
pub use std::boxed::Box;
48+
49+
#[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
50+
pub use alloc::rc::{Rc, Weak as RcWeak};
51+
#[cfg(all(feature = "rc", feature = "std"))]
52+
pub use std::rc::{Rc, Weak as RcWeak};
53+
54+
#[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
55+
pub use alloc::sync::{Arc, Weak as ArcWeak};
56+
#[cfg(all(feature = "rc", feature = "std"))]
57+
pub use std::sync::{Arc, Weak as ArcWeak};
58+
59+
#[cfg(all(feature = "alloc", not(feature = "std")))]
60+
pub use alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
61+
#[cfg(feature = "std")]
62+
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
63+
64+
#[cfg(all(not(no_core_cstr), not(feature = "std")))]
65+
pub use self::core::ffi::CStr;
66+
#[cfg(feature = "std")]
67+
pub use std::ffi::CStr;
68+
69+
#[cfg(all(not(no_core_cstr), feature = "alloc", not(feature = "std")))]
70+
pub use alloc::ffi::CString;
71+
#[cfg(feature = "std")]
72+
pub use std::ffi::CString;
73+
74+
#[cfg(all(not(no_core_net), not(feature = "std")))]
75+
pub use self::core::net;
76+
#[cfg(feature = "std")]
77+
pub use std::net;
78+
79+
#[cfg(feature = "std")]
80+
pub use std::error;
81+
82+
#[cfg(feature = "std")]
83+
pub use std::collections::{HashMap, HashSet};
84+
#[cfg(feature = "std")]
85+
pub use std::ffi::{OsStr, OsString};
86+
#[cfg(feature = "std")]
87+
pub use std::hash::{BuildHasher, Hash};
88+
#[cfg(feature = "std")]
89+
pub use std::io::Write;
90+
#[cfg(feature = "std")]
91+
pub use std::path::{Path, PathBuf};
92+
#[cfg(feature = "std")]
93+
pub use std::sync::{Mutex, RwLock};
94+
#[cfg(feature = "std")]
95+
pub use std::time::{SystemTime, UNIX_EPOCH};
96+
97+
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
98+
pub use std::sync::atomic::{
99+
AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32,
100+
AtomicU8, AtomicUsize, Ordering,
101+
};
102+
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))]
103+
pub use std::sync::atomic::{AtomicI64, AtomicU64};
104+
105+
#[cfg(all(feature = "std", not(no_target_has_atomic)))]
106+
pub use std::sync::atomic::Ordering;
107+
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "8"))]
108+
pub use std::sync::atomic::{AtomicBool, AtomicI8, AtomicU8};
109+
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "16"))]
110+
pub use std::sync::atomic::{AtomicI16, AtomicU16};
111+
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "32"))]
112+
pub use std::sync::atomic::{AtomicI32, AtomicU32};
113+
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "64"))]
114+
pub use std::sync::atomic::{AtomicI64, AtomicU64};
115+
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "ptr"))]
116+
pub use std::sync::atomic::{AtomicIsize, AtomicUsize};
117+
118+
#[cfg(not(no_core_num_saturating))]
119+
pub use self::core::num::Saturating;
120+
}
121+
122+
// None of this crate's error handling needs the `From::from` error conversion
123+
// performed implicitly by the `?` operator or the standard library's `try!`
124+
// macro. This simplified macro gives a 5.5% improvement in compile time
125+
// compared to standard `try!`, and 9% improvement compared to `?`.
126+
macro_rules! tri {
127+
($expr:expr) => {
128+
match $expr {
129+
Ok(val) => val,
130+
Err(err) => return Err(err),
131+
}
132+
};
133+
}
134+
135+
#[cfg_attr(no_serde_core, path = "core/de/mod.rs")]
136+
pub mod de;
137+
#[cfg_attr(no_serde_core, path = "core/ser/mod.rs")]
138+
pub mod ser;
139+
140+
#[cfg_attr(no_serde_core, path = "core/format.rs")]
141+
mod format;
142+
143+
#[doc(inline)]
144+
pub use crate::de::{Deserialize, Deserializer};
145+
#[doc(inline)]
146+
pub use crate::ser::{Serialize, Serializer};
147+
148+
// Used by generated code. Not public API.
149+
#[doc(hidden)]
150+
#[cfg_attr(no_serde_core, path = "core/private/mod.rs")]
151+
mod private;
152+
153+
// Used by declarative macro generated code. Not public API.
154+
#[doc(hidden)]
155+
pub mod __private {
156+
#[doc(hidden)]
157+
pub use crate::private::doc;
158+
#[doc(hidden)]
159+
pub use core::result::Result;
160+
}
161+
162+
include!(concat!(env!("OUT_DIR"), "/private.rs"));
163+
164+
#[cfg(all(not(feature = "std"), no_core_error))]
165+
#[cfg_attr(no_serde_core, path = "core/std_error.rs")]
166+
mod std_error;
167+
};
168+
}

0 commit comments

Comments
 (0)