rustdoc_types/lib.rs
1//! Rustdoc's JSON output interface
2//!
3//! These types are the public API exposed through the `--output-format json` flag. The [`Crate`]
4//! struct is the root of the JSON blob and all other items are contained within.
5//!
6//! # Feature Flags
7//!
8//! ## `rustc-hash`
9//!
10//! We expose a `rustc-hash` feature, disabled by default. This feature switches the
11//! [`std::collections::HashMap`] for [`rustc_hash::FxHashMap`] to improve the performance of said
12//! `HashMap` in specific situations.
13//!
14//! `cargo-semver-checks` for example, saw a [-3% improvement][1] when benchmarking using the
15//! `aws_sdk_ec2` JSON output (~500MB of JSON). As always, we recommend measuring the impact before
16//! turning this feature on, as [`FxHashMap`][2] only concerns itself with hash speed, and may
17//! increase the number of collisions.
18//!
19//! ## `rkyv_0_8`
20//!
21//! We expose a `rkyv_0_8` feature, disabled by default. When enabled, it derives `rkyv`'s
22//! [`Archive`][3], [`Serialize`][4] and [`Deserialize`][5] traits for all types in this crate.
23//! Furthermore, it exposes the corresponding `Archived*` types (e.g. `ArchivedId` for [`Id`]).
24//!
25//! `rkyv` lets you works with JSON output without paying the deserialization cost _upfront_,
26//! thanks to [zero-copy deserialization][6].
27//! You can perform various types of analyses on the `Archived*` version of the relevant types,
28//! incurring the full deserialization cost only for the subset of items you actually need.
29//!
30//! [1]: https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/rustc-hash.20and.20performance.20of.20rustdoc-types/near/474855731
31//! [2]: https://crates.io/crates/rustc-hash
32//! [3]: https://docs.rs/rkyv/0.8.15/rkyv/trait.Archive.html
33//! [4]: https://docs.rs/rkyv/0.8.15/rkyv/trait.Serialize.html
34//! [5]: https://docs.rs/rkyv/0.8.15/rkyv/trait.Deserialize.html
35//! [6]: https://rkyv.org/zero-copy-deserialization.html
36
37// # On `rkyv` Derives
38//
39// In most cases, it's enough to add `#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]`
40// on top of a type to derive the relevant `rkyv` traits.
41//
42// There are a few exceptions, though, where more complex macro options are required.
43// The following sections break down the patterns that are showcased by `rkyv'`s
44// [JSON schema example](https://github.com/rkyv/rkyv/blob/985b0230a0b9cb9fce4a4ee9facb6af148e27c8e/rkyv/examples/json_like_schema.rs).
45//
46// ## Recursive Types
47//
48// Let's look at the `Type` enum as an example. It stores a `Box<Type>` in its `Slice` variant.
49// A "vanilla" `rkyv` annotation will cause an overflow in the compiler when
50// building the crate, since the bounds generated by the macro will be self-referential and thus
51// trap the compiler into a never-ending loop.
52//
53// To prevent this issue, `#[rkyv(omit_bounds)]` must be added to the relevant field.
54//
55// ## Co-Recursive Types
56//
57// The same problem occurs if a type is co-recursive—i.e. it doesn't _directly_ store a pointer
58// to another instance of the same type, but one of its fields does, transitively.
59//
60// For example, let's look at `Path`:
61//
62// - `Path` has a field of type `Option<Box<GenericArgs>>`
63// - One of the variants in `GenericArgs` has a field of type `Vec<GenericArg>`
64// - One of the variants of `GenericArg` has a field of type `Type`
65// - `Type::ResolvedPath` stores a `Path` instance
66//
67// The same logic of the recursive case applies here: we must use `#[rkyv(omit_bounds)]` to break the cycle.
68//
69// ## Additional Bounds
70//
71// Whenever `#[rkyv(omit_bounds)]` is added to a field or variant, `rkyv` omits _all_ traits bounds for that
72// field in the generated impl. This may result in compilation errors due to insufficient bounds in the
73// generated code.
74//
75// To add _some_ bounds back, `rkyv` exposes four knobs:
76//
77// - `#[rkyv(archive_bounds(..))]` to add predicates to all generated impls
78// - `#[rkyv(serialize_bounds(..))]` to add predicates to just the `Serialize` impl
79// - `#[rkyv(deserialize_bounds(..))]` to add predicates to just the `Deserialize` impl
80// - `#[rkyv(bytecheck(bounds(..)))]` to add predicates to just the `CheckBytes` impl
81//
82// In particular, we use the following annotations in this crate:
83//
84// - `serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, __S::Error: rkyv::rancor::Source)` for serializing
85// variable-length types like `Vec<T>`. `rkyv`'s zero-copy format requires the serializer to be able
86// to write bytes (`Writer`) and allocate scratch space (`Allocator`) for these types
87// ([`rkyv`'s `Vec` impl bounds](https://docs.rs/rkyv/0.8.15/rkyv/trait.Serialize.html#impl-Serialize%3CS%3E-for-Vec%3CT%3E)).
88// The `Error: Source` bound lets error types compose.
89// - `deserialize_bounds(__D::Error: rkyv::rancor::Source)` so that errors from deserializing fields behind
90// `omit_bounds` (e.g. `Box<T>`, `Vec<T>`) can compose via the `Source` trait.
91// - `bytecheck(bounds(__C: rkyv::validation::ArchiveContext, __C::Error: rkyv::rancor::Source))` for validating
92// archived data. Checking that bytes represent a valid archived value requires an `ArchiveContext` that tracks
93// validation state (e.g. subtree ranges, to prevent overlapping/out-of-bounds archived data).
94
95#[cfg(not(feature = "rustc-hash"))]
96use std::collections::HashMap;
97use std::path::PathBuf;
98
99#[cfg(feature = "rustc-hash")]
100use rustc_hash::FxHashMap as HashMap;
101use serde_derive::{Deserialize, Serialize};
102
103
104/// The version of JSON output that this crate represents.
105///
106/// This integer is incremented with every breaking change to the API,
107/// and is returned along with the JSON blob as [`Crate::format_version`].
108/// Consuming code should assert that this value matches the format version(s) that it supports.
109//
110// WARNING: When you update `FORMAT_VERSION`, please also update the "Latest feature" line with a
111// description of the change. This minimizes the risk of two concurrent PRs changing
112// `FORMAT_VERSION` from N to N+1 and git merging them without conflicts; the "Latest feature" line
113// will instead cause conflicts. See #94591 for more. (This paragraph and the "Latest feature" line
114// are deliberately not in a doc comment, because they need not be in public docs.)
115//
116// Latest feature: Add `ExternCrate::path`.
117pub const FORMAT_VERSION: u32 = 57;
118
119/// The root of the emitted JSON blob.
120///
121/// It contains all type/documentation information
122/// about the language items in the local crate, as well as info about external items to allow
123/// tools to find or link to them.
124#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
125#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
126#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
127pub struct Crate {
128 /// The id of the root [`Module`] item of the local crate.
129 pub root: Id,
130 /// The version string given to `--crate-version`, if any.
131 pub crate_version: Option<String>,
132 /// Whether or not the output includes private items.
133 pub includes_private: bool,
134 /// A collection of all items in the local crate as well as some external traits and their
135 /// items that are referenced locally.
136 pub index: HashMap<Id, Item>,
137 /// Maps IDs to fully qualified paths and other info helpful for generating links.
138 pub paths: HashMap<Id, ItemSummary>,
139 /// Maps `crate_id` of items to a crate name and html_root_url if it exists.
140 pub external_crates: HashMap<u32, ExternalCrate>,
141 /// Information about the target for which this documentation was generated
142 pub target: Target,
143 /// A single version number to be used in the future when making backwards incompatible changes
144 /// to the JSON output.
145 pub format_version: u32,
146}
147
148/// Information about a target
149#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
150#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
151#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
152pub struct Target {
153 /// The target triple for which this documentation was generated
154 pub triple: String,
155 /// A list of features valid for use in `#[target_feature]` attributes
156 /// for the target where this rustdoc JSON was generated.
157 pub target_features: Vec<TargetFeature>,
158}
159
160/// Information about a target feature.
161///
162/// Rust target features are used to influence code generation, especially around selecting
163/// instructions which are not universally supported by the target architecture.
164///
165/// Target features are commonly enabled by the [`#[target_feature]` attribute][1] to influence code
166/// generation for a particular function, and less commonly enabled by compiler options like
167/// `-Ctarget-feature` or `-Ctarget-cpu`. Targets themselves automatically enable certain target
168/// features by default, for example because the target's ABI specification requires saving specific
169/// registers which only exist in an architectural extension.
170///
171/// Target features can imply other target features: for example, x86-64 `avx2` implies `avx`, and
172/// aarch64 `sve2` implies `sve`, since both of these architectural extensions depend on their
173/// predecessors.
174///
175/// Target features can be probed at compile time by [`#[cfg(target_feature)]`][2] or `cfg!(…)`
176/// conditional compilation to determine whether a target feature is enabled in a particular
177/// context.
178///
179/// [1]: https://doc.rust-lang.org/stable/reference/attributes/codegen.html#the-target_feature-attribute
180/// [2]: https://doc.rust-lang.org/reference/conditional-compilation.html#target_feature
181#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
182#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
183#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
184pub struct TargetFeature {
185 /// The name of this target feature.
186 pub name: String,
187 /// Other target features which are implied by this target feature, if any.
188 pub implies_features: Vec<String>,
189 /// If this target feature is unstable, the name of the associated language feature gate.
190 pub unstable_feature_gate: Option<String>,
191 /// Whether this feature is globally enabled for this compilation session.
192 ///
193 /// Target features can be globally enabled implicitly as a result of the target's definition.
194 /// For example, x86-64 hardware floating point ABIs require saving x87 and SSE2 registers,
195 /// which in turn requires globally enabling the `x87` and `sse2` target features so that the
196 /// generated machine code conforms to the target's ABI.
197 ///
198 /// Target features can also be globally enabled explicitly as a result of compiler flags like
199 /// [`-Ctarget-feature`][1] or [`-Ctarget-cpu`][2].
200 ///
201 /// [1]: https://doc.rust-lang.org/beta/rustc/codegen-options/index.html#target-feature
202 /// [2]: https://doc.rust-lang.org/beta/rustc/codegen-options/index.html#target-cpu
203 pub globally_enabled: bool,
204}
205
206/// Metadata of a crate, either the same crate on which `rustdoc` was invoked, or its dependency.
207#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
208#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
209#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
210pub struct ExternalCrate {
211 /// The name of the crate.
212 ///
213 /// Note: This is the [*crate* name][crate-name], which may not be the same as the
214 /// [*package* name][package-name]. For example, for <https://crates.io/crates/regex-syntax>,
215 /// this field will be `regex_syntax` (which uses an `_`, not a `-`).
216 ///
217 /// [crate-name]: https://doc.rust-lang.org/stable/cargo/reference/cargo-targets.html#the-name-field
218 /// [package-name]: https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-name-field
219 pub name: String,
220 /// The root URL at which the crate's documentation lives.
221 pub html_root_url: Option<String>,
222
223 /// A path from where this crate was loaded.
224 ///
225 /// This will typically be a `.rlib` or `.rmeta`. It can be used to determine which crate
226 /// this was in terms of whatever build-system invoked rustc.
227 #[cfg_attr(feature = "rkyv_0_8", rkyv(with = rkyv::with::AsString))]
228 pub path: PathBuf,
229}
230
231/// Information about an external (not defined in the local crate) [`Item`].
232///
233/// For external items, you don't get the same level of
234/// information. This struct should contain enough to generate a link/reference to the item in
235/// question, or can be used by a tool that takes the json output of multiple crates to find
236/// the actual item definition with all the relevant info.
237#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
238#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
239#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
240pub struct ItemSummary {
241 /// Can be used to look up the name and html_root_url of the crate this item came from in the
242 /// `external_crates` map.
243 pub crate_id: u32,
244 /// The list of path components for the fully qualified path of this item (e.g.
245 /// `["std", "io", "lazy", "Lazy"]` for `std::io::lazy::Lazy`).
246 ///
247 /// Note that items can appear in multiple paths, and the one chosen is implementation
248 /// defined. Currently, this is the full path to where the item was defined. Eg
249 /// [`String`] is currently `["alloc", "string", "String"]` and [`HashMap`][`std::collections::HashMap`]
250 /// is `["std", "collections", "hash", "map", "HashMap"]`, but this is subject to change.
251 pub path: Vec<String>,
252 /// Whether this item is a struct, trait, macro, etc.
253 pub kind: ItemKind,
254}
255
256/// Anything that can hold documentation - modules, structs, enums, functions, traits, etc.
257///
258/// The `Item` data type holds fields that can apply to any of these,
259/// and leaves kind-specific details (like function args or enum variants) to the `inner` field.
260#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
261#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
262#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
263pub struct Item {
264 /// The unique identifier of this item. Can be used to find this item in various mappings.
265 pub id: Id,
266 /// This can be used as a key to the `external_crates` map of [`Crate`] to see which crate
267 /// this item came from.
268 pub crate_id: u32,
269 /// Some items such as impls don't have names.
270 pub name: Option<String>,
271 /// The source location of this item (absent if it came from a macro expansion or inline
272 /// assembly).
273 pub span: Option<Span>,
274 /// By default all documented items are public, but you can tell rustdoc to output private items
275 /// so this field is needed to differentiate.
276 pub visibility: Visibility,
277 /// The full markdown docstring of this item. Absent if there is no documentation at all,
278 /// Some("") if there is some documentation but it is empty (EG `#[doc = ""]`).
279 pub docs: Option<String>,
280 /// This mapping resolves [intra-doc links](https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md) from the docstring to their IDs
281 pub links: HashMap<String, Id>,
282 /// Attributes on this item.
283 ///
284 /// Does not include `#[deprecated]` attributes: see the [`Self::deprecation`] field instead.
285 ///
286 /// Attributes appear in pretty-printed Rust form, regardless of their formatting
287 /// in the original source code. For example:
288 /// - `#[non_exhaustive]` and `#[must_use]` are represented as themselves.
289 /// - `#[no_mangle]` and `#[export_name]` are also represented as themselves.
290 /// - `#[repr(C)]` and other reprs also appear as themselves,
291 /// though potentially with a different order: e.g. `repr(i8, C)` may become `repr(C, i8)`.
292 /// Multiple repr attributes on the same item may be combined into an equivalent single attr.
293 pub attrs: Vec<Attribute>,
294 /// Information about the item’s deprecation, if present.
295 pub deprecation: Option<Deprecation>,
296 /// The type-specific fields describing this item.
297 pub inner: ItemEnum,
298}
299
300#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
301#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
302#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
303#[serde(rename_all = "snake_case")]
304/// An attribute, e.g. `#[repr(C)]`
305///
306/// This doesn't include:
307/// - `#[doc = "Doc Comment"]` or `/// Doc comment`. These are in [`Item::docs`] instead.
308/// - `#[deprecated]`. These are in [`Item::deprecation`] instead.
309pub enum Attribute {
310 /// `#[non_exhaustive]`
311 NonExhaustive,
312
313 /// `#[must_use]`
314 MustUse { reason: Option<String> },
315
316 /// `#[macro_export]`
317 MacroExport,
318
319 /// `#[export_name = "name"]`
320 ExportName(String),
321
322 /// `#[link_section = "name"]`
323 LinkSection(String),
324
325 /// `#[automatically_derived]`
326 AutomaticallyDerived,
327
328 /// `#[repr]`
329 Repr(AttributeRepr),
330
331 /// `#[no_mangle]`
332 NoMangle,
333
334 /// #[target_feature(enable = "feature1", enable = "feature2")]
335 TargetFeature { enable: Vec<String> },
336
337 /// Something else.
338 ///
339 /// Things here are explicitly *not* covered by the [`FORMAT_VERSION`]
340 /// constant, and may change without bumping the format version.
341 ///
342 /// As an implementation detail, this is currently either:
343 /// 1. A HIR debug printing, like `"#[attr = Optimize(Speed)]"`
344 /// 2. The attribute as it appears in source form, like
345 /// `"#[optimize(speed)]"`.
346 Other(String),
347}
348
349#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
350#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
351#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
352/// The contents of a `#[repr(...)]` attribute.
353///
354/// Used in [`Attribute::Repr`].
355pub struct AttributeRepr {
356 /// The representation, e.g. `#[repr(C)]`, `#[repr(transparent)]`
357 pub kind: ReprKind,
358
359 /// Alignment in bytes, if explicitly specified by `#[repr(align(...)]`.
360 pub align: Option<u64>,
361 /// Alignment in bytes, if explicitly specified by `#[repr(packed(...)]]`.
362 pub packed: Option<u64>,
363
364 /// The integer type for an enum descriminant, if explicitly specified.
365 ///
366 /// e.g. `"i32"`, for `#[repr(C, i32)]`
367 pub int: Option<String>,
368}
369
370#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
371#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
372#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
373#[serde(rename_all = "snake_case")]
374/// The kind of `#[repr]`.
375///
376/// See [AttributeRepr::kind]`.
377pub enum ReprKind {
378 /// `#[repr(Rust)]`
379 ///
380 /// Also the default.
381 Rust,
382 /// `#[repr(C)]`
383 C,
384 /// `#[repr(transparent)]
385 Transparent,
386 /// `#[repr(simd)]`
387 Simd,
388}
389
390/// A range of source code.
391#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
392#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
393#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
394pub struct Span {
395 /// The path to the source file for this span relative to the path `rustdoc` was invoked with.
396 #[cfg_attr(feature = "rkyv_0_8", rkyv(with = rkyv::with::AsString))]
397 pub filename: PathBuf,
398 /// One indexed Line and Column of the first character of the `Span`.
399 pub begin: (usize, usize),
400 /// One indexed Line and Column of the last character of the `Span`.
401 pub end: (usize, usize),
402}
403
404/// Information about the deprecation of an [`Item`].
405#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
406#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
407#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
408pub struct Deprecation {
409 /// Usually a version number when this [`Item`] first became deprecated.
410 pub since: Option<String>,
411 /// The reason for deprecation and/or what alternatives to use.
412 pub note: Option<String>,
413}
414
415/// Visibility of an [`Item`].
416#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
417#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
418#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
419#[serde(rename_all = "snake_case")]
420pub enum Visibility {
421 /// Explicitly public visibility set with `pub`.
422 Public,
423 /// For the most part items are private by default. The exceptions are associated items of
424 /// public traits and variants of public enums.
425 Default,
426 /// Explicitly crate-wide visibility set with `pub(crate)`
427 Crate,
428 /// For `pub(in path)` visibility.
429 Restricted {
430 /// ID of the module to which this visibility restricts items.
431 parent: Id,
432 /// The path with which [`parent`] was referenced
433 /// (like `super::super` or `crate::foo::bar`).
434 ///
435 /// [`parent`]: Visibility::Restricted::parent
436 path: String,
437 },
438}
439
440/// Dynamic trait object type (`dyn Trait`).
441#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
442#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
443#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
444pub struct DynTrait {
445 /// All the traits implemented. One of them is the vtable, and the rest must be auto traits.
446 pub traits: Vec<PolyTrait>,
447 /// The lifetime of the whole dyn object
448 /// ```text
449 /// dyn Debug + 'static
450 /// ^^^^^^^
451 /// |
452 /// this part
453 /// ```
454 pub lifetime: Option<String>,
455}
456
457/// A trait and potential HRTBs
458#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
459#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
460#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
461pub struct PolyTrait {
462 /// The path to the trait.
463 #[serde(rename = "trait")]
464 pub trait_: Path,
465 /// Used for Higher-Rank Trait Bounds (HRTBs)
466 /// ```text
467 /// dyn for<'a> Fn() -> &'a i32"
468 /// ^^^^^^^
469 /// ```
470 pub generic_params: Vec<GenericParamDef>,
471}
472
473/// A set of generic arguments provided to a path segment, e.g.
474///
475/// ```text
476/// std::option::Option<u32>
477/// ^^^^^
478/// ```
479#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
480#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
481#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
482#[cfg_attr(feature = "rkyv_0_8", rkyv(serialize_bounds(
483 __S: rkyv::ser::Writer + rkyv::ser::Allocator,
484 __S::Error: rkyv::rancor::Source,
485)))]
486#[cfg_attr(feature = "rkyv_0_8", rkyv(deserialize_bounds(
487 __D::Error: rkyv::rancor::Source,
488)))]
489#[cfg_attr(feature = "rkyv_0_8", rkyv(bytecheck(bounds(
490 __C: rkyv::validation::ArchiveContext,
491))))]
492#[serde(rename_all = "snake_case")]
493pub enum GenericArgs {
494 /// `<'a, 32, B: Copy, C = u32>`
495 AngleBracketed {
496 /// The list of each argument on this type.
497 /// ```text
498 /// <'a, 32, B: Copy, C = u32>
499 /// ^^^^^^
500 /// ```
501 args: Vec<GenericArg>,
502 /// Associated type or constant bindings (e.g. `Item=i32` or `Item: Clone`) for this type.
503 constraints: Vec<AssocItemConstraint>,
504 },
505 /// `Fn(A, B) -> C`
506 Parenthesized {
507 /// The input types, enclosed in parentheses.
508 #[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))]
509 inputs: Vec<Type>,
510 /// The output type provided after the `->`, if present.
511 #[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))]
512 output: Option<Type>,
513 },
514 /// `T::method(..)`
515 ReturnTypeNotation,
516}
517
518/// One argument in a list of generic arguments to a path segment.
519///
520/// Part of [`GenericArgs`].
521#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
522#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
523#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
524#[serde(rename_all = "snake_case")]
525pub enum GenericArg {
526 /// A lifetime argument.
527 /// ```text
528 /// std::borrow::Cow<'static, str>
529 /// ^^^^^^^
530 /// ```
531 Lifetime(String),
532 /// A type argument.
533 /// ```text
534 /// std::borrow::Cow<'static, str>
535 /// ^^^
536 /// ```
537 Type(Type),
538 /// A constant as a generic argument.
539 /// ```text
540 /// core::array::IntoIter<u32, { 640 * 1024 }>
541 /// ^^^^^^^^^^^^^^
542 /// ```
543 Const(Constant),
544 /// A generic argument that's explicitly set to be inferred.
545 /// ```text
546 /// std::vec::Vec::<_>
547 /// ^
548 /// ```
549 Infer,
550}
551
552/// A constant.
553#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
554#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
555#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
556pub struct Constant {
557 /// The stringified expression of this constant. Note that its mapping to the original
558 /// source code is unstable and it's not guaranteed that it'll match the source code.
559 pub expr: String,
560 /// The value of the evaluated expression for this constant, which is only computed for numeric
561 /// types.
562 pub value: Option<String>,
563 /// Whether this constant is a bool, numeric, string, or char literal.
564 pub is_literal: bool,
565}
566
567/// Describes a bound applied to an associated type/constant.
568///
569/// Example:
570/// ```text
571/// IntoIterator<Item = u32, IntoIter: Clone>
572/// ^^^^^^^^^^ ^^^^^^^^^^^^^^^
573/// ```
574#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
575#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
576#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
577#[cfg_attr(feature = "rkyv_0_8", rkyv(serialize_bounds(
578 __S: rkyv::ser::Writer + rkyv::ser::Allocator,
579 __S::Error: rkyv::rancor::Source,
580)))]
581#[cfg_attr(feature = "rkyv_0_8", rkyv(deserialize_bounds(
582 __D::Error: rkyv::rancor::Source,
583)))]
584#[cfg_attr(feature = "rkyv_0_8", rkyv(bytecheck(bounds(
585 __C: rkyv::validation::ArchiveContext,
586 <__C as rkyv::rancor::Fallible>::Error: rkyv::rancor::Source,
587))))]
588pub struct AssocItemConstraint {
589 /// The name of the associated type/constant.
590 pub name: String,
591 /// Arguments provided to the associated type/constant.
592 #[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))]
593 pub args: Option<Box<GenericArgs>>,
594 /// The kind of bound applied to the associated type/constant.
595 pub binding: AssocItemConstraintKind,
596}
597
598/// The way in which an associate type/constant is bound.
599#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
600#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
601#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
602#[serde(rename_all = "snake_case")]
603pub enum AssocItemConstraintKind {
604 /// The required value/type is specified exactly. e.g.
605 /// ```text
606 /// Iterator<Item = u32, IntoIter: DoubleEndedIterator>
607 /// ^^^^^^^^^^
608 /// ```
609 Equality(Term),
610 /// The type is required to satisfy a set of bounds.
611 /// ```text
612 /// Iterator<Item = u32, IntoIter: DoubleEndedIterator>
613 /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
614 /// ```
615 Constraint(Vec<GenericBound>),
616}
617
618/// An opaque identifier for an item.
619///
620/// It can be used to lookup in [`Crate::index`] or [`Crate::paths`] to resolve it
621/// to an [`Item`].
622///
623/// Id's are only valid within a single JSON blob. They cannot be used to
624/// resolve references between the JSON output's for different crates.
625///
626/// Rustdoc makes no guarantees about the inner value of Id's. Applications
627/// should treat them as opaque keys to lookup items, and avoid attempting
628/// to parse them, or otherwise depend on any implementation details.
629#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
630#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
631#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)))]
632// FIXME(aDotInTheVoid): Consider making this non-public in rustdoc-types.
633pub struct Id(pub u32);
634
635/// The fundamental kind of an item. Unlike [`ItemEnum`], this does not carry any additional info.
636///
637/// Part of [`ItemSummary`].
638#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
639#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
640#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
641#[cfg_attr(feature = "rkyv_0_8", rkyv(compare(PartialEq)))]
642#[serde(rename_all = "snake_case")]
643pub enum ItemKind {
644 /// A module declaration, e.g. `mod foo;` or `mod foo {}`
645 Module,
646 /// A crate imported via the `extern crate` syntax.
647 ExternCrate,
648 /// An import of 1 or more items into scope, using the `use` keyword.
649 Use,
650 /// A `struct` declaration.
651 Struct,
652 /// A field of a struct.
653 StructField,
654 /// A `union` declaration.
655 Union,
656 /// An `enum` declaration.
657 Enum,
658 /// A variant of a enum.
659 Variant,
660 /// A function declaration, e.g. `fn f() {}`
661 Function,
662 /// A type alias declaration, e.g. `type Pig = std::borrow::Cow<'static, str>;`
663 TypeAlias,
664 /// The declaration of a constant, e.g. `const GREETING: &str = "Hi :3";`
665 Constant,
666 /// A `trait` declaration.
667 Trait,
668 /// A trait alias declaration, e.g. `trait Int = Add + Sub + Mul + Div;`
669 ///
670 /// See [the tracking issue](https://github.com/rust-lang/rust/issues/41517)
671 TraitAlias,
672 /// An `impl` block.
673 Impl,
674 /// A `static` declaration.
675 Static,
676 /// `type`s from an `extern` block.
677 ///
678 /// See [the tracking issue](https://github.com/rust-lang/rust/issues/43467)
679 ExternType,
680 /// A macro declaration.
681 ///
682 /// Corresponds to either `ItemEnum::Macro(_)`
683 /// or `ItemEnum::ProcMacro(ProcMacro { kind: MacroKind::Bang })`
684 Macro,
685 /// A procedural macro attribute.
686 ///
687 /// Corresponds to `ItemEnum::ProcMacro(ProcMacro { kind: MacroKind::Attr })`
688 ProcAttribute,
689 /// A procedural macro usable in the `#[derive()]` attribute.
690 ///
691 /// Corresponds to `ItemEnum::ProcMacro(ProcMacro { kind: MacroKind::Derive })`
692 ProcDerive,
693 /// An associated constant of a trait or a type.
694 AssocConst,
695 /// An associated type of a trait or a type.
696 AssocType,
697 /// A primitive type, e.g. `u32`.
698 ///
699 /// [`Item`]s of this kind only come from the core library.
700 Primitive,
701 /// A keyword declaration.
702 ///
703 /// [`Item`]s of this kind only come from the come library and exist solely
704 /// to carry documentation for the respective keywords.
705 Keyword,
706 /// An attribute declaration.
707 ///
708 /// [`Item`]s of this kind only come from the core library and exist solely
709 /// to carry documentation for the respective builtin attributes.
710 Attribute,
711}
712
713/// Specific fields of an item.
714///
715/// Part of [`Item`].
716#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
717#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
718#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
719#[serde(rename_all = "snake_case")]
720pub enum ItemEnum {
721 /// A module declaration, e.g. `mod foo;` or `mod foo {}`
722 Module(Module),
723 /// A crate imported via the `extern crate` syntax.
724 ExternCrate {
725 /// The name of the imported crate.
726 name: String,
727 /// If the crate is renamed, this is its name in the crate.
728 rename: Option<String>,
729 },
730 /// An import of 1 or more items into scope, using the `use` keyword.
731 Use(Use),
732
733 /// A `union` declaration.
734 Union(Union),
735 /// A `struct` declaration.
736 Struct(Struct),
737 /// A field of a struct.
738 StructField(Type),
739 /// An `enum` declaration.
740 Enum(Enum),
741 /// A variant of a enum.
742 Variant(Variant),
743
744 /// A function declaration (including methods and other associated functions)
745 Function(Function),
746
747 /// A `trait` declaration.
748 Trait(Trait),
749 /// A trait alias declaration, e.g. `trait Int = Add + Sub + Mul + Div;`
750 ///
751 /// See [the tracking issue](https://github.com/rust-lang/rust/issues/41517)
752 TraitAlias(TraitAlias),
753 /// An `impl` block.
754 Impl(Impl),
755
756 /// A type alias declaration, e.g. `type Pig = std::borrow::Cow<'static, str>;`
757 TypeAlias(TypeAlias),
758 /// The declaration of a constant, e.g. `const GREETING: &str = "Hi :3";`
759 Constant {
760 /// The type of the constant.
761 #[serde(rename = "type")]
762 type_: Type,
763 /// The declared constant itself.
764 #[serde(rename = "const")]
765 const_: Constant,
766 },
767
768 /// A declaration of a `static`.
769 Static(Static),
770
771 /// `type`s from an `extern` block.
772 ///
773 /// See [the tracking issue](https://github.com/rust-lang/rust/issues/43467)
774 ExternType,
775
776 /// A macro_rules! declarative macro. Contains a single string with the source
777 /// representation of the macro with the patterns stripped.
778 Macro(String),
779 /// A procedural macro.
780 ProcMacro(ProcMacro),
781
782 /// A primitive type, e.g. `u32`.
783 ///
784 /// [`Item`]s of this kind only come from the core library.
785 Primitive(Primitive),
786
787 /// An associated constant of a trait or a type.
788 AssocConst {
789 /// The type of the constant.
790 #[serde(rename = "type")]
791 type_: Type,
792 /// Inside a trait declaration, this is the default value for the associated constant,
793 /// if provided.
794 /// Inside an `impl` block, this is the value assigned to the associated constant,
795 /// and will always be present.
796 ///
797 /// The representation is implementation-defined and not guaranteed to be representative of
798 /// either the resulting value or of the source code.
799 ///
800 /// ```rust
801 /// const X: usize = 640 * 1024;
802 /// // ^^^^^^^^^^
803 /// ```
804 value: Option<String>,
805 },
806 /// An associated type of a trait or a type.
807 AssocType {
808 /// The generic parameters and where clauses on ahis associated type.
809 generics: Generics,
810 /// The bounds for this associated type. e.g.
811 /// ```rust
812 /// trait IntoIterator {
813 /// type Item;
814 /// type IntoIter: Iterator<Item = Self::Item>;
815 /// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^
816 /// }
817 /// ```
818 bounds: Vec<GenericBound>,
819 /// Inside a trait declaration, this is the default for the associated type, if provided.
820 /// Inside an impl block, this is the type assigned to the associated type, and will always
821 /// be present.
822 ///
823 /// ```rust
824 /// type X = usize;
825 /// // ^^^^^
826 /// ```
827 #[serde(rename = "type")]
828 type_: Option<Type>,
829 },
830}
831
832impl ItemEnum {
833 /// Get just the kind of this item, but with no further data.
834 ///
835 /// ```rust
836 /// # use rustdoc_types::{ItemKind, ItemEnum};
837 /// let item = ItemEnum::ExternCrate { name: "libc".to_owned(), rename: None };
838 /// assert_eq!(item.item_kind(), ItemKind::ExternCrate);
839 /// ```
840 pub fn item_kind(&self) -> ItemKind {
841 match self {
842 ItemEnum::Module(_) => ItemKind::Module,
843 ItemEnum::ExternCrate { .. } => ItemKind::ExternCrate,
844 ItemEnum::Use(_) => ItemKind::Use,
845 ItemEnum::Union(_) => ItemKind::Union,
846 ItemEnum::Struct(_) => ItemKind::Struct,
847 ItemEnum::StructField(_) => ItemKind::StructField,
848 ItemEnum::Enum(_) => ItemKind::Enum,
849 ItemEnum::Variant(_) => ItemKind::Variant,
850 ItemEnum::Function(_) => ItemKind::Function,
851 ItemEnum::Trait(_) => ItemKind::Trait,
852 ItemEnum::TraitAlias(_) => ItemKind::TraitAlias,
853 ItemEnum::Impl(_) => ItemKind::Impl,
854 ItemEnum::TypeAlias(_) => ItemKind::TypeAlias,
855 ItemEnum::Constant { .. } => ItemKind::Constant,
856 ItemEnum::Static(_) => ItemKind::Static,
857 ItemEnum::ExternType => ItemKind::ExternType,
858 ItemEnum::Macro(_) => ItemKind::Macro,
859 ItemEnum::ProcMacro(pm) => match pm.kind {
860 MacroKind::Bang => ItemKind::Macro,
861 MacroKind::Attr => ItemKind::ProcAttribute,
862 MacroKind::Derive => ItemKind::ProcDerive,
863 },
864 ItemEnum::Primitive(_) => ItemKind::Primitive,
865 ItemEnum::AssocConst { .. } => ItemKind::AssocConst,
866 ItemEnum::AssocType { .. } => ItemKind::AssocType,
867 }
868 }
869}
870
871/// A module declaration, e.g. `mod foo;` or `mod foo {}`.
872#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
873#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
874#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
875pub struct Module {
876 /// Whether this is the root item of a crate.
877 ///
878 /// This item doesn't correspond to any construction in the source code and is generated by the
879 /// compiler.
880 pub is_crate: bool,
881 /// [`Item`]s declared inside this module.
882 pub items: Vec<Id>,
883 /// If `true`, this module is not part of the public API, but it contains
884 /// items that are re-exported as public API.
885 pub is_stripped: bool,
886}
887
888/// A `union`.
889#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
890#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
891#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
892pub struct Union {
893 /// The generic parameters and where clauses on this union.
894 pub generics: Generics,
895 /// Whether any fields have been removed from the result, due to being private or hidden.
896 pub has_stripped_fields: bool,
897 /// The list of fields in the union.
898 ///
899 /// All of the corresponding [`Item`]s are of kind [`ItemEnum::StructField`].
900 pub fields: Vec<Id>,
901 /// All impls (both of traits and inherent) for this union.
902 ///
903 /// All of the corresponding [`Item`]s are of kind [`ItemEnum::Impl`].
904 pub impls: Vec<Id>,
905}
906
907/// A `struct`.
908#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
909#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
910#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
911pub struct Struct {
912 /// The kind of the struct (e.g. unit, tuple-like or struct-like) and the data specific to it,
913 /// i.e. fields.
914 pub kind: StructKind,
915 /// The generic parameters and where clauses on this struct.
916 pub generics: Generics,
917 /// All impls (both of traits and inherent) for this struct.
918 /// All of the corresponding [`Item`]s are of kind [`ItemEnum::Impl`].
919 pub impls: Vec<Id>,
920}
921
922/// The kind of a [`Struct`] and the data specific to it, i.e. fields.
923#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
924#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
925#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
926#[serde(rename_all = "snake_case")]
927pub enum StructKind {
928 /// A struct with no fields and no parentheses.
929 ///
930 /// ```rust
931 /// pub struct Unit;
932 /// ```
933 Unit,
934 /// A struct with unnamed fields.
935 ///
936 /// All [`Id`]'s will point to [`ItemEnum::StructField`].
937 /// Unlike most of JSON, private and `#[doc(hidden)]` fields will be given as `None`
938 /// instead of being omitted, because order matters.
939 ///
940 /// ```rust
941 /// pub struct TupleStruct(i32);
942 /// pub struct EmptyTupleStruct();
943 /// ```
944 Tuple(Vec<Option<Id>>),
945 /// A struct with named fields.
946 ///
947 /// ```rust
948 /// pub struct PlainStruct { x: i32 }
949 /// pub struct EmptyPlainStruct {}
950 /// ```
951 Plain {
952 /// The list of fields in the struct.
953 ///
954 /// All of the corresponding [`Item`]s are of kind [`ItemEnum::StructField`].
955 fields: Vec<Id>,
956 /// Whether any fields have been removed from the result, due to being private or hidden.
957 has_stripped_fields: bool,
958 },
959}
960
961/// An `enum`.
962#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
963#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
964#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
965pub struct Enum {
966 /// Information about the type parameters and `where` clauses of the enum.
967 pub generics: Generics,
968 /// Whether any variants have been removed from the result, due to being private or hidden.
969 pub has_stripped_variants: bool,
970 /// The list of variants in the enum.
971 ///
972 /// All of the corresponding [`Item`]s are of kind [`ItemEnum::Variant`]
973 pub variants: Vec<Id>,
974 /// `impl`s for the enum.
975 pub impls: Vec<Id>,
976}
977
978/// A variant of an enum.
979#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
980#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
981#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
982pub struct Variant {
983 /// Whether the variant is plain, a tuple-like, or struct-like. Contains the fields.
984 pub kind: VariantKind,
985 /// The discriminant, if explicitly specified.
986 pub discriminant: Option<Discriminant>,
987}
988
989/// The kind of an [`Enum`] [`Variant`] and the data specific to it, i.e. fields.
990#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
991#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
992#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
993#[serde(rename_all = "snake_case")]
994pub enum VariantKind {
995 /// A variant with no parentheses
996 ///
997 /// ```rust
998 /// enum Demo {
999 /// PlainVariant,
1000 /// PlainWithDiscriminant = 1,
1001 /// }
1002 /// ```
1003 Plain,
1004 /// A variant with unnamed fields.
1005 ///
1006 /// All [`Id`]'s will point to [`ItemEnum::StructField`].
1007 /// Unlike most of JSON, `#[doc(hidden)]` fields will be given as `None`
1008 /// instead of being omitted, because order matters.
1009 ///
1010 /// ```rust
1011 /// enum Demo {
1012 /// TupleVariant(i32),
1013 /// EmptyTupleVariant(),
1014 /// }
1015 /// ```
1016 Tuple(Vec<Option<Id>>),
1017 /// A variant with named fields.
1018 ///
1019 /// ```rust
1020 /// enum Demo {
1021 /// StructVariant { x: i32 },
1022 /// EmptyStructVariant {},
1023 /// }
1024 /// ```
1025 Struct {
1026 /// The list of named fields in the variant.
1027 /// All of the corresponding [`Item`]s are of kind [`ItemEnum::StructField`].
1028 fields: Vec<Id>,
1029 /// Whether any fields have been removed from the result, due to being private or hidden.
1030 has_stripped_fields: bool,
1031 },
1032}
1033
1034/// The value that distinguishes a variant in an [`Enum`] from other variants.
1035#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1036#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1037#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1038pub struct Discriminant {
1039 /// The expression that produced the discriminant.
1040 ///
1041 /// Unlike `value`, this preserves the original formatting (eg suffixes,
1042 /// hexadecimal, and underscores), making it unsuitable to be machine
1043 /// interpreted.
1044 ///
1045 /// In some cases, when the value is too complex, this may be `"{ _ }"`.
1046 /// When this occurs is unstable, and may change without notice.
1047 pub expr: String,
1048 /// The numerical value of the discriminant. Stored as a string due to
1049 /// JSON's poor support for large integers, and the fact that it would need
1050 /// to store from [`i128::MIN`] to [`u128::MAX`].
1051 pub value: String,
1052}
1053
1054/// A set of fundamental properties of a function.
1055#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1056#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1057#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1058pub struct FunctionHeader {
1059 /// Is this function marked as `const`?
1060 pub is_const: bool,
1061 /// Is this function unsafe?
1062 pub is_unsafe: bool,
1063 /// Is this function async?
1064 pub is_async: bool,
1065 /// The ABI used by the function.
1066 pub abi: Abi,
1067}
1068
1069/// The ABI (Application Binary Interface) used by a function.
1070///
1071/// If a variant has an `unwind` field, this means the ABI that it represents can be specified in 2
1072/// ways: `extern "_"` and `extern "_-unwind"`, and a value of `true` for that field signifies the
1073/// latter variant.
1074///
1075/// See the [Rustonomicon section](https://doc.rust-lang.org/nightly/nomicon/ffi.html#ffi-and-unwinding)
1076/// on unwinding for more info.
1077#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1078#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1079#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1080pub enum Abi {
1081 // We only have a concrete listing here for stable ABI's because there are so many
1082 // See rustc_ast_passes::feature_gate::PostExpansionVisitor::check_abi for the list
1083 /// The default ABI, but that can also be written explicitly with `extern "Rust"`.
1084 Rust,
1085 /// Can be specified as `extern "C"` or, as a shorthand, just `extern`.
1086 C { unwind: bool },
1087 /// Can be specified as `extern "cdecl"`.
1088 Cdecl { unwind: bool },
1089 /// Can be specified as `extern "stdcall"`.
1090 Stdcall { unwind: bool },
1091 /// Can be specified as `extern "fastcall"`.
1092 Fastcall { unwind: bool },
1093 /// Can be specified as `extern "aapcs"`.
1094 Aapcs { unwind: bool },
1095 /// Can be specified as `extern "win64"`.
1096 Win64 { unwind: bool },
1097 /// Can be specified as `extern "sysv64"`.
1098 SysV64 { unwind: bool },
1099 /// Can be specified as `extern "system"`.
1100 System { unwind: bool },
1101 /// Any other ABI, including unstable ones.
1102 Other(String),
1103}
1104
1105/// A function declaration (including methods and other associated functions).
1106#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1107#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1108#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1109pub struct Function {
1110 /// Information about the function signature, or declaration.
1111 pub sig: FunctionSignature,
1112 /// Information about the function’s type parameters and `where` clauses.
1113 pub generics: Generics,
1114 /// Information about core properties of the function, e.g. whether it's `const`, its ABI, etc.
1115 pub header: FunctionHeader,
1116 /// Whether the function has a body, i.e. an implementation.
1117 pub has_body: bool,
1118}
1119
1120/// Generic parameters accepted by an item and `where` clauses imposed on it and the parameters.
1121#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1122#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1123#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1124pub struct Generics {
1125 /// A list of generic parameter definitions (e.g. `<T: Clone + Hash, U: Copy>`).
1126 pub params: Vec<GenericParamDef>,
1127 /// A list of where predicates (e.g. `where T: Iterator, T::Item: Copy`).
1128 pub where_predicates: Vec<WherePredicate>,
1129}
1130
1131/// One generic parameter accepted by an item.
1132#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1133#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1134#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1135pub struct GenericParamDef {
1136 /// Name of the parameter.
1137 /// ```rust
1138 /// fn f<'resource, Resource>(x: &'resource Resource) {}
1139 /// // ^^^^^^^^ ^^^^^^^^
1140 /// ```
1141 pub name: String,
1142 /// The kind of the parameter and data specific to a particular parameter kind, e.g. type
1143 /// bounds.
1144 pub kind: GenericParamDefKind,
1145}
1146
1147/// The kind of a [`GenericParamDef`].
1148#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1149#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1150#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1151#[cfg_attr(feature = "rkyv_0_8", rkyv(serialize_bounds(
1152 __S: rkyv::ser::Writer + rkyv::ser::Allocator,
1153 __S::Error: rkyv::rancor::Source,
1154)))]
1155#[cfg_attr(feature = "rkyv_0_8", rkyv(deserialize_bounds(
1156 __D::Error: rkyv::rancor::Source,
1157)))]
1158#[cfg_attr(feature = "rkyv_0_8", rkyv(bytecheck(bounds(
1159 __C: rkyv::validation::ArchiveContext,
1160))))]
1161#[serde(rename_all = "snake_case")]
1162pub enum GenericParamDefKind {
1163 /// Denotes a lifetime parameter.
1164 Lifetime {
1165 /// Lifetimes that this lifetime parameter is required to outlive.
1166 ///
1167 /// ```rust
1168 /// fn f<'a, 'b, 'resource: 'a + 'b>(a: &'a str, b: &'b str, res: &'resource str) {}
1169 /// // ^^^^^^^
1170 /// ```
1171 outlives: Vec<String>,
1172 },
1173
1174 /// Denotes a type parameter.
1175 Type {
1176 /// Bounds applied directly to the type. Note that the bounds from `where` clauses
1177 /// that constrain this parameter won't appear here.
1178 ///
1179 /// ```rust
1180 /// fn default2<T: Default>() -> [T; 2] where T: Clone { todo!() }
1181 /// // ^^^^^^^
1182 /// ```
1183 #[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))]
1184 bounds: Vec<GenericBound>,
1185 /// The default type for this parameter, if provided, e.g.
1186 ///
1187 /// ```rust
1188 /// trait PartialEq<Rhs = Self> {}
1189 /// // ^^^^
1190 /// ```
1191 #[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))]
1192 default: Option<Type>,
1193 /// This is normally `false`, which means that this generic parameter is
1194 /// declared in the Rust source text.
1195 ///
1196 /// If it is `true`, this generic parameter has been introduced by the
1197 /// compiler behind the scenes.
1198 ///
1199 /// # Example
1200 ///
1201 /// Consider
1202 ///
1203 /// ```ignore (pseudo-rust)
1204 /// pub fn f(_: impl Trait) {}
1205 /// ```
1206 ///
1207 /// The compiler will transform this behind the scenes to
1208 ///
1209 /// ```ignore (pseudo-rust)
1210 /// pub fn f<impl Trait: Trait>(_: impl Trait) {}
1211 /// ```
1212 ///
1213 /// In this example, the generic parameter named `impl Trait` (and which
1214 /// is bound by `Trait`) is synthetic, because it was not originally in
1215 /// the Rust source text.
1216 is_synthetic: bool,
1217 },
1218
1219 /// Denotes a constant parameter.
1220 Const {
1221 /// The type of the constant as declared.
1222 #[serde(rename = "type")]
1223 #[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))]
1224 type_: Type,
1225 /// The stringified expression for the default value, if provided. It's not guaranteed that
1226 /// it'll match the actual source code for the default value.
1227 default: Option<String>,
1228 },
1229}
1230
1231/// One `where` clause.
1232/// ```rust
1233/// fn default<T>() -> T where T: Default { T::default() }
1234/// // ^^^^^^^^^^
1235/// ```
1236#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1237#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1238#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1239#[serde(rename_all = "snake_case")]
1240pub enum WherePredicate {
1241 /// A type is expected to comply with a set of bounds
1242 BoundPredicate {
1243 /// The type that's being constrained.
1244 ///
1245 /// ```rust
1246 /// fn f<T>(x: T) where for<'a> &'a T: Iterator {}
1247 /// // ^
1248 /// ```
1249 #[serde(rename = "type")]
1250 type_: Type,
1251 /// The set of bounds that constrain the type.
1252 ///
1253 /// ```rust
1254 /// fn f<T>(x: T) where for<'a> &'a T: Iterator {}
1255 /// // ^^^^^^^^
1256 /// ```
1257 bounds: Vec<GenericBound>,
1258 /// Used for Higher-Rank Trait Bounds (HRTBs)
1259 /// ```rust
1260 /// fn f<T>(x: T) where for<'a> &'a T: Iterator {}
1261 /// // ^^^^^^^
1262 /// ```
1263 generic_params: Vec<GenericParamDef>,
1264 },
1265
1266 /// A lifetime is expected to outlive other lifetimes.
1267 LifetimePredicate {
1268 /// The name of the lifetime.
1269 lifetime: String,
1270 /// The lifetimes that must be encompassed by the lifetime.
1271 outlives: Vec<String>,
1272 },
1273
1274 /// A type must exactly equal another type.
1275 EqPredicate {
1276 /// The left side of the equation.
1277 lhs: Type,
1278 /// The right side of the equation.
1279 rhs: Term,
1280 },
1281}
1282
1283/// Either a trait bound or a lifetime bound.
1284#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1285#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1286#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1287#[serde(rename_all = "snake_case")]
1288pub enum GenericBound {
1289 /// A trait bound.
1290 TraitBound {
1291 /// The full path to the trait.
1292 #[serde(rename = "trait")]
1293 trait_: Path,
1294 /// Used for Higher-Rank Trait Bounds (HRTBs)
1295 /// ```text
1296 /// where F: for<'a, 'b> Fn(&'a u8, &'b u8)
1297 /// ^^^^^^^^^^^
1298 /// |
1299 /// this part
1300 /// ```
1301 generic_params: Vec<GenericParamDef>,
1302 /// The context for which a trait is supposed to be used, e.g. `const
1303 modifier: TraitBoundModifier,
1304 },
1305 /// A lifetime bound, e.g.
1306 /// ```rust
1307 /// fn f<'a, T>(x: &'a str, y: &T) where T: 'a {}
1308 /// // ^^^
1309 /// ```
1310 Outlives(String),
1311 /// `use<'a, T>` precise-capturing bound syntax
1312 Use(Vec<PreciseCapturingArg>),
1313}
1314
1315/// A set of modifiers applied to a trait.
1316#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1317#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1318#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1319#[serde(rename_all = "snake_case")]
1320pub enum TraitBoundModifier {
1321 /// Marks the absence of a modifier.
1322 None,
1323 /// Indicates that the trait bound relaxes a trait bound applied to a parameter by default,
1324 /// e.g. `T: Sized?`, the `Sized` trait is required for all generic type parameters by default
1325 /// unless specified otherwise with this modifier.
1326 Maybe,
1327 /// Indicates that the trait bound must be applicable in both a run-time and a compile-time
1328 /// context.
1329 MaybeConst,
1330}
1331
1332/// One precise capturing argument. See [the rust reference](https://doc.rust-lang.org/reference/types/impl-trait.html#precise-capturing).
1333#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1334#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1335#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1336#[serde(rename_all = "snake_case")]
1337pub enum PreciseCapturingArg {
1338 /// A lifetime.
1339 /// ```rust
1340 /// pub fn hello<'a, T, const N: usize>() -> impl Sized + use<'a, T, N> {}
1341 /// // ^^
1342 Lifetime(String),
1343 /// A type or constant parameter.
1344 /// ```rust
1345 /// pub fn hello<'a, T, const N: usize>() -> impl Sized + use<'a, T, N> {}
1346 /// // ^ ^
1347 Param(String),
1348}
1349
1350/// Either a type or a constant, usually stored as the right-hand side of an equation in places like
1351/// [`AssocItemConstraint`]
1352#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1353#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1354#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1355#[serde(rename_all = "snake_case")]
1356pub enum Term {
1357 /// A type.
1358 ///
1359 /// ```rust
1360 /// fn f(x: impl IntoIterator<Item = u32>) {}
1361 /// // ^^^
1362 /// ```
1363 Type(Type),
1364 /// A constant.
1365 ///
1366 /// ```ignore (incomplete feature in the snippet)
1367 /// trait Foo {
1368 /// const BAR: usize;
1369 /// }
1370 ///
1371 /// fn f(x: impl Foo<BAR = 42>) {}
1372 /// // ^^
1373 /// ```
1374 Constant(Constant),
1375}
1376
1377/// A type.
1378#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1379#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1380#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1381#[cfg_attr(feature = "rkyv_0_8", rkyv(serialize_bounds(
1382 __S: rkyv::ser::Writer + rkyv::ser::Allocator,
1383 __S::Error: rkyv::rancor::Source,
1384)))]
1385#[cfg_attr(feature = "rkyv_0_8", rkyv(deserialize_bounds(
1386 __D::Error: rkyv::rancor::Source,
1387)))]
1388#[cfg_attr(feature = "rkyv_0_8", rkyv(bytecheck(bounds(
1389 __C: rkyv::validation::ArchiveContext,
1390))))]
1391#[serde(rename_all = "snake_case")]
1392pub enum Type {
1393 /// Structs, enums, unions and type aliases, e.g. `std::option::Option<u32>`
1394 ResolvedPath(Path),
1395 /// Dynamic trait object type (`dyn Trait`).
1396 DynTrait(DynTrait),
1397 /// Parameterized types. The contained string is the name of the parameter.
1398 Generic(String),
1399 /// Built-in numeric types (e.g. `u32`, `f32`), `bool`, `char`.
1400 Primitive(String),
1401 /// A function pointer type, e.g. `fn(u32) -> u32`, `extern "C" fn() -> *const u8`
1402 FunctionPointer(#[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))] Box<FunctionPointer>),
1403 /// A tuple type, e.g. `(String, u32, Box<usize>)`
1404 Tuple(#[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))] Vec<Type>),
1405 /// An unsized slice type, e.g. `[u32]`.
1406 Slice(#[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))] Box<Type>),
1407 /// An array type, e.g. `[u32; 15]`
1408 Array {
1409 /// The type of the contained element.
1410 #[serde(rename = "type")]
1411 #[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))]
1412 type_: Box<Type>,
1413 /// The stringified expression that is the length of the array.
1414 ///
1415 /// Keep in mind that it's not guaranteed to match the actual source code of the expression.
1416 len: String,
1417 },
1418 /// A pattern type, e.g. `u32 is 1..`
1419 ///
1420 /// See [the tracking issue](https://github.com/rust-lang/rust/issues/123646)
1421 Pat {
1422 /// The base type, e.g. the `u32` in `u32 is 1..`
1423 #[serde(rename = "type")]
1424 #[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))]
1425 type_: Box<Type>,
1426 #[doc(hidden)]
1427 __pat_unstable_do_not_use: String,
1428 },
1429 /// An opaque type that satisfies a set of bounds, `impl TraitA + TraitB + ...`
1430 ImplTrait(Vec<GenericBound>),
1431 /// A type that's left to be inferred, `_`
1432 Infer,
1433 /// A raw pointer type, e.g. `*mut u32`, `*const u8`, etc.
1434 RawPointer {
1435 /// This is `true` for `*mut _` and `false` for `*const _`.
1436 is_mutable: bool,
1437 /// The type of the pointee.
1438 #[serde(rename = "type")]
1439 #[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))]
1440 type_: Box<Type>,
1441 },
1442 /// `&'a mut String`, `&str`, etc.
1443 BorrowedRef {
1444 /// The name of the lifetime of the reference, if provided.
1445 lifetime: Option<String>,
1446 /// This is `true` for `&mut i32` and `false` for `&i32`
1447 is_mutable: bool,
1448 /// The type of the pointee, e.g. the `i32` in `&'a mut i32`
1449 #[serde(rename = "type")]
1450 #[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))]
1451 type_: Box<Type>,
1452 },
1453 /// Associated types like `<Type as Trait>::Name` and `T::Item` where
1454 /// `T: Iterator` or inherent associated types like `Struct::Name`.
1455 QualifiedPath {
1456 /// The name of the associated type in the parent type.
1457 ///
1458 /// ```ignore (incomplete expression)
1459 /// <core::array::IntoIter<u32, 42> as Iterator>::Item
1460 /// // ^^^^
1461 /// ```
1462 name: String,
1463 /// The generic arguments provided to the associated type.
1464 ///
1465 /// ```ignore (incomplete expression)
1466 /// <core::slice::IterMut<'static, u32> as BetterIterator>::Item<'static>
1467 /// // ^^^^^^^^^
1468 /// ```
1469 #[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))]
1470 args: Option<Box<GenericArgs>>,
1471 /// The type with which this type is associated.
1472 ///
1473 /// ```ignore (incomplete expression)
1474 /// <core::array::IntoIter<u32, 42> as Iterator>::Item
1475 /// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1476 /// ```
1477 #[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))]
1478 self_type: Box<Type>,
1479 /// `None` iff this is an *inherent* associated type.
1480 #[serde(rename = "trait")]
1481 trait_: Option<Path>,
1482 },
1483}
1484
1485/// A type that has a simple path to it. This is the kind of type of structs, unions, enums, etc.
1486#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1487#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1488#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1489#[cfg_attr(feature = "rkyv_0_8", rkyv(serialize_bounds(
1490 __S: rkyv::ser::Writer + rkyv::ser::Allocator,
1491 __S::Error: rkyv::rancor::Source,
1492)))]
1493#[cfg_attr(feature = "rkyv_0_8", rkyv(deserialize_bounds(
1494 __D::Error: rkyv::rancor::Source,
1495)))]
1496#[cfg_attr(feature = "rkyv_0_8", rkyv(bytecheck(bounds(
1497 __C: rkyv::validation::ArchiveContext,
1498 <__C as rkyv::rancor::Fallible>::Error: rkyv::rancor::Source,
1499))))]
1500pub struct Path {
1501 /// The path of the type.
1502 ///
1503 /// This will be the path that is *used* (not where it is defined), so
1504 /// multiple `Path`s may have different values for this field even if
1505 /// they all refer to the same item. e.g.
1506 ///
1507 /// ```rust
1508 /// pub type Vec1 = std::vec::Vec<i32>; // path: "std::vec::Vec"
1509 /// pub type Vec2 = Vec<i32>; // path: "Vec"
1510 /// pub type Vec3 = std::prelude::v1::Vec<i32>; // path: "std::prelude::v1::Vec"
1511 /// ```
1512 //
1513 // Example tested in ./tests/rustdoc-json/path_name.rs
1514 pub path: String,
1515 /// The ID of the type.
1516 pub id: Id,
1517 /// Generic arguments to the type.
1518 ///
1519 /// ```ignore (incomplete expression)
1520 /// std::borrow::Cow<'static, str>
1521 /// // ^^^^^^^^^^^^^^
1522 /// ```
1523 #[cfg_attr(feature = "rkyv_0_8", rkyv(omit_bounds))]
1524 pub args: Option<Box<GenericArgs>>,
1525}
1526
1527/// A type that is a function pointer.
1528#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1529#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1530#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1531pub struct FunctionPointer {
1532 /// The signature of the function.
1533 pub sig: FunctionSignature,
1534 /// Used for Higher-Rank Trait Bounds (HRTBs)
1535 ///
1536 /// ```ignore (incomplete expression)
1537 /// for<'c> fn(val: &'c i32) -> i32
1538 /// // ^^^^^^^
1539 /// ```
1540 pub generic_params: Vec<GenericParamDef>,
1541 /// The core properties of the function, such as the ABI it conforms to, whether it's unsafe, etc.
1542 pub header: FunctionHeader,
1543}
1544
1545/// The signature of a function.
1546#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1547#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1548#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1549pub struct FunctionSignature {
1550 /// List of argument names and their type.
1551 ///
1552 /// Note that not all names will be valid identifiers, as some of
1553 /// them may be patterns.
1554 pub inputs: Vec<(String, Type)>,
1555 /// The output type, if specified.
1556 pub output: Option<Type>,
1557 /// Whether the function accepts an arbitrary amount of trailing arguments the C way.
1558 ///
1559 /// ```ignore (incomplete code)
1560 /// fn printf(fmt: &str, ...);
1561 /// ```
1562 pub is_c_variadic: bool,
1563}
1564
1565/// A `trait` declaration.
1566#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1567#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1568#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1569pub struct Trait {
1570 /// Whether the trait is marked `auto` and is thus implemented automatically
1571 /// for all applicable types.
1572 pub is_auto: bool,
1573 /// Whether the trait is marked as `unsafe`.
1574 pub is_unsafe: bool,
1575 /// Whether the trait is [dyn compatible](https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility)[^1].
1576 ///
1577 /// [^1]: Formerly known as "object safe".
1578 pub is_dyn_compatible: bool,
1579 /// Associated [`Item`]s that can/must be implemented by the `impl` blocks.
1580 pub items: Vec<Id>,
1581 /// Information about the type parameters and `where` clauses of the trait.
1582 pub generics: Generics,
1583 /// Constraints that must be met by the implementor of the trait.
1584 pub bounds: Vec<GenericBound>,
1585 /// The implementations of the trait.
1586 pub implementations: Vec<Id>,
1587}
1588
1589/// A trait alias declaration, e.g. `trait Int = Add + Sub + Mul + Div;`
1590///
1591/// See [the tracking issue](https://github.com/rust-lang/rust/issues/41517)
1592#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1593#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1594#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1595pub struct TraitAlias {
1596 /// Information about the type parameters and `where` clauses of the alias.
1597 pub generics: Generics,
1598 /// The bounds that are associated with the alias.
1599 pub params: Vec<GenericBound>,
1600}
1601
1602/// An `impl` block.
1603#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1604#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1605#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1606pub struct Impl {
1607 /// Whether this impl is for an unsafe trait.
1608 pub is_unsafe: bool,
1609 /// Information about the impl’s type parameters and `where` clauses.
1610 pub generics: Generics,
1611 /// The list of the names of all the trait methods that weren't mentioned in this impl but
1612 /// were provided by the trait itself.
1613 ///
1614 /// For example, for this impl of the [`PartialEq`] trait:
1615 /// ```rust
1616 /// struct Foo;
1617 ///
1618 /// impl PartialEq for Foo {
1619 /// fn eq(&self, other: &Self) -> bool { todo!() }
1620 /// }
1621 /// ```
1622 /// This field will be `["ne"]`, as it has a default implementation defined for it.
1623 pub provided_trait_methods: Vec<String>,
1624 /// The trait being implemented or `None` if the impl is inherent, which means
1625 /// `impl Struct {}` as opposed to `impl Trait for Struct {}`.
1626 #[serde(rename = "trait")]
1627 pub trait_: Option<Path>,
1628 /// The type that the impl block is for.
1629 #[serde(rename = "for")]
1630 pub for_: Type,
1631 /// The list of associated items contained in this impl block.
1632 pub items: Vec<Id>,
1633 /// Whether this is a negative impl (e.g. `!Sized` or `!Send`).
1634 pub is_negative: bool,
1635 /// Whether this is an impl that’s implied by the compiler
1636 /// (for autotraits, e.g. `Send` or `Sync`).
1637 pub is_synthetic: bool,
1638 // FIXME: document this
1639 pub blanket_impl: Option<Type>,
1640}
1641
1642/// A `use` statement.
1643#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1644#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1645#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1646#[serde(rename_all = "snake_case")]
1647pub struct Use {
1648 /// The full path being imported.
1649 pub source: String,
1650 /// May be different from the last segment of `source` when renaming imports:
1651 /// `use source as name;`
1652 pub name: String,
1653 /// The ID of the item being imported. Will be `None` in case of re-exports of primitives:
1654 /// ```rust
1655 /// pub use i32 as my_i32;
1656 /// ```
1657 pub id: Option<Id>,
1658 /// Whether this statement is a wildcard `use`, e.g. `use source::*;`
1659 pub is_glob: bool,
1660}
1661
1662/// A procedural macro.
1663#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1664#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1665#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1666pub struct ProcMacro {
1667 /// How this macro is supposed to be called: `foo!()`, `#[foo]` or `#[derive(foo)]`
1668 pub kind: MacroKind,
1669 /// Helper attributes defined by a macro to be used inside it.
1670 ///
1671 /// Defined only for derive macros.
1672 ///
1673 /// E.g. the [`Default`] derive macro defines a `#[default]` helper attribute so that one can
1674 /// do:
1675 ///
1676 /// ```rust
1677 /// #[derive(Default)]
1678 /// enum Option<T> {
1679 /// #[default]
1680 /// None,
1681 /// Some(T),
1682 /// }
1683 /// ```
1684 pub helpers: Vec<String>,
1685}
1686
1687/// The way a [`ProcMacro`] is declared to be used.
1688#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1689#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1690#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1691#[serde(rename_all = "snake_case")]
1692pub enum MacroKind {
1693 /// A bang macro `foo!()`.
1694 Bang,
1695 /// An attribute macro `#[foo]`.
1696 Attr,
1697 /// A derive macro `#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]`
1698 Derive,
1699}
1700
1701/// A type alias declaration, e.g. `type Pig = std::borrow::Cow<'static, str>;`
1702#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1703#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1704#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1705pub struct TypeAlias {
1706 /// The type referred to by this alias.
1707 #[serde(rename = "type")]
1708 pub type_: Type,
1709 /// Information about the type parameters and `where` clauses of the alias.
1710 pub generics: Generics,
1711}
1712
1713/// A `static` declaration.
1714#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1715#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1716#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1717pub struct Static {
1718 /// The type of the static.
1719 #[serde(rename = "type")]
1720 pub type_: Type,
1721 /// This is `true` for mutable statics, declared as `static mut X: T = f();`
1722 pub is_mutable: bool,
1723 /// The stringified expression for the initial value.
1724 ///
1725 /// It's not guaranteed that it'll match the actual source code for the initial value.
1726 pub expr: String,
1727
1728 /// Is the static `unsafe`?
1729 ///
1730 /// This is only true if it's in an `extern` block, and not explicitly marked
1731 /// as `safe`.
1732 ///
1733 /// ```rust
1734 /// unsafe extern {
1735 /// static A: i32; // unsafe
1736 /// safe static B: i32; // safe
1737 /// }
1738 ///
1739 /// static C: i32 = 0; // safe
1740 /// static mut D: i32 = 0; // safe
1741 /// ```
1742 pub is_unsafe: bool,
1743}
1744
1745/// A primitive type declaration. Declarations of this kind can only come from the core library.
1746#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1747#[cfg_attr(feature = "rkyv_0_8", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
1748#[cfg_attr(feature = "rkyv_0_8", rkyv(derive(Debug)))]
1749pub struct Primitive {
1750 /// The name of the type.
1751 pub name: String,
1752 /// The implementations, inherent and of traits, on the primitive type.
1753 pub impls: Vec<Id>,
1754}
1755
1756#[cfg(test)]
1757mod tests;