>();
// Output the trait definition
wrap_into_docblock(w, |w| {
write!(w, "");
render_attributes(w, it, true);
write!(
w,
"{}{}{}trait {}{}{}",
it.visibility.print_with_space(),
t.unsafety.print_with_space(),
if t.is_auto { "auto " } else { "" },
it.name.as_ref().unwrap(),
t.generics.print(),
bounds
);
if !t.generics.where_predicates.is_empty() {
write!(w, "{}", WhereClause { gens: &t.generics, indent: 0, end_newline: true });
} else {
write!(w, " ");
}
if t.items.is_empty() {
write!(w, "{{ }}");
} else {
// FIXME: we should be using a derived_id for the Anchors here
write!(w, "{{\n");
for t in &types {
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait);
write!(w, ";\n");
}
if !types.is_empty() && !consts.is_empty() {
w.write_str("\n");
}
for t in &consts {
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait);
write!(w, ";\n");
}
if !consts.is_empty() && !required.is_empty() {
w.write_str("\n");
}
for (pos, m) in required.iter().enumerate() {
render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait);
write!(w, ";\n");
if pos < required.len() - 1 {
write!(w, "");
}
}
if !required.is_empty() && !provided.is_empty() {
w.write_str("\n");
}
for (pos, m) in provided.iter().enumerate() {
render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait);
match m.inner {
clean::MethodItem(ref inner) if !inner.generics.where_predicates.is_empty() => {
write!(w, ",\n {{ ... }}\n");
}
_ => {
write!(w, " {{ ... }}\n");
}
}
if pos < provided.len() - 1 {
write!(w, "");
}
}
write!(w, "}}");
}
write!(w, "
")
});
// Trait documentation
document(w, cx, it);
fn write_small_section_header(w: &mut Buffer, id: &str, title: &str, extra_content: &str) {
write!(
w,
"
{2}",
id, title, extra_content
)
}
fn write_loading_content(w: &mut Buffer, extra_content: &str) {
write!(w, "{}Loading content...", extra_content)
}
fn trait_item(w: &mut Buffer, cx: &Context, m: &clean::Item, t: &clean::Item) {
let name = m.name.as_ref().unwrap();
let item_type = m.type_();
let id = cx.derive_id(format!("{}.{}", item_type, name));
write!(w, "", id = id);
render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl);
write!(w, "
");
render_stability_since(w, m, t);
write!(w, "
");
document(w, cx, m);
}
if !types.is_empty() {
write_small_section_header(
w,
"associated-types",
"Associated Types",
"",
);
for t in &types {
trait_item(w, cx, *t, it);
}
write_loading_content(w, "
");
}
if !consts.is_empty() {
write_small_section_header(
w,
"associated-const",
"Associated Constants",
"",
);
for t in &consts {
trait_item(w, cx, *t, it);
}
write_loading_content(w, "
");
}
// Output the documentation for each function individually
if !required.is_empty() {
write_small_section_header(
w,
"required-methods",
"Required methods",
"",
);
for m in &required {
trait_item(w, cx, *m, it);
}
write_loading_content(w, "
");
}
if !provided.is_empty() {
write_small_section_header(
w,
"provided-methods",
"Provided methods",
"",
);
for m in &provided {
trait_item(w, cx, *m, it);
}
write_loading_content(w, "
");
}
// If there are methods directly on this trait object, render them here.
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All);
if let Some(implementors) = cx.cache.implementors.get(&it.def_id) {
// The DefId is for the first Type found with that name. The bool is
// if any Types with the same name but different DefId have been found.
let mut implementor_dups: FxHashMap<&str, (DefId, bool)> = FxHashMap::default();
for implementor in implementors {
match implementor.inner_impl().for_ {
clean::ResolvedPath { ref path, did, is_generic: false, .. }
| clean::BorrowedRef {
type_: box clean::ResolvedPath { ref path, did, is_generic: false, .. },
..
} => {
let &mut (prev_did, ref mut has_duplicates) =
implementor_dups.entry(path.last_name()).or_insert((did, false));
if prev_did != did {
*has_duplicates = true;
}
}
_ => {}
}
}
let (local, foreign) = implementors.iter().partition::, _>(|i| {
i.inner_impl().for_.def_id().map_or(true, |d| cx.cache.paths.contains_key(&d))
});
let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) =
local.iter().partition(|i| i.inner_impl().synthetic);
synthetic.sort_by(compare_impl);
concrete.sort_by(compare_impl);
if !foreign.is_empty() {
write_small_section_header(w, "foreign-impls", "Implementations on Foreign Types", "");
for implementor in foreign {
let assoc_link = AssocItemLink::GotoSource(
implementor.impl_item.def_id,
&implementor.inner_impl().provided_trait_methods,
);
render_impl(
w,
cx,
&implementor,
assoc_link,
RenderMode::Normal,
implementor.impl_item.stable_since(),
false,
None,
true,
false,
&[],
);
}
write_loading_content(w, "");
}
write_small_section_header(
w,
"implementors",
"Implementors",
"",
);
for implementor in concrete {
render_implementor(cx, implementor, w, &implementor_dups, &[]);
}
write_loading_content(w, "
");
if t.auto {
write_small_section_header(
w,
"synthetic-implementors",
"Auto implementors",
"",
);
for implementor in synthetic {
render_implementor(
cx,
implementor,
w,
&implementor_dups,
&collect_paths_for_type(implementor.inner_impl().for_.clone()),
);
}
write_loading_content(w, "
");
}
} else {
// even without any implementations to write in, we still want the heading and list, so the
// implementors javascript file pulled in below has somewhere to write the impls into
write_small_section_header(
w,
"implementors",
"Implementors",
"",
);
write_loading_content(w, "
");
if t.auto {
write_small_section_header(
w,
"synthetic-implementors",
"Auto implementors",
"",
);
write_loading_content(w, "
");
}
}
write!(
w,
"",
root_path = vec![".."; cx.current.len()].join("/"),
path = if it.def_id.is_local() {
cx.current.join("/")
} else {
let (ref path, _) = cx.cache.external_paths[&it.def_id];
path[..path.len() - 1].join("/")
},
ty = it.type_(),
name = *it.name.as_ref().unwrap()
);
}
fn naive_assoc_href(it: &clean::Item, link: AssocItemLink<'_>) -> String {
use crate::html::item_type::ItemType::*;
let name = it.name.as_ref().unwrap();
let ty = match it.type_() {
Typedef | AssocType => AssocType,
s => s,
};
let anchor = format!("#{}.{}", ty, name);
match link {
AssocItemLink::Anchor(Some(ref id)) => format!("#{}", id),
AssocItemLink::Anchor(None) => anchor,
AssocItemLink::GotoSource(did, _) => {
href(did).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor)
}
}
}
fn assoc_const(
w: &mut Buffer,
it: &clean::Item,
ty: &clean::Type,
_default: Option<&String>,
link: AssocItemLink<'_>,
extra: &str,
) {
write!(
w,
"{}{}const {}: {}",
extra,
it.visibility.print_with_space(),
naive_assoc_href(it, link),
it.name.as_ref().unwrap(),
ty.print()
);
}
fn assoc_type(
w: &mut Buffer,
it: &clean::Item,
bounds: &[clean::GenericBound],
default: Option<&clean::Type>,
link: AssocItemLink<'_>,
extra: &str,
) {
write!(
w,
"{}type {}",
extra,
naive_assoc_href(it, link),
it.name.as_ref().unwrap()
);
if !bounds.is_empty() {
write!(w, ": {}", print_generic_bounds(bounds))
}
if let Some(default) = default {
write!(w, " = {}", default.print())
}
}
fn render_stability_since_raw(w: &mut Buffer, ver: Option<&str>, containing_ver: Option<&str>) {
if let Some(v) = ver {
if containing_ver != ver && !v.is_empty() {
write!(w, "{0}", v)
}
}
}
fn render_stability_since(w: &mut Buffer, item: &clean::Item, containing_item: &clean::Item) {
render_stability_since_raw(w, item.stable_since(), containing_item.stable_since())
}
fn render_assoc_item(
w: &mut Buffer,
item: &clean::Item,
link: AssocItemLink<'_>,
parent: ItemType,
) {
fn method(
w: &mut Buffer,
meth: &clean::Item,
header: hir::FnHeader,
g: &clean::Generics,
d: &clean::FnDecl,
link: AssocItemLink<'_>,
parent: ItemType,
) {
let name = meth.name.as_ref().unwrap();
let anchor = format!("#{}.{}", meth.type_(), name);
let href = match link {
AssocItemLink::Anchor(Some(ref id)) => format!("#{}", id),
AssocItemLink::Anchor(None) => anchor,
AssocItemLink::GotoSource(did, provided_methods) => {
// We're creating a link from an impl-item to the corresponding
// trait-item and need to map the anchored type accordingly.
let ty = if provided_methods.contains(name) {
ItemType::Method
} else {
ItemType::TyMethod
};
href(did).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor)
}
};
let mut header_len = format!(
"{}{}{}{}{}{:#}fn {}{:#}",
meth.visibility.print_with_space(),
header.constness.print_with_space(),
header.asyncness.print_with_space(),
header.unsafety.print_with_space(),
print_default_space(meth.is_default()),
print_abi_with_space(header.abi),
name,
g.print()
)
.len();
let (indent, end_newline) = if parent == ItemType::Trait {
header_len += 4;
(4, false)
} else {
(0, true)
};
render_attributes(w, meth, false);
write!(
w,
"{}{}{}{}{}{}{}fn {name}\
{generics}{decl}{where_clause}",
if parent == ItemType::Trait { " " } else { "" },
meth.visibility.print_with_space(),
header.constness.print_with_space(),
header.asyncness.print_with_space(),
header.unsafety.print_with_space(),
print_default_space(meth.is_default()),
print_abi_with_space(header.abi),
href = href,
name = name,
generics = g.print(),
decl = Function { decl: d, header_len, indent, asyncness: header.asyncness }.print(),
where_clause = WhereClause { gens: g, indent, end_newline }
)
}
match item.inner {
clean::StrippedItem(..) => {}
clean::TyMethodItem(ref m) => method(w, item, m.header, &m.generics, &m.decl, link, parent),
clean::MethodItem(ref m) => method(w, item, m.header, &m.generics, &m.decl, link, parent),
clean::AssocConstItem(ref ty, ref default) => assoc_const(
w,
item,
ty,
default.as_ref(),
link,
if parent == ItemType::Trait { " " } else { "" },
),
clean::AssocTypeItem(ref bounds, ref default) => assoc_type(
w,
item,
bounds,
default.as_ref(),
link,
if parent == ItemType::Trait { " " } else { "" },
),
_ => panic!("render_assoc_item called on non-associated-item"),
}
}
fn item_struct(w: &mut Buffer, cx: &Context, it: &clean::Item, s: &clean::Struct) {
wrap_into_docblock(w, |w| {
write!(w, "");
render_attributes(w, it, true);
render_struct(w, it, Some(&s.generics), s.struct_type, &s.fields, "", true);
write!(w, "
")
});
document(w, cx, it);
let mut fields = s
.fields
.iter()
.filter_map(|f| match f.inner {
clean::StructFieldItem(ref ty) => Some((f, ty)),
_ => None,
})
.peekable();
if let doctree::Plain = s.struct_type {
if fields.peek().is_some() {
write!(
w,
"",
document_non_exhaustive_header(it)
);
document_non_exhaustive(w, it);
for (field, ty) in fields {
let id = cx.derive_id(format!(
"{}.{}",
ItemType::StructField,
field.name.as_ref().unwrap()
));
write!(
w,
"\
\
{name}: {ty}
\
",
item_type = ItemType::StructField,
id = id,
name = field.name.as_ref().unwrap(),
ty = ty.print()
);
document(w, cx, field);
}
}
}
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
}
fn item_union(w: &mut Buffer, cx: &Context, it: &clean::Item, s: &clean::Union) {
wrap_into_docblock(w, |w| {
write!(w, "");
render_attributes(w, it, true);
render_union(w, it, Some(&s.generics), &s.fields, "", true);
write!(w, "
")
});
document(w, cx, it);
let mut fields = s
.fields
.iter()
.filter_map(|f| match f.inner {
clean::StructFieldItem(ref ty) => Some((f, ty)),
_ => None,
})
.peekable();
if fields.peek().is_some() {
write!(
w,
""
);
for (field, ty) in fields {
let name = field.name.as_ref().expect("union field name");
let id = format!("{}.{}", ItemType::StructField, name);
write!(
w,
"\
\
{name}: {ty}
\
",
id = id,
name = name,
shortty = ItemType::StructField,
ty = ty.print()
);
if let Some(stability_class) = field.stability_class() {
write!(w, "", stab = stability_class);
}
document(w, cx, field);
}
}
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
}
fn item_enum(w: &mut Buffer, cx: &Context, it: &clean::Item, e: &clean::Enum) {
wrap_into_docblock(w, |w| {
write!(w, "");
render_attributes(w, it, true);
write!(
w,
"{}enum {}{}{}",
it.visibility.print_with_space(),
it.name.as_ref().unwrap(),
e.generics.print(),
WhereClause { gens: &e.generics, indent: 0, end_newline: true }
);
if e.variants.is_empty() && !e.variants_stripped {
write!(w, " {{}}");
} else {
write!(w, " {{\n");
for v in &e.variants {
write!(w, " ");
let name = v.name.as_ref().unwrap();
match v.inner {
clean::VariantItem(ref var) => match var.kind {
clean::VariantKind::CLike => write!(w, "{}", name),
clean::VariantKind::Tuple(ref tys) => {
write!(w, "{}(", name);
for (i, ty) in tys.iter().enumerate() {
if i > 0 {
write!(w, ", ")
}
write!(w, "{}", ty.print());
}
write!(w, ")");
}
clean::VariantKind::Struct(ref s) => {
render_struct(w, v, None, s.struct_type, &s.fields, " ", false);
}
},
_ => unreachable!(),
}
write!(w, ",\n");
}
if e.variants_stripped {
write!(w, " // some variants omitted\n");
}
write!(w, "}}");
}
write!(w, "
")
});
document(w, cx, it);
if !e.variants.is_empty() {
write!(
w,
"\n",
document_non_exhaustive_header(it)
);
document_non_exhaustive(w, it);
for variant in &e.variants {
let id =
cx.derive_id(format!("{}.{}", ItemType::Variant, variant.name.as_ref().unwrap()));
write!(
w,
"\
\
{name}",
id = id,
name = variant.name.as_ref().unwrap()
);
if let clean::VariantItem(ref var) = variant.inner {
if let clean::VariantKind::Tuple(ref tys) = var.kind {
write!(w, "(");
for (i, ty) in tys.iter().enumerate() {
if i > 0 {
write!(w, ", ");
}
write!(w, "{}", ty.print());
}
write!(w, ")");
}
}
write!(w, "
");
document(w, cx, variant);
document_non_exhaustive(w, variant);
use crate::clean::{Variant, VariantKind};
if let clean::VariantItem(Variant { kind: VariantKind::Struct(ref s) }) = variant.inner
{
let variant_id = cx.derive_id(format!(
"{}.{}.fields",
ItemType::Variant,
variant.name.as_ref().unwrap()
));
write!(w, "", id = variant_id);
write!(
w,
"
Fields of {name}
",
name = variant.name.as_ref().unwrap()
);
for field in &s.fields {
use crate::clean::StructFieldItem;
if let StructFieldItem(ref ty) = field.inner {
let id = cx.derive_id(format!(
"variant.{}.field.{}",
variant.name.as_ref().unwrap(),
field.name.as_ref().unwrap()
));
write!(
w,
"
\
\
{f}: {t}\
",
id = id,
f = field.name.as_ref().unwrap(),
t = ty.print()
);
document(w, cx, field);
}
}
write!(w, "
");
}
render_stability_since(w, variant, it);
}
}
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
}
const ALLOWED_ATTRIBUTES: &[Symbol] = &[
sym::export_name,
sym::lang,
sym::link_section,
sym::must_use,
sym::no_mangle,
sym::repr,
sym::non_exhaustive,
];
// The `top` parameter is used when generating the item declaration to ensure it doesn't have a
// left padding. For example:
//
// #[foo] <----- "top" attribute
// struct Foo {
// #[bar] <---- not "top" attribute
// bar: usize,
// }
fn render_attributes(w: &mut Buffer, it: &clean::Item, top: bool) {
let attrs = it
.attrs
.other_attrs
.iter()
.filter_map(|attr| {
if ALLOWED_ATTRIBUTES.contains(&attr.name_or_empty()) {
Some(pprust::attribute_to_string(&attr))
} else {
None
}
})
.join("\n");
if !attrs.is_empty() {
write!(
w,
"{}",
if top { " top-attr" } else { "" },
&attrs
);
}
}
fn render_struct(
w: &mut Buffer,
it: &clean::Item,
g: Option<&clean::Generics>,
ty: doctree::StructType,
fields: &[clean::Item],
tab: &str,
structhead: bool,
) {
write!(
w,
"{}{}{}",
it.visibility.print_with_space(),
if structhead { "struct " } else { "" },
it.name.as_ref().unwrap()
);
if let Some(g) = g {
write!(w, "{}", g.print())
}
match ty {
doctree::Plain => {
if let Some(g) = g {
write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: true })
}
let mut has_visible_fields = false;
write!(w, " {{");
for field in fields {
if let clean::StructFieldItem(ref ty) = field.inner {
write!(
w,
"\n{} {}{}: {},",
tab,
field.visibility.print_with_space(),
field.name.as_ref().unwrap(),
ty.print()
);
has_visible_fields = true;
}
}
if has_visible_fields {
if it.has_stripped_fields().unwrap() {
write!(w, "\n{} // some fields omitted", tab);
}
write!(w, "\n{}", tab);
} else if it.has_stripped_fields().unwrap() {
// If there are no visible fields we can just display
// `{ /* fields omitted */ }` to save space.
write!(w, " /* fields omitted */ ");
}
write!(w, "}}");
}
doctree::Tuple => {
write!(w, "(");
for (i, field) in fields.iter().enumerate() {
if i > 0 {
write!(w, ", ");
}
match field.inner {
clean::StrippedItem(box clean::StructFieldItem(..)) => write!(w, "_"),
clean::StructFieldItem(ref ty) => {
write!(w, "{}{}", field.visibility.print_with_space(), ty.print())
}
_ => unreachable!(),
}
}
write!(w, ")");
if let Some(g) = g {
write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: false })
}
write!(w, ";");
}
doctree::Unit => {
// Needed for PhantomData.
if let Some(g) = g {
write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: false })
}
write!(w, ";");
}
}
}
fn render_union(
w: &mut Buffer,
it: &clean::Item,
g: Option<&clean::Generics>,
fields: &[clean::Item],
tab: &str,
structhead: bool,
) {
write!(
w,
"{}{}{}",
it.visibility.print_with_space(),
if structhead { "union " } else { "" },
it.name.as_ref().unwrap()
);
if let Some(g) = g {
write!(w, "{}", g.print());
write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: true });
}
write!(w, " {{\n{}", tab);
for field in fields {
if let clean::StructFieldItem(ref ty) = field.inner {
write!(
w,
" {}{}: {},\n{}",
field.visibility.print_with_space(),
field.name.as_ref().unwrap(),
ty.print(),
tab
);
}
}
if it.has_stripped_fields().unwrap() {
write!(w, " // some fields omitted\n{}", tab);
}
write!(w, "}}");
}
#[derive(Copy, Clone)]
enum AssocItemLink<'a> {
Anchor(Option<&'a str>),
GotoSource(DefId, &'a FxHashSet),
}
impl<'a> AssocItemLink<'a> {
fn anchor(&self, id: &'a String) -> Self {
match *self {
AssocItemLink::Anchor(_) => AssocItemLink::Anchor(Some(&id)),
ref other => *other,
}
}
}
enum AssocItemRender<'a> {
All,
DerefFor { trait_: &'a clean::Type, type_: &'a clean::Type, deref_mut_: bool },
}
#[derive(Copy, Clone, PartialEq)]
enum RenderMode {
Normal,
ForDeref { mut_: bool },
}
fn render_assoc_items(
w: &mut Buffer,
cx: &Context,
containing_item: &clean::Item,
it: DefId,
what: AssocItemRender<'_>,
) {
let c = &cx.cache;
let v = match c.impls.get(&it) {
Some(v) => v,
None => return,
};
let (non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none());
if !non_trait.is_empty() {
let render_mode = match what {
AssocItemRender::All => {
write!(
w,
"\
\
"
);
RenderMode::Normal
}
AssocItemRender::DerefFor { trait_, type_, deref_mut_ } => {
write!(
w,
"\
\
",
trait_.print(),
type_.print()
);
RenderMode::ForDeref { mut_: deref_mut_ }
}
};
for i in &non_trait {
render_impl(
w,
cx,
i,
AssocItemLink::Anchor(None),
render_mode,
containing_item.stable_since(),
true,
None,
false,
true,
&[],
);
}
}
if let AssocItemRender::DerefFor { .. } = what {
return;
}
if !traits.is_empty() {
let deref_impl =
traits.iter().find(|t| t.inner_impl().trait_.def_id() == c.deref_trait_did);
if let Some(impl_) = deref_impl {
let has_deref_mut =
traits.iter().any(|t| t.inner_impl().trait_.def_id() == c.deref_mut_trait_did);
render_deref_methods(w, cx, impl_, containing_item, has_deref_mut);
}
let (synthetic, concrete): (Vec<&&Impl>, Vec<&&Impl>) =
traits.iter().partition(|t| t.inner_impl().synthetic);
let (blanket_impl, concrete): (Vec<&&Impl>, _) =
concrete.into_iter().partition(|t| t.inner_impl().blanket_impl.is_some());
let mut impls = Buffer::empty_from(&w);
render_impls(cx, &mut impls, &concrete, containing_item);
let impls = impls.into_inner();
if !impls.is_empty() {
write!(
w,
"\
\
{}
",
impls
);
}
if !synthetic.is_empty() {
write!(
w,
"\
\
\
"
);
render_impls(cx, w, &synthetic, containing_item);
write!(w, "
");
}
if !blanket_impl.is_empty() {
write!(
w,
"\
\
\
"
);
render_impls(cx, w, &blanket_impl, containing_item);
write!(w, "
");
}
}
}
fn render_deref_methods(
w: &mut Buffer,
cx: &Context,
impl_: &Impl,
container_item: &clean::Item,
deref_mut: bool,
) {
let deref_type = impl_.inner_impl().trait_.as_ref().unwrap();
let (target, real_target) = impl_
.inner_impl()
.items
.iter()
.find_map(|item| match item.inner {
clean::TypedefItem(ref t, true) => Some(match *t {
clean::Typedef { item_type: Some(ref type_), .. } => (type_, &t.type_),
_ => (&t.type_, &t.type_),
}),
_ => None,
})
.expect("Expected associated type binding");
let what =
AssocItemRender::DerefFor { trait_: deref_type, type_: real_target, deref_mut_: deref_mut };
if let Some(did) = target.def_id() {
render_assoc_items(w, cx, container_item, did, what);
} else {
if let Some(prim) = target.primitive_type() {
if let Some(&did) = cx.cache.primitive_locations.get(&prim) {
render_assoc_items(w, cx, container_item, did, what);
}
}
}
}
fn should_render_item(item: &clean::Item, deref_mut_: bool) -> bool {
let self_type_opt = match item.inner {
clean::MethodItem(ref method) => method.decl.self_type(),
clean::TyMethodItem(ref method) => method.decl.self_type(),
_ => None,
};
if let Some(self_ty) = self_type_opt {
let (by_mut_ref, by_box, by_value) = match self_ty {
SelfTy::SelfBorrowed(_, mutability)
| SelfTy::SelfExplicit(clean::BorrowedRef { mutability, .. }) => {
(mutability == Mutability::Mut, false, false)
}
SelfTy::SelfExplicit(clean::ResolvedPath { did, .. }) => {
(false, Some(did) == cache().owned_box_did, false)
}
SelfTy::SelfValue => (false, false, true),
_ => (false, false, false),
};
(deref_mut_ || !by_mut_ref) && !by_box && !by_value
} else {
false
}
}
fn render_impl(
w: &mut Buffer,
cx: &Context,
i: &Impl,
link: AssocItemLink<'_>,
render_mode: RenderMode,
outer_version: Option<&str>,
show_def_docs: bool,
use_absolute: Option,
is_on_foreign_type: bool,
show_default_items: bool,
// This argument is used to reference same type with different paths to avoid duplication
// in documentation pages for trait with automatic implementations like "Send" and "Sync".
aliases: &[String],
) {
if render_mode == RenderMode::Normal {
let id = cx.derive_id(match i.inner_impl().trait_ {
Some(ref t) => {
if is_on_foreign_type {
get_id_for_impl_on_foreign_type(&i.inner_impl().for_, t)
} else {
format!("impl-{}", small_url_encode(&format!("{:#}", t.print())))
}
}
None => "impl".to_string(),
});
let aliases = if aliases.is_empty() {
String::new()
} else {
format!(" aliases=\"{}\"", aliases.join(","))
};
if let Some(use_absolute) = use_absolute {
write!(w, "", id, aliases);
fmt_impl_for_trait_page(&i.inner_impl(), w, use_absolute);
if show_def_docs {
for it in &i.inner_impl().items {
if let clean::TypedefItem(ref tydef, _) = it.inner {
write!(w, " ");
assoc_type(w, it, &[], Some(&tydef.type_), AssocItemLink::Anchor(None), "");
write!(w, ";");
}
}
}
write!(w, "
");
} else {
write!(
w,
"{}
",
id,
aliases,
i.inner_impl().print()
);
}
write!(w, "", id);
let since = i.impl_item.stability.as_ref().map(|s| &s.since[..]);
render_stability_since_raw(w, since, outer_version);
if let Some(l) = cx.src_href(&i.impl_item) {
write!(w, "[src]", l, "goto source code");
}
write!(w, "
");
if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) {
let mut ids = cx.id_map.borrow_mut();
write!(
w,
"
{}
",
Markdown(
&*dox,
&i.impl_item.links(),
&mut ids,
cx.shared.codes,
cx.shared.edition,
&cx.shared.playground
)
.to_string()
);
}
}
fn doc_impl_item(
w: &mut Buffer,
cx: &Context,
item: &clean::Item,
link: AssocItemLink<'_>,
render_mode: RenderMode,
is_default_item: bool,
outer_version: Option<&str>,
trait_: Option<&clean::Trait>,
show_def_docs: bool,
) {
let item_type = item.type_();
let name = item.name.as_ref().unwrap();
let render_method_item = match render_mode {
RenderMode::Normal => true,
RenderMode::ForDeref { mut_: deref_mut_ } => should_render_item(&item, deref_mut_),
};
let (is_hidden, extra_class) =
if (trait_.is_none() || item.doc_value().is_some() || item.inner.is_associated())
&& !is_default_item
{
(false, "")
} else {
(true, " hidden")
};
match item.inner {
clean::MethodItem(clean::Method { .. })
| clean::TyMethodItem(clean::TyMethod { .. }) => {
// Only render when the method is not static or we allow static methods
if render_method_item {
let id = cx.derive_id(format!("{}.{}", item_type, name));
write!(w, "", id, item_type, extra_class);
render_assoc_item(w, item, link.anchor(&id), ItemType::Impl);
write!(w, "
");
render_stability_since_raw(w, item.stable_since(), outer_version);
if let Some(l) = cx.src_href(item) {
write!(
w,
"[src]",
l, "goto source code"
);
}
write!(w, "
");
}
}
clean::TypedefItem(ref tydef, _) => {
let id = cx.derive_id(format!("{}.{}", ItemType::AssocType, name));
write!(w, "", id, item_type, extra_class);
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id), "");
write!(w, "
");
}
clean::AssocConstItem(ref ty, ref default) => {
let id = cx.derive_id(format!("{}.{}", item_type, name));
write!(w, "", id, item_type, extra_class);
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id), "");
write!(w, "
");
render_stability_since_raw(w, item.stable_since(), outer_version);
if let Some(l) = cx.src_href(item) {
write!(
w,
"[src]",
l, "goto source code"
);
}
write!(w, "
");
}
clean::AssocTypeItem(ref bounds, ref default) => {
let id = cx.derive_id(format!("{}.{}", item_type, name));
write!(w, "", id, item_type, extra_class);
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id), "");
write!(w, "
");
}
clean::StrippedItem(..) => return,
_ => panic!("can't make docs for trait item with name {:?}", item.name),
}
if render_method_item {
if !is_default_item {
if let Some(t) = trait_ {
// The trait item may have been stripped so we might not
// find any documentation or stability for it.
if let Some(it) = t.items.iter().find(|i| i.name == item.name) {
// We need the stability of the item from the trait
// because impls can't have a stability.
document_stability(w, cx, it, is_hidden);
if item.doc_value().is_some() {
document_full(w, item, cx, "", is_hidden);
} else if show_def_docs {
// In case the item isn't documented,
// provide short documentation from the trait.
document_short(w, cx, it, link, "", is_hidden);
}
}
} else {
document_stability(w, cx, item, is_hidden);
if show_def_docs {
document_full(w, item, cx, "", is_hidden);
}
}
} else {
document_stability(w, cx, item, is_hidden);
if show_def_docs {
document_short(w, cx, item, link, "", is_hidden);
}
}
}
}
let traits = &cx.cache.traits;
let trait_ = i.trait_did().map(|did| &traits[&did]);
write!(w, "");
for trait_item in &i.inner_impl().items {
doc_impl_item(
w,
cx,
trait_item,
link,
render_mode,
false,
outer_version,
trait_,
show_def_docs,
);
}
fn render_default_items(
w: &mut Buffer,
cx: &Context,
t: &clean::Trait,
i: &clean::Impl,
render_mode: RenderMode,
outer_version: Option<&str>,
show_def_docs: bool,
) {
for trait_item in &t.items {
let n = trait_item.name.clone();
if i.items.iter().any(|m| m.name == n) {
continue;
}
let did = i.trait_.as_ref().unwrap().def_id().unwrap();
let assoc_link = AssocItemLink::GotoSource(did, &i.provided_trait_methods);
doc_impl_item(
w,
cx,
trait_item,
assoc_link,
render_mode,
true,
outer_version,
None,
show_def_docs,
);
}
}
// If we've implemented a trait, then also emit documentation for all
// default items which weren't overridden in the implementation block.
// We don't emit documentation for default items if they appear in the
// Implementations on Foreign Types or Implementors sections.
if show_default_items {
if let Some(t) = trait_ {
render_default_items(
w,
cx,
t,
&i.inner_impl(),
render_mode,
outer_version,
show_def_docs,
);
}
}
write!(w, "
");
}
fn item_opaque_ty(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::OpaqueTy) {
write!(w, "");
render_attributes(w, it, false);
write!(
w,
"type {}{}{where_clause} = impl {bounds};
",
it.name.as_ref().unwrap(),
t.generics.print(),
where_clause = WhereClause { gens: &t.generics, indent: 0, end_newline: true },
bounds = bounds(&t.bounds, false)
);
document(w, cx, it);
// Render any items associated directly to this alias, as otherwise they
// won't be visible anywhere in the docs. It would be nice to also show
// associated items from the aliased type (see discussion in #32077), but
// we need #14072 to make sense of the generics.
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
}
fn item_trait_alias(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::TraitAlias) {
write!(w, "");
render_attributes(w, it, false);
write!(
w,
"trait {}{}{} = {};
",
it.name.as_ref().unwrap(),
t.generics.print(),
WhereClause { gens: &t.generics, indent: 0, end_newline: true },
bounds(&t.bounds, true)
);
document(w, cx, it);
// Render any items associated directly to this alias, as otherwise they
// won't be visible anywhere in the docs. It would be nice to also show
// associated items from the aliased type (see discussion in #32077), but
// we need #14072 to make sense of the generics.
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
}
fn item_typedef(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Typedef) {
write!(w, "");
render_attributes(w, it, false);
write!(
w,
"type {}{}{where_clause} = {type_};
",
it.name.as_ref().unwrap(),
t.generics.print(),
where_clause = WhereClause { gens: &t.generics, indent: 0, end_newline: true },
type_ = t.type_.print()
);
document(w, cx, it);
// Render any items associated directly to this alias, as otherwise they
// won't be visible anywhere in the docs. It would be nice to also show
// associated items from the aliased type (see discussion in #32077), but
// we need #14072 to make sense of the generics.
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
}
fn item_foreign_type(w: &mut Buffer, cx: &Context, it: &clean::Item) {
writeln!(w, "extern {{");
render_attributes(w, it, false);
write!(
w,
" {}type {};\n}}
",
it.visibility.print_with_space(),
it.name.as_ref().unwrap(),
);
document(w, cx, it);
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
}
fn print_sidebar(cx: &Context, it: &clean::Item, buffer: &mut Buffer) {
let parentlen = cx.current.len() - if it.is_mod() { 1 } else { 0 };
if it.is_struct()
|| it.is_trait()
|| it.is_primitive()
|| it.is_union()
|| it.is_enum()
|| it.is_mod()
|| it.is_typedef()
{
write!(
buffer,
"{}{}
",
match it.inner {
clean::StructItem(..) => "Struct ",
clean::TraitItem(..) => "Trait ",
clean::PrimitiveItem(..) => "Primitive Type ",
clean::UnionItem(..) => "Union ",
clean::EnumItem(..) => "Enum ",
clean::TypedefItem(..) => "Type Definition ",
clean::ForeignTypeItem => "Foreign Type ",
clean::ModuleItem(..) =>
if it.is_crate() {
"Crate "
} else {
"Module "
},
_ => "",
},
it.name.as_ref().unwrap()
);
}
if it.is_crate() {
if let Some(ref version) = cx.cache.crate_version {
write!(
buffer,
"",
Escape(version)
);
}
}
write!(buffer, "");
}
fn get_next_url(used_links: &mut FxHashSet, url: String) -> String {
if used_links.insert(url.clone()) {
return url;
}
let mut add = 1;
while !used_links.insert(format!("{}-{}", url, add)) {
add += 1;
}
format!("{}-{}", url, add)
}
fn get_methods(
i: &clean::Impl,
for_deref: bool,
used_links: &mut FxHashSet,
deref_mut: bool,
) -> Vec {
i.items
.iter()
.filter_map(|item| match item.name {
Some(ref name) if !name.is_empty() && item.is_method() => {
if !for_deref || should_render_item(item, deref_mut) {
Some(format!(
"{}",
get_next_url(used_links, format!("method.{}", name)),
name
))
} else {
None
}
}
_ => None,
})
.collect::>()
}
// The point is to url encode any potential character from a type with genericity.
fn small_url_encode(s: &str) -> String {
s.replace("<", "%3C")
.replace(">", "%3E")
.replace(" ", "%20")
.replace("?", "%3F")
.replace("'", "%27")
.replace("&", "%26")
.replace(",", "%2C")
.replace(":", "%3A")
.replace(";", "%3B")
.replace("[", "%5B")
.replace("]", "%5D")
.replace("\"", "%22")
}
fn sidebar_assoc_items(it: &clean::Item) -> String {
let mut out = String::new();
let c = cache();
if let Some(v) = c.impls.get(&it.def_id) {
let mut used_links = FxHashSet::default();
{
let used_links_bor = &mut used_links;
let mut ret = v
.iter()
.filter(|i| i.inner_impl().trait_.is_none())
.flat_map(move |i| get_methods(i.inner_impl(), false, used_links_bor, false))
.collect::>();
if !ret.is_empty() {
// We want links' order to be reproducible so we don't use unstable sort.
ret.sort();
out.push_str(&format!(
"\
",
ret.join("")
));
}
}
if v.iter().any(|i| i.inner_impl().trait_.is_some()) {
if let Some(impl_) = v
.iter()
.filter(|i| i.inner_impl().trait_.is_some())
.find(|i| i.inner_impl().trait_.def_id() == c.deref_trait_did)
{
if let Some((target, real_target)) =
impl_.inner_impl().items.iter().find_map(|item| match item.inner {
clean::TypedefItem(ref t, true) => Some(match *t {
clean::Typedef { item_type: Some(ref type_), .. } => (type_, &t.type_),
_ => (&t.type_, &t.type_),
}),
_ => None,
})
{
let deref_mut = v
.iter()
.filter(|i| i.inner_impl().trait_.is_some())
.any(|i| i.inner_impl().trait_.def_id() == c.deref_mut_trait_did);
let inner_impl = target
.def_id()
.or(target
.primitive_type()
.and_then(|prim| c.primitive_locations.get(&prim).cloned()))
.and_then(|did| c.impls.get(&did));
if let Some(impls) = inner_impl {
out.push_str("");
let mut ret = impls
.iter()
.filter(|i| i.inner_impl().trait_.is_none())
.flat_map(|i| {
get_methods(i.inner_impl(), true, &mut used_links, deref_mut)
})
.collect::>();
// We want links' order to be reproducible so we don't use unstable sort.
ret.sort();
if !ret.is_empty() {
out.push_str(&format!(
"",
ret.join("")
));
}
}
}
}
let format_impls = |impls: Vec<&Impl>| {
let mut links = FxHashSet::default();
let mut ret = impls
.iter()
.filter_map(|i| {
let is_negative_impl = is_negative_impl(i.inner_impl());
if let Some(ref i) = i.inner_impl().trait_ {
let i_display = format!("{:#}", i.print());
let out = Escape(&i_display);
let encoded = small_url_encode(&format!("{:#}", i.print()));
let generated = format!(
"{}{}",
encoded,
if is_negative_impl { "!" } else { "" },
out
);
if links.insert(generated.clone()) { Some(generated) } else { None }
} else {
None
}
})
.collect::>();
ret.sort();
ret.join("")
};
let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) =
v.iter().partition::, _>(|i| i.inner_impl().synthetic);
let (blanket_impl, concrete): (Vec<&Impl>, Vec<&Impl>) = concrete
.into_iter()
.partition::, _>(|i| i.inner_impl().blanket_impl.is_some());
let concrete_format = format_impls(concrete);
let synthetic_format = format_impls(synthetic);
let blanket_format = format_impls(blanket_impl);
if !concrete_format.is_empty() {
out.push_str(
"",
);
out.push_str(&format!("", concrete_format));
}
if !synthetic_format.is_empty() {
out.push_str(
"",
);
out.push_str(&format!("", synthetic_format));
}
if !blanket_format.is_empty() {
out.push_str(
"",
);
out.push_str(&format!("", blanket_format));
}
}
}
out
}
fn sidebar_struct(buf: &mut Buffer, it: &clean::Item, s: &clean::Struct) {
let mut sidebar = String::new();
let fields = get_struct_fields_name(&s.fields);
if !fields.is_empty() {
if let doctree::Plain = s.struct_type {
sidebar.push_str(&format!(
"\
",
fields
));
}
}
sidebar.push_str(&sidebar_assoc_items(it));
if !sidebar.is_empty() {
write!(buf, "{}
", sidebar);
}
}
fn get_id_for_impl_on_foreign_type(for_: &clean::Type, trait_: &clean::Type) -> String {
small_url_encode(&format!("impl-{:#}-for-{:#}", trait_.print(), for_.print()))
}
fn extract_for_impl_name(item: &clean::Item) -> Option<(String, String)> {
match item.inner {
clean::ItemEnum::ImplItem(ref i) => {
if let Some(ref trait_) = i.trait_ {
Some((
format!("{:#}", i.for_.print()),
get_id_for_impl_on_foreign_type(&i.for_, trait_),
))
} else {
None
}
}
_ => None,
}
}
fn is_negative_impl(i: &clean::Impl) -> bool {
i.polarity == Some(clean::ImplPolarity::Negative)
}
fn sidebar_trait(buf: &mut Buffer, it: &clean::Item, t: &clean::Trait) {
let mut sidebar = String::new();
let mut types = t
.items
.iter()
.filter_map(|m| match m.name {
Some(ref name) if m.is_associated_type() => {
Some(format!("{name}", name = name))
}
_ => None,
})
.collect::>();
let mut consts = t
.items
.iter()
.filter_map(|m| match m.name {
Some(ref name) if m.is_associated_const() => {
Some(format!("{name}", name = name))
}
_ => None,
})
.collect::>();
let mut required = t
.items
.iter()
.filter_map(|m| match m.name {
Some(ref name) if m.is_ty_method() => {
Some(format!("{name}", name = name))
}
_ => None,
})
.collect::>();
let mut provided = t
.items
.iter()
.filter_map(|m| match m.name {
Some(ref name) if m.is_method() => {
Some(format!("{0}", name))
}
_ => None,
})
.collect::>();
if !types.is_empty() {
types.sort();
sidebar.push_str(&format!(
"",
types.join("")
));
}
if !consts.is_empty() {
consts.sort();
sidebar.push_str(&format!(
"",
consts.join("")
));
}
if !required.is_empty() {
required.sort();
sidebar.push_str(&format!(
"",
required.join("")
));
}
if !provided.is_empty() {
provided.sort();
sidebar.push_str(&format!(
"",
provided.join("")
));
}
let c = cache();
if let Some(implementors) = c.implementors.get(&it.def_id) {
let mut res = implementors
.iter()
.filter(|i| i.inner_impl().for_.def_id().map_or(false, |d| !c.paths.contains_key(&d)))
.filter_map(|i| extract_for_impl_name(&i.impl_item))
.collect::>();
if !res.is_empty() {
res.sort();
sidebar.push_str(&format!(
"",
res.into_iter()
.map(|(name, id)| format!("{}", id, Escape(&name)))
.collect::>()
.join("")
));
}
}
sidebar.push_str(&sidebar_assoc_items(it));
sidebar.push_str("");
if t.auto {
sidebar.push_str(
"",
);
}
write!(buf, "{}
", sidebar)
}
fn sidebar_primitive(buf: &mut Buffer, it: &clean::Item) {
let sidebar = sidebar_assoc_items(it);
if !sidebar.is_empty() {
write!(buf, "{}
", sidebar);
}
}
fn sidebar_typedef(buf: &mut Buffer, it: &clean::Item) {
let sidebar = sidebar_assoc_items(it);
if !sidebar.is_empty() {
write!(buf, "{}
", sidebar);
}
}
fn get_struct_fields_name(fields: &[clean::Item]) -> String {
let mut fields = fields
.iter()
.filter(|f| if let clean::StructFieldItem(..) = f.inner { true } else { false })
.filter_map(|f| match f.name {
Some(ref name) => {
Some(format!("{name}", name = name))
}
_ => None,
})
.collect::>();
fields.sort();
fields.join("")
}
fn sidebar_union(buf: &mut Buffer, it: &clean::Item, u: &clean::Union) {
let mut sidebar = String::new();
let fields = get_struct_fields_name(&u.fields);
if !fields.is_empty() {
sidebar.push_str(&format!(
"\
",
fields
));
}
sidebar.push_str(&sidebar_assoc_items(it));
if !sidebar.is_empty() {
write!(buf, "{}
", sidebar);
}
}
fn sidebar_enum(buf: &mut Buffer, it: &clean::Item, e: &clean::Enum) {
let mut sidebar = String::new();
let mut variants = e
.variants
.iter()
.filter_map(|v| match v.name {
Some(ref name) => Some(format!("{name}", name = name)),
_ => None,
})
.collect::>();
if !variants.is_empty() {
variants.sort_unstable();
sidebar.push_str(&format!(
"\
",
variants.join(""),
));
}
sidebar.push_str(&sidebar_assoc_items(it));
if !sidebar.is_empty() {
write!(buf, "{}
", sidebar);
}
}
fn item_ty_to_strs(ty: &ItemType) -> (&'static str, &'static str) {
match *ty {
ItemType::ExternCrate | ItemType::Import => ("reexports", "Re-exports"),
ItemType::Module => ("modules", "Modules"),
ItemType::Struct => ("structs", "Structs"),
ItemType::Union => ("unions", "Unions"),
ItemType::Enum => ("enums", "Enums"),
ItemType::Function => ("functions", "Functions"),
ItemType::Typedef => ("types", "Type Definitions"),
ItemType::Static => ("statics", "Statics"),
ItemType::Constant => ("constants", "Constants"),
ItemType::Trait => ("traits", "Traits"),
ItemType::Impl => ("impls", "Implementations"),
ItemType::TyMethod => ("tymethods", "Type Methods"),
ItemType::Method => ("methods", "Methods"),
ItemType::StructField => ("fields", "Struct Fields"),
ItemType::Variant => ("variants", "Variants"),
ItemType::Macro => ("macros", "Macros"),
ItemType::Primitive => ("primitives", "Primitive Types"),
ItemType::AssocType => ("associated-types", "Associated Types"),
ItemType::AssocConst => ("associated-consts", "Associated Constants"),
ItemType::ForeignType => ("foreign-types", "Foreign Types"),
ItemType::Keyword => ("keywords", "Keywords"),
ItemType::OpaqueTy => ("opaque-types", "Opaque Types"),
ItemType::ProcAttribute => ("attributes", "Attribute Macros"),
ItemType::ProcDerive => ("derives", "Derive Macros"),
ItemType::TraitAlias => ("trait-aliases", "Trait aliases"),
}
}
fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
let mut sidebar = String::new();
if items.iter().any(|it| it.type_() == ItemType::ExternCrate || it.type_() == ItemType::Import)
{
sidebar.push_str(&format!(
"{name}",
id = "reexports",
name = "Re-exports"
));
}
// ordering taken from item_module, reorder, where it prioritized elements in a certain order
// to print its headings
for &myty in &[
ItemType::Primitive,
ItemType::Module,
ItemType::Macro,
ItemType::Struct,
ItemType::Enum,
ItemType::Constant,
ItemType::Static,
ItemType::Trait,
ItemType::Function,
ItemType::Typedef,
ItemType::Union,
ItemType::Impl,
ItemType::TyMethod,
ItemType::Method,
ItemType::StructField,
ItemType::Variant,
ItemType::AssocType,
ItemType::AssocConst,
ItemType::ForeignType,
ItemType::Keyword,
] {
if items.iter().any(|it| !it.is_stripped() && it.type_() == myty) {
let (short, name) = item_ty_to_strs(&myty);
sidebar.push_str(&format!(
"{name}",
id = short,
name = name
));
}
}
if !sidebar.is_empty() {
write!(buf, "", sidebar);
}
}
fn sidebar_foreign_type(buf: &mut Buffer, it: &clean::Item) {
let sidebar = sidebar_assoc_items(it);
if !sidebar.is_empty() {
write!(buf, "{}
", sidebar);
}
}
fn item_macro(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Macro) {
wrap_into_docblock(w, |w| {
w.write_str(&highlight::render_with_highlighting(&t.source, Some("macro"), None, None))
});
document(w, cx, it)
}
fn item_proc_macro(w: &mut Buffer, cx: &Context, it: &clean::Item, m: &clean::ProcMacro) {
let name = it.name.as_ref().expect("proc-macros always have names");
match m.kind {
MacroKind::Bang => {
write!(w, "");
write!(w, "{}!() {{ /* proc-macro */ }}", name);
write!(w, "
");
}
MacroKind::Attr => {
write!(w, "");
write!(w, "#[{}]", name);
write!(w, "
");
}
MacroKind::Derive => {
write!(w, "");
write!(w, "#[derive({})]", name);
if !m.helpers.is_empty() {
writeln!(w, "\n{{");
writeln!(w, " // Attributes available to this derive:");
for attr in &m.helpers {
writeln!(w, " #[{}]", attr);
}
write!(w, "}}");
}
write!(w, "
");
}
}
document(w, cx, it)
}
fn item_primitive(w: &mut Buffer, cx: &Context, it: &clean::Item) {
document(w, cx, it);
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
}
fn item_keyword(w: &mut Buffer, cx: &Context, it: &clean::Item) {
document(w, cx, it)
}
crate const BASIC_KEYWORDS: &str = "rust, rustlang, rust-lang";
fn make_item_keywords(it: &clean::Item) -> String {
format!("{}, {}", BASIC_KEYWORDS, it.name.as_ref().unwrap())
}
/// Returns a list of all paths used in the type.
/// This is used to help deduplicate imported impls
/// for reexported types. If any of the contained
/// types are re-exported, we don't use the corresponding
/// entry from the js file, as inlining will have already
/// picked up the impl
fn collect_paths_for_type(first_ty: clean::Type) -> Vec {
let mut out = Vec::new();
let mut visited = FxHashSet::default();
let mut work = VecDeque::new();
let cache = cache();
work.push_back(first_ty);
while let Some(ty) = work.pop_front() {
if !visited.insert(ty.clone()) {
continue;
}
match ty {
clean::Type::ResolvedPath { did, .. } => {
let get_extern = || cache.external_paths.get(&did).map(|s| s.0.clone());
let fqp = cache.exact_paths.get(&did).cloned().or_else(get_extern);
if let Some(path) = fqp {
out.push(path.join("::"));
}
}
clean::Type::Tuple(tys) => {
work.extend(tys.into_iter());
}
clean::Type::Slice(ty) => {
work.push_back(*ty);
}
clean::Type::Array(ty, _) => {
work.push_back(*ty);
}
clean::Type::RawPointer(_, ty) => {
work.push_back(*ty);
}
clean::Type::BorrowedRef { type_, .. } => {
work.push_back(*type_);
}
clean::Type::QPath { self_type, trait_, .. } => {
work.push_back(*self_type);
work.push_back(*trait_);
}
_ => {}
}
}
out
}
crate fn cache() -> Arc {
CACHE_KEY.with(|c| c.borrow().clone())
}