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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use artichoke_core::intern::Intern;
use core::convert::TryInto;
use core::iter::FusedIterator;
use core::ops::Range;

use crate::Symbol;

/// Extension trait to return an iterator that returns all symbol identifiers
/// stored in an [interner] as [`Symbol`]s.
///
/// The returned iterator yields [`Symbol`] as its item type.
///
/// Implementors of this trait must issue symbol identifiers as an [arithmetic
/// progression] with a common difference of 1. The sequence of symbol
/// identifiers must be representable by a [`Range<u32>`].
///
/// This trait is automatically implemented for all types that implement
/// [`Intern`] from [`artichoke_core`].
///
/// # Examples
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
/// use alloc::boxed::Box;
/// use artichoke_core::intern::Intern;
/// use spinoso_symbol::{InternerAllSymbols, Symbol};
///
/// #[derive(Default)]
/// struct Interner(u32);
///
/// impl Intern for Interner {
///     type Symbol = u32;
///     type Error = &'static str;
///     const SYMBOL_RANGE_START: u32 = 1;
///
///     fn intern_bytes<T>(&mut self, symbol: T) -> Result<Self::Symbol, Self::Error>
///     where
///         T: Into<Cow<'static, [u8]>>
///     {
///         let boxed = Box::<[u8]>::from(symbol.into());
///         Box::leak(boxed);
///         self.0 += 1;
///         let sym = self.0;
///         Ok(sym)
///     }
///
///     fn check_interned_bytes(&self, symbol: &[u8]) -> Result<Option<Self::Symbol>, Self::Error> {
///         Err("not implemented")
///     }
///
///     fn lookup_symbol(&self, symbol: Self::Symbol) -> Result<Option<&[u8]>, Self::Error> {
///         Err("not implemented")
///     }
///
///     fn symbol_count(&self) -> usize {
///         self.0 as usize
///     }
/// }
///
/// let mut interner = Interner::default();
/// let mut all_symbols = interner.all_symbols();
/// assert_eq!(all_symbols.count(), 0);
///
/// interner.intern_bytes(&b"Spinoso"[..]);
/// interner.intern_bytes(&b"Artichoke"[..]);
///
/// let mut all_symbols = interner.all_symbols();
/// assert_eq!(all_symbols.next(), Some(Symbol::new(1)));
/// assert_eq!(all_symbols.next(), Some(Symbol::new(2)));
/// assert_eq!(all_symbols.next(), None);
/// ```
///
/// [interner]: Intern
/// [arithmetic progression]: https://en.wikipedia.org/wiki/Arithmetic_progression
/// [`Range<u32>`]: core::ops::Range
#[allow(clippy::module_name_repetitions)]
#[cfg_attr(docsrs, doc(cfg(feature = "artichoke")))]
pub trait InternerAllSymbols: Intern {
    /// Returns an iterator that returns all symbol identifiers stored in an
    /// [interner] as [`Symbol`]s.
    ///
    /// The returned iterator yields [`Symbol`] as its item type.
    ///
    /// This function requires that the interner issues symbol identifiers as an
    /// [arithmetic progression] with a common difference of 1. The sequence of
    /// symbol identifiers must be representable by a [`Range<u32>`].
    ///
    /// [`AllSymbols`] supports yielding up to `u32::MAX - 1` `Symbol`s.
    ///
    /// # Examples
    ///
    /// See trait-level documentation for examples.
    ///
    /// [interner]: Intern
    /// [arithmetic progression]: https://en.wikipedia.org/wiki/Arithmetic_progression
    /// [`Range<u32>`]: core::ops::Range
    fn all_symbols(&self) -> AllSymbols;
}

impl<T, U> InternerAllSymbols for T
where
    T: Intern<Symbol = U>,
    U: Copy + Into<u32>,
{
    #[inline]
    #[cfg_attr(docsrs, doc(cfg(feature = "artichoke")))]
    fn all_symbols(&self) -> AllSymbols {
        self.into()
    }
}

impl<T, U> From<&T> for AllSymbols
where
    T: Intern<Symbol = U>,
    U: Copy + Into<u32>,
{
    /// Construct a [`AllSymbols`] iterator from an interner.
    #[inline]
    fn from(interner: &T) -> Self {
        let min = T::SYMBOL_RANGE_START.into();
        let max_idx = interner.symbol_count().try_into().unwrap_or(u32::MAX);
        let max = min.saturating_add(max_idx);
        AllSymbols(min..max)
    }
}

/// An iterator that returns all of the Symbols in an [interner].
///
/// This iterator yields [`Symbol`] as its item type.
///
/// This iterator supports yielding up to `u32::MAX - 1` `Symbol`s.
///
/// This struct is created by the [`all_symbols`] method in the
/// [`InternerAllSymbols`] trait.  See its documentation for more.
///
/// [interner]: Intern
/// [`all_symbols`]: InternerAllSymbols::all_symbols
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[must_use = "this `AllSymbols` is an `Iterator`, which should be consumed if constructed"]
#[cfg_attr(docsrs, doc(cfg(feature = "artichoke")))]
pub struct AllSymbols(Range<u32>);

impl Iterator for AllSymbols {
    type Item = Symbol;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(Symbol::from)
    }

    #[inline]
    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.0.nth(n).map(Symbol::from)
    }

    #[inline]
    fn count(self) -> usize {
        // Inline implementation of `Step::steps_between` since
        // `<Range as Iterator>::count` is not specialized to use it for integer
        // ranges.
        if self.0.start <= self.0.end {
            (self.0.end - self.0.start) as usize
        } else {
            0
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }

    #[inline]
    fn last(self) -> Option<Self::Item> {
        self.0.last().map(Symbol::from)
    }

    #[inline]
    fn min(mut self) -> Option<Self::Item> {
        self.0.next().map(Symbol::from)
    }

    #[inline]
    fn max(self) -> Option<Self::Item> {
        self.0.max().map(Symbol::from)
    }
}

impl DoubleEndedIterator for AllSymbols {
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0.next_back().map(Symbol::from)
    }

    #[inline]
    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
        self.0.nth_back(n).map(Symbol::from)
    }
}

impl ExactSizeIterator for AllSymbols {}

impl FusedIterator for AllSymbols {}

#[cfg(test)]
mod tests {
    use artichoke_core::intern::Intern;
    use std::borrow::Cow;

    use super::InternerAllSymbols;
    use crate::Symbol;

    #[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
    struct Interner(u32);

    impl Intern for Interner {
        type Symbol = u32;
        type Error = &'static str;
        const SYMBOL_RANGE_START: Self::Symbol = 0;

        fn intern_bytes<T>(&mut self, symbol: T) -> Result<Self::Symbol, Self::Error>
        where
            T: Into<Cow<'static, [u8]>>,
        {
            drop(symbol.into());
            Err("not implemented")
        }

        fn check_interned_bytes(&self, symbol: &[u8]) -> Result<Option<Self::Symbol>, Self::Error> {
            let _ = symbol;
            Err("not implemented")
        }

        fn lookup_symbol(&self, symbol: Self::Symbol) -> Result<Option<&[u8]>, Self::Error> {
            let _ = symbol;
            Err("not implemented")
        }

        fn symbol_count(&self) -> usize {
            self.0 as usize
        }
    }

    #[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
    struct OffByOneInterner(u32);

    impl Intern for OffByOneInterner {
        type Symbol = u32;
        type Error = &'static str;
        const SYMBOL_RANGE_START: Self::Symbol = 1;

        fn intern_bytes<T>(&mut self, symbol: T) -> Result<Self::Symbol, Self::Error>
        where
            T: Into<Cow<'static, [u8]>>,
        {
            drop(symbol.into());
            Err("not implemented")
        }

        fn check_interned_bytes(&self, symbol: &[u8]) -> Result<Option<Self::Symbol>, Self::Error> {
            let _ = symbol;
            Err("not implemented")
        }

        fn lookup_symbol(&self, symbol: Self::Symbol) -> Result<Option<&[u8]>, Self::Error> {
            let _ = symbol;
            Err("not implemented")
        }

        fn symbol_count(&self) -> usize {
            self.0 as usize
        }
    }

    #[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
    struct NonZeroInterner(u32);

    impl Intern for NonZeroInterner {
        type Symbol = u32;
        type Error = &'static str;
        const SYMBOL_RANGE_START: Self::Symbol = u32::MAX - 16;

        fn intern_bytes<T>(&mut self, symbol: T) -> Result<Self::Symbol, Self::Error>
        where
            T: Into<Cow<'static, [u8]>>,
        {
            drop(symbol.into());
            Err("not implemented")
        }

        fn check_interned_bytes(&self, symbol: &[u8]) -> Result<Option<Self::Symbol>, Self::Error> {
            let _ = symbol;
            Err("not implemented")
        }

        fn lookup_symbol(&self, symbol: Self::Symbol) -> Result<Option<&[u8]>, Self::Error> {
            let _ = symbol;
            Err("not implemented")
        }

        fn symbol_count(&self) -> usize {
            self.0 as usize
        }
    }

    #[test]
    fn zero_offset_count() {
        let interner = Interner::default();
        let all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.count(), 0);

        let interner = Interner(100);
        let all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.count(), 100);

        let interner = Interner(u32::MAX);
        let all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.count(), u32::MAX as usize);
    }

    #[test]
    fn zero_offset_yielded() {
        let interner = Interner::default();
        let mut all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.next(), None);

        let interner = Interner(5);
        let mut all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.next(), Some(Symbol::new(0)));
        assert_eq!(all_symbols.next(), Some(Symbol::new(1)));
        assert_eq!(all_symbols.next(), Some(Symbol::new(2)));
        assert_eq!(all_symbols.next(), Some(Symbol::new(3)));
        assert_eq!(all_symbols.next(), Some(Symbol::new(4)));
        assert_eq!(all_symbols.next(), None);

        let interner = Interner(u32::MAX);
        let mut all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.next(), Some(Symbol::new(0)));
        assert_eq!(all_symbols.next_back(), Some(Symbol::new(u32::MAX - 1)));
    }

    #[test]
    fn one_offset_count() {
        let interner = OffByOneInterner::default();
        let all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.count(), 0);

        let interner = OffByOneInterner(100);
        let all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.count(), 100);

        let interner = OffByOneInterner(u32::MAX);
        let all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.count(), (u32::MAX - 1) as usize);
    }

    #[test]
    fn one_offset_yielded() {
        let interner = OffByOneInterner::default();
        let mut all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.next(), None);

        let interner = OffByOneInterner(5);
        let mut all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.next(), Some(Symbol::new(1)));
        assert_eq!(all_symbols.next(), Some(Symbol::new(2)));
        assert_eq!(all_symbols.next(), Some(Symbol::new(3)));
        assert_eq!(all_symbols.next(), Some(Symbol::new(4)));
        assert_eq!(all_symbols.next(), Some(Symbol::new(5)));
        assert_eq!(all_symbols.next(), None);

        let interner = OffByOneInterner(u32::MAX);
        let mut all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.next(), Some(Symbol::new(1)));
        assert_eq!(all_symbols.next_back(), Some(Symbol::new(u32::MAX - 1)));
    }

    #[test]
    fn high_offset_count() {
        let interner = NonZeroInterner::default();
        let all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.count(), 0);

        let interner = NonZeroInterner(100);
        let all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.count(), 16);

        let interner = NonZeroInterner(u32::MAX);
        let all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.count(), 16);
    }

    #[test]
    fn high_offset_yielded() {
        let interner = NonZeroInterner::default();
        let mut all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.next(), None);

        let interner = NonZeroInterner(5);
        let mut all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.next(), Some(Symbol::new(u32::MAX - 16)));
        assert_eq!(all_symbols.next(), Some(Symbol::new(u32::MAX - 15)));
        assert_eq!(all_symbols.next(), Some(Symbol::new(u32::MAX - 14)));
        assert_eq!(all_symbols.next(), Some(Symbol::new(u32::MAX - 13)));
        assert_eq!(all_symbols.next(), Some(Symbol::new(u32::MAX - 12)));
        assert_eq!(all_symbols.next(), None);

        let interner = NonZeroInterner(u32::MAX);
        let mut all_symbols = interner.all_symbols();
        assert_eq!(all_symbols.next(), Some(Symbol::new(u32::MAX - 16)));
        assert_eq!(all_symbols.next_back(), Some(Symbol::new(u32::MAX - 1)));
    }
}