Document get_def_path

This commit is contained in:
Michael Wright 2019-02-04 07:29:19 +02:00
parent b6c3a6a09f
commit f3ee53d225

View File

@ -130,6 +130,15 @@ pub fn match_def_path(tcx: TyCtxt<'_, '_, '_>, def_id: DefId, path: &[&str]) ->
apb.names.len() == path.len() && apb.names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b)
}
/// Get the absolute path of `def_id` as a vector of `&str`.
///
/// # Examples
/// ```rust,ignore
/// let def_path = get_def_path(tcx, def_id);
/// if let &["core", "option", "Option"] = &def_path[..] {
/// // The given `def_id` is that of an `Option` type
/// };
/// ```
pub fn get_def_path(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Vec<&'static str> {
let mut apb = AbsolutePathBuffer { names: vec![] };
tcx.push_item_path(&mut apb, def_id, false);