From 5d99ba1d9a5acf02a5cd50e456f164bd80b523b5 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 4 Feb 2021 20:49:24 +0100 Subject: Make `ModPath`'s representation private --- crates/hir_def/src/path.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'crates/hir_def/src/path.rs') diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs index 84ea09b53..0caad53d3 100644 --- a/crates/hir_def/src/path.rs +++ b/crates/hir_def/src/path.rs @@ -20,7 +20,7 @@ use crate::{ #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ModPath { pub kind: PathKind, - pub segments: Vec, + segments: Vec, } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -53,6 +53,11 @@ impl ModPath { ModPath { kind, segments } } + /// Creates a `ModPath` from a `PathKind`, with no extra path segments. + pub const fn from_kind(kind: PathKind) -> ModPath { + ModPath { kind, segments: Vec::new() } + } + /// Calls `cb` with all paths, represented by this use item. pub(crate) fn expand_use_item( item_src: InFile, @@ -64,6 +69,18 @@ impl ModPath { } } + pub fn segments(&self) -> &[Name] { + &self.segments + } + + pub fn push_segment(&mut self, segment: Name) { + self.segments.push(segment); + } + + pub fn pop_segment(&mut self) -> Option { + self.segments.pop() + } + /// Returns the number of segments in the path (counting special segments like `$crate` and /// `super`). pub fn len(&self) -> usize { @@ -78,7 +95,7 @@ impl ModPath { } pub fn is_ident(&self) -> bool { - self.kind == PathKind::Plain && self.segments.len() == 1 + self.as_ident().is_some() } pub fn is_self(&self) -> bool { @@ -87,10 +104,14 @@ impl ModPath { /// If this path is a single identifier, like `foo`, return its name. pub fn as_ident(&self) -> Option<&Name> { - if !self.is_ident() { + if self.kind != PathKind::Plain { return None; } - self.segments.first() + + match &*self.segments { + [name] => Some(name), + _ => None, + } } } -- cgit v1.2.3