2 pub(crate) mod item_type;
3 pub(crate) mod renderer;
5 use rustc_hir::def_id::DefId;
7 pub(crate) use renderer::{run_format, FormatRenderer};
9 use crate::clean::{self, ItemId};
10 use crate::html::render::Context;
12 /// Specifies whether rendering directly implemented trait items or ones from a certain Deref
14 pub(crate) enum AssocItemRender<'a> {
16 DerefFor { trait_: &'a clean::Path, type_: &'a clean::Type, deref_mut_: bool },
19 /// For different handling of associated items from the Deref target of a type rather than the type
21 #[derive(Copy, Clone, PartialEq)]
22 pub(crate) enum RenderMode {
24 ForDeref { mut_: bool },
27 /// Metadata about implementations for a type or trait.
28 #[derive(Clone, Debug)]
29 pub(crate) struct Impl {
30 pub(crate) impl_item: clean::Item,
34 pub(crate) fn inner_impl(&self) -> &clean::Impl {
35 match *self.impl_item.kind {
36 clean::ImplItem(ref impl_) => impl_,
37 _ => panic!("non-impl item found in impl"),
41 pub(crate) fn trait_did(&self) -> Option<DefId> {
42 self.inner_impl().trait_.as_ref().map(|t| t.def_id())
45 /// This function is used to extract a `DefId` to be used as a key for the `Cache::impls` field.
47 /// It allows to prevent having duplicated implementations showing up (the biggest issue was
48 /// with blanket impls).
50 /// It panics if `self` is a `ItemId::Primitive`.
51 pub(crate) fn def_id(&self) -> DefId {
52 match self.impl_item.item_id {
53 ItemId::Blanket { impl_id, .. } => impl_id,
54 ItemId::Auto { trait_, .. } => trait_,
55 ItemId::DefId(def_id) => def_id,
56 ItemId::Primitive(_, _) => {
58 "Unexpected ItemId::Primitive in expect_def_id: {:?}",
59 self.impl_item.item_id
65 // Returns true if this is an implementation on a "local" type, meaning:
66 // the type is in the current crate, or the type and the trait are both
67 // re-exported by the current crate.
68 pub(crate) fn is_on_local_type(&self, cx: &Context<'_>) -> bool {
69 let cache = cx.cache();
70 let for_type = &self.inner_impl().for_;
71 if let Some(for_type_did) = for_type.def_id(cache) {
72 // The "for" type is local if it's in the paths for the current crate.
73 if cache.paths.contains_key(&for_type_did) {
76 if let Some(trait_did) = self.trait_did() {
77 // The "for" type and the trait are from the same crate. That could
78 // be different from the current crate, for instance when both were
79 // re-exported from some other crate. But they are local with respect to
81 if for_type_did.krate == trait_did.krate {
84 // Hack: many traits and types in std are re-exported from
85 // core or alloc. In general, rustdoc is capable of recognizing
86 // these implementations as being on local types. However, in at
87 // least one case (https://github.com/rust-lang/rust/issues/97610),
88 // rustdoc gets confused and labels an implementation as being on
89 // a foreign type. To make sure that confusion doesn't pass on to
90 // the reader, consider all implementations in std, core, and alloc
91 // to be on local types.
92 let crate_name = cx.tcx().crate_name(trait_did.krate);
93 if matches!(crate_name.as_str(), "std" | "core" | "alloc") {