]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/clean/def_ctor.rs
Remove Ty prefix from Ty{Adt|Array|Slice|RawPtr|Ref|FnDef|FnPtr|Dynamic|Closure|Gener...
[rust.git] / src / librustdoc / clean / def_ctor.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use core::DocContext;
12
13 use super::*;
14
15 pub fn get_def_from_def_id<F>(cx: &DocContext,
16                               def_id: DefId,
17                               callback: &F,
18 ) -> Vec<Item>
19 where F: Fn(& dyn Fn(DefId) -> Def) -> Vec<Item> {
20     let ty = cx.tcx.type_of(def_id);
21
22     match ty.sty {
23         ty::Adt(adt, _) => callback(&match adt.adt_kind() {
24             AdtKind::Struct => Def::Struct,
25             AdtKind::Enum => Def::Enum,
26             AdtKind::Union => Def::Union,
27         }),
28         ty::TyInt(_) |
29         ty::TyUint(_) |
30         ty::TyFloat(_) |
31         ty::TyStr |
32         ty::TyBool |
33         ty::TyChar => callback(&move |_: DefId| {
34             match ty.sty {
35                 ty::TyInt(x) => Def::PrimTy(hir::TyInt(x)),
36                 ty::TyUint(x) => Def::PrimTy(hir::TyUint(x)),
37                 ty::TyFloat(x) => Def::PrimTy(hir::TyFloat(x)),
38                 ty::TyStr => Def::PrimTy(hir::TyStr),
39                 ty::TyBool => Def::PrimTy(hir::TyBool),
40                 ty::TyChar => Def::PrimTy(hir::TyChar),
41                 _ => unreachable!(),
42             }
43         }),
44         _ => {
45             debug!("Unexpected type {:?}", def_id);
46             Vec::new()
47         }
48     }
49 }
50
51 pub fn get_def_from_node_id<F>(cx: &DocContext,
52                                id: ast::NodeId,
53                                name: String,
54                                callback: &F,
55 ) -> Vec<Item>
56 where F: Fn(& dyn Fn(DefId) -> Def, String) -> Vec<Item> {
57     let item = &cx.tcx.hir.expect_item(id).node;
58
59     callback(&match *item {
60         hir::ItemKind::Struct(_, _) => Def::Struct,
61         hir::ItemKind::Union(_, _) => Def::Union,
62         hir::ItemKind::Enum(_, _) => Def::Enum,
63         _ => panic!("Unexpected type {:?} {:?}", item, id),
64     }, name)
65 }