solar_ast/ast/
path.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use super::Box;
use solar_data_structures::smallvec::SmallVec;
use solar_interface::{Ident, Span, Symbol};
use std::fmt;

/// A boxed [`PathSlice`].
pub type AstPath<'ast> = Box<'ast, PathSlice>;

/// A qualified identifier: `foo.bar.baz`.
///
/// This is a list of identifiers, and is never empty.
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct PathSlice([Ident]);

impl ToOwned for PathSlice {
    type Owned = Path;

    #[inline]
    fn to_owned(&self) -> Self::Owned {
        Path::new(&self.0)
    }
}

impl fmt::Debug for PathSlice {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl fmt::Display for PathSlice {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (i, ident) in self.segments().iter().enumerate() {
            if i != 0 {
                f.write_str(".")?;
            }
            write!(f, "{ident}")?;
        }
        Ok(())
    }
}

impl PartialEq<Ident> for PathSlice {
    fn eq(&self, other: &Ident) -> bool {
        match self.get_ident() {
            Some(ident) => ident == other,
            None => false,
        }
    }
}

impl PartialEq<Symbol> for PathSlice {
    fn eq(&self, other: &Symbol) -> bool {
        match self.get_ident() {
            Some(ident) => ident.name == *other,
            None => false,
        }
    }
}

impl PathSlice {
    /// Creates a new path from a single ident.
    #[inline]
    pub fn from_ref(segment: &Ident) -> &Self {
        unsafe { Self::from_slice_unchecked(std::slice::from_ref(segment)) }
    }

    /// Creates a new path from a single ident.
    #[inline]
    pub fn from_mut(segment: &mut Ident) -> &mut Self {
        unsafe { Self::from_mut_slice_unchecked(std::slice::from_mut(segment)) }
    }

    /// Creates a new path from a slice of segments.
    ///
    /// # Panics
    ///
    /// Panics if `segments` is empty.
    #[inline]
    pub fn from_slice(segments: &[Ident]) -> &Self {
        assert!(!segments.is_empty());
        // SAFETY: `segments` is not empty.
        unsafe { Self::from_slice_unchecked(segments) }
    }

    /// Creates a new path from a slice of segments.
    ///
    /// # Safety
    ///
    /// The caller must ensure that `segments` is not empty.
    #[inline]
    pub unsafe fn from_slice_unchecked(segments: &[Ident]) -> &Self {
        // SAFETY: We're just a wrapper around a slice `[Ident]`.
        unsafe { &*(segments as *const _ as *const Self) }
    }

    /// Creates a new path from a mutable slice of segments.
    ///
    /// # Panics
    ///
    /// Panics if `segments` is empty.
    #[inline]
    pub fn from_mut_slice(segments: &mut [Ident]) -> &mut Self {
        assert!(!segments.is_empty());
        // SAFETY: `segments` is not empty.
        unsafe { Self::from_mut_slice_unchecked(segments) }
    }

    /// Creates a new path from a mutable slice of segments.
    ///
    /// # Safety
    ///
    /// The caller must ensure that `segments` is not empty.
    #[inline]
    pub unsafe fn from_mut_slice_unchecked(segments: &mut [Ident]) -> &mut Self {
        // SAFETY: We're just a wrapper around a slice `[Ident]`.
        unsafe { &mut *(segments as *mut _ as *mut Self) }
    }

    /// Returns the path's span.
    #[inline]
    pub fn span(&self) -> Span {
        match self.segments() {
            [] => unreachable!(),
            [ident] => ident.span,
            [first, .., last] => first.span.with_hi(last.span.hi()),
        }
    }

    /// Returns the path's segments.
    #[inline]
    pub fn segments(&self) -> &[Ident] {
        &self.0
    }

    /// Returns the path's segments.
    #[inline]
    pub fn segments_mut(&mut self) -> &mut [Ident] {
        &mut self.0
    }

    /// If this path consists of a single ident, returns the ident.
    #[inline]
    pub fn get_ident(&self) -> Option<&Ident> {
        match self.segments() {
            [ident] => Some(ident),
            _ => None,
        }
    }

    /// If this path consists of a single ident, returns the ident.
    #[inline]
    pub fn get_ident_mut(&mut self) -> Option<&mut Ident> {
        match self.segments_mut() {
            [ident] => Some(ident),
            _ => None,
        }
    }

    /// Returns the first segment of the path.
    #[inline]
    pub fn first(&self) -> &Ident {
        self.segments().first().expect("paths cannot be empty")
    }

    /// Returns the first segment of the path.
    #[inline]
    pub fn first_mut(&mut self) -> &mut Ident {
        self.segments_mut().first_mut().expect("paths cannot be empty")
    }

    /// Returns the last segment of the path.
    #[inline]
    pub fn last(&self) -> &Ident {
        self.segments().last().expect("paths cannot be empty")
    }

    /// Returns the last segment of the path.
    #[inline]
    pub fn last_mut(&mut self) -> &mut Ident {
        self.segments_mut().last_mut().expect("paths cannot be empty")
    }
}

/// A qualified identifier: `foo.bar.baz`.
///
/// This is a list of identifiers, and is never empty.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Path(SmallVec<[Ident; 1]>);

impl fmt::Debug for Path {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self.as_slice(), f)
    }
}

impl fmt::Display for Path {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self.as_slice(), f)
    }
}

impl PartialEq<Ident> for Path {
    fn eq(&self, other: &Ident) -> bool {
        PartialEq::eq(self.as_slice(), other)
    }
}

impl PartialEq<Symbol> for Path {
    fn eq(&self, other: &Symbol) -> bool {
        PartialEq::eq(self.as_slice(), other)
    }
}

impl FromIterator<Ident> for Path {
    /// Creates a path from an iterator of idents.
    ///
    /// # Panics
    ///
    /// Panics if the iterator is empty.
    fn from_iter<T: IntoIterator<Item = Ident>>(iter: T) -> Self {
        let mut iter = iter.into_iter();
        let first = iter.next().expect("paths cannot be empty");
        match iter.next() {
            Some(second) => Self(SmallVec::from_iter([first, second].into_iter().chain(iter))),
            None => Self::new_single(first),
        }
    }
}

impl std::ops::Deref for Path {
    type Target = PathSlice;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}

impl std::ops::DerefMut for Path {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.as_mut_slice()
    }
}

impl std::borrow::Borrow<PathSlice> for Path {
    #[inline]
    fn borrow(&self) -> &PathSlice {
        self.as_slice()
    }
}

impl Path {
    /// Creates a new path from a slice of segments.
    ///
    /// # Panics
    ///
    /// Panics if `segments` is empty.
    #[inline]
    pub fn new(segments: &[Ident]) -> Self {
        assert!(!segments.is_empty());
        Self(SmallVec::from_slice(segments))
    }

    /// Creates a new path from a list of segments.
    ///
    /// # Panics
    ///
    /// Panics if `segments` is empty.
    #[inline]
    pub fn from_vec(segments: Vec<Ident>) -> Self {
        assert!(!segments.is_empty());
        Self(SmallVec::from_vec(segments))
    }

    /// Creates a new path from a list of segments.
    ///
    /// # Panics
    ///
    /// Panics if `segments` is empty.
    #[inline]
    pub fn from_ast(ast: AstPath<'_>) -> Self {
        Self::new(ast.segments())
    }

    /// Creates a new path from a single ident.
    #[inline]
    pub fn new_single(ident: Ident) -> Self {
        Self(SmallVec::from_buf_and_len([ident], 1))
    }

    /// Converts the path into a slice.
    #[inline]
    pub fn as_slice(&self) -> &PathSlice {
        unsafe { PathSlice::from_slice_unchecked(&self.0) }
    }

    /// Converts the path into a mutable slice.
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut PathSlice {
        unsafe { PathSlice::from_mut_slice_unchecked(&mut self.0) }
    }
}