indexed_arena/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3#![no_std]
4
5extern crate alloc;
6
7use alloc::vec::Vec;
8use core::{
9    fmt,
10    hash::{Hash, Hasher},
11    iter,
12    marker::PhantomData,
13    num::NonZero,
14    ops::{Index, IndexMut, Range},
15    slice,
16};
17
18mod util;
19
20/// A trait for index types used in arenas.
21///
22/// An [`Id`] represents both the internal index in an arena and a type-level distinction
23/// (for example, when using multiple arenas with the same underlying numeric index type).
24pub trait Id: Copy + Ord {
25    /// The maximum value (as a usize) this id type can represent.
26    const MAX: usize;
27
28    /// Converts a `usize` value to this id type.
29    ///
30    /// The input `idx` (should / is guaranteed to) be less than or equal to `Self::MAX`.
31    ///
32    /// # Panics
33    ///
34    /// If the input `idx` is not less than `Self::MAX`, this function will panic.
35    fn from_usize(idx: usize) -> Self;
36
37    /// Converts this id type into a `usize`.
38    ///
39    /// The returned value (should / is guaranteed to) be less or equal than `Self::MAX`.
40    fn into_usize(self) -> usize;
41}
42
43macro_rules! impl_id_for_nums {
44    ($($ty:ty),*) => {$(
45        impl Id for $ty {
46            const MAX: usize = <$ty>::MAX as usize;
47            #[inline]
48            fn from_usize(idx: usize) -> Self {
49                assert!(idx <= <Self as Id>::MAX);
50                idx as $ty
51            }
52            #[inline]
53            fn into_usize(self) -> usize {
54                self as usize
55            }
56        }
57        impl Id for NonZero<$ty> {
58            const MAX: usize = (<$ty>::MAX - 1) as usize;
59            #[inline]
60            fn from_usize(idx: usize) -> Self {
61                assert!(idx <= <Self as Id>::MAX);
62                unsafe { NonZero::new_unchecked((idx + 1) as $ty) }
63            }
64            #[inline]
65            fn into_usize(self) -> usize {
66                (self.get() - 1) as usize
67            }
68        }
69    )*};
70}
71impl_id_for_nums!(u8, u16, u32, u64, usize);
72
73/// A typed index for referencing elements in an [`Arena`].
74///
75/// The [`Idx<T, I>`] type wraps an underlying id of type `I` and carries a phantom type `T`
76/// to ensure type safety when indexing into an arena.
77///
78/// # Examples
79///
80/// ```
81/// use indexed_arena::{Arena, Idx};
82///
83/// let mut arena: Arena<&str, u32> = Arena::new();
84/// let idx: Idx<&str, u32> = arena.alloc("hello");
85/// assert_eq!(arena[idx], "hello");
86/// ```
87pub struct Idx<T, I: Id> {
88    raw: I,
89    phantom: PhantomData<fn() -> T>,
90}
91
92impl<T, I: Id> Idx<T, I> {
93    /// Consumes the index and returns its underlying raw value.
94    ///
95    /// # Examples
96    ///
97    /// ```
98    /// use indexed_arena::{Arena, Idx};
99    ///
100    /// let mut arena: Arena<i32, u32> = Arena::new();
101    /// let idx = arena.alloc(10);
102    /// let raw = idx.into_raw();
103    /// // raw is a u32 representing the index inside the arena.
104    /// assert_eq!(raw, 0u32);
105    /// ```
106    #[inline]
107    pub const fn into_raw(self) -> I {
108        self.raw
109    }
110}
111
112impl<T, I: Id> Clone for Idx<T, I> {
113    #[inline]
114    fn clone(&self) -> Self {
115        *self
116    }
117}
118impl<T, I: Id> Copy for Idx<T, I> {}
119
120impl<T, I: Id + fmt::Debug> fmt::Debug for Idx<T, I> {
121    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
122        let t_name = util::simple_type_name::<T>();
123        let i_name = util::simple_type_name::<I>();
124        write!(fmt, "Idx::<{}, {}>({:?})", t_name, i_name, self.raw)
125    }
126}
127
128impl<T, I: Id> PartialEq for Idx<T, I> {
129    #[inline]
130    fn eq(&self, other: &Self) -> bool {
131        self.raw == other.raw
132    }
133}
134impl<T, I: Id> Eq for Idx<T, I> {}
135
136impl<T, I: Id> Ord for Idx<T, I> {
137    #[inline]
138    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
139        self.raw.cmp(&other.raw)
140    }
141}
142
143impl<T, I: Id> PartialOrd for Idx<T, I> {
144    #[inline]
145    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
146        Some(self.cmp(other))
147    }
148}
149
150impl<T, I: Id + Hash> Hash for Idx<T, I> {
151    #[inline]
152    fn hash<H: Hasher>(&self, state: &mut H) {
153        self.raw.hash(state)
154    }
155}
156
157/// A span of indices within an `Arena`.
158///
159/// This type represents a contiguous range of allocated indices in an arena.
160///
161/// # Examples
162///
163/// ```
164/// use indexed_arena::{Arena, IdxSpan};
165///
166/// let mut arena: Arena<i32, u32> = Arena::new();
167/// let span: IdxSpan<i32, u32> = arena.alloc_many(1..=4);
168/// assert_eq!(span.len(), 4);
169/// assert!(!span.is_empty());
170/// assert_eq!(&arena[span], &[1, 2, 3, 4]);
171/// ```
172pub struct IdxSpan<T, I: Id> {
173    start: I,
174    end: I,
175    phantom: PhantomData<fn() -> T>,
176}
177
178impl<T, I: Id> IdxSpan<T, I> {
179    /// Creates a new [`IdxSpan`] from the given range of raw indices.
180    #[inline]
181    pub const fn new(range: Range<I>) -> Self {
182        Self { start: range.start, end: range.end, phantom: PhantomData }
183    }
184
185    /// Returns the starting raw index.
186    #[inline]
187    pub const fn start(&self) -> I {
188        self.start
189    }
190
191    /// Returns the ending raw index.
192    #[inline]
193    pub const fn end(&self) -> I {
194        self.end
195    }
196
197    /// Returns the number of indices in the span.
198    #[inline]
199    pub fn len(&self) -> usize {
200        self.end.into_usize() - self.start.into_usize()
201    }
202
203    /// Returns true if the span is empty.
204    #[inline]
205    pub fn is_empty(&self) -> bool {
206        self.start == self.end
207    }
208}
209
210impl<T, I: Id> Clone for IdxSpan<T, I> {
211    #[inline]
212    fn clone(&self) -> Self {
213        *self
214    }
215}
216impl<T, I: Id> Copy for IdxSpan<T, I> {}
217
218impl<T, I: Id + fmt::Debug> fmt::Debug for IdxSpan<T, I> {
219    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
220        let t_name = util::simple_type_name::<T>();
221        let i_name = util::simple_type_name::<I>();
222        write!(fmt, "IdxSpan::<{}, {}>({:?}..{:?})", t_name, i_name, self.start, self.end)
223    }
224}
225
226impl<T, I: Id> PartialEq for IdxSpan<T, I> {
227    #[inline]
228    fn eq(&self, other: &Self) -> bool {
229        self.start == other.start && self.end == other.end
230    }
231}
232impl<T, I: Id> Eq for IdxSpan<T, I> {}
233
234impl<T, I: Id + Hash> Hash for IdxSpan<T, I> {
235    #[inline]
236    fn hash<H: Hasher>(&self, state: &mut H) {
237        self.start.hash(state);
238        self.end.hash(state);
239    }
240}
241
242/// A index-based arena.
243///
244/// [`Arena`] provides a mechanism to allocate objects and refer to them by a
245/// strongly-typed index ([`Idx<T, I>`]). The index not only represents the position
246/// in the underlying vector but also leverages the type system to prevent accidental misuse
247/// across different arenas.
248pub struct Arena<T, I: Id> {
249    data: Vec<T>,
250    phantom: PhantomData<(I, T)>,
251}
252
253impl<T, I: Id> Arena<T, I> {
254    /// Creates a new empty arena.
255    ///
256    /// # Examples
257    ///
258    /// ```
259    /// # use indexed_arena::Arena;
260    /// let arena: Arena<i32, u32> = Arena::new();
261    /// assert!(arena.is_empty());
262    /// ```
263    #[inline]
264    pub const fn new() -> Self {
265        Self { data: Vec::new(), phantom: PhantomData }
266    }
267
268    /// Creates a new arena with the specified capacity.
269    ///
270    /// # Examples
271    ///
272    /// ```
273    /// # use indexed_arena::Arena;
274    /// let arena: Arena<i32, u32> = Arena::with_capacity(10);
275    /// assert!(arena.is_empty());
276    /// assert!(arena.capacity() >= 10);
277    /// ```
278    #[inline]
279    pub fn with_capacity(capacity: usize) -> Self {
280        Self { data: Vec::with_capacity(capacity), phantom: PhantomData }
281    }
282
283    /// Returns the number of elements stored in the arena.
284    ///
285    /// # Examples
286    ///
287    /// ```
288    /// # use indexed_arena::Arena;
289    /// let mut arena = Arena::<_, u32>::new();
290    /// assert_eq!(arena.len(), 0);
291    ///
292    /// arena.alloc("foo");
293    /// assert_eq!(arena.len(), 1);
294    ///
295    /// arena.alloc("bar");
296    /// assert_eq!(arena.len(), 2);
297    ///
298    /// arena.alloc("baz");
299    /// assert_eq!(arena.len(), 3);
300    /// ```
301    #[inline]
302    pub fn len(&self) -> usize {
303        self.data.len()
304    }
305
306    /// Returns the capacity of the arena.
307    ///
308    /// # Examples
309    ///
310    /// ```
311    /// use indexed_arena::Arena;
312    ///
313    /// let arena: Arena<String, u32> = Arena::with_capacity(10);
314    /// assert!(arena.capacity() >= 10);
315    /// ```
316    #[inline]
317    pub fn capacity(&self) -> usize {
318        self.data.capacity()
319    }
320
321    /// Returns `true` if the arena contains no elements.
322    ///
323    /// # Examples
324    ///
325    /// ```
326    /// # use indexed_arena::Arena;
327    /// let mut arena = Arena::<_, u32>::new();
328    /// assert!(arena.is_empty());
329    ///
330    /// arena.alloc(0.9);
331    /// assert!(!arena.is_empty());
332    /// ```
333    #[inline]
334    pub fn is_empty(&self) -> bool {
335        self.data.is_empty()
336    }
337
338    /// Allocates an element in the arena and returns its index.
339    ///
340    /// # Panics
341    ///
342    /// Panics if the arena is full (i.e. if the number of elements exceeds `I::MAX`).
343    /// If you hnadle this case, use [`Arena::try_alloc`] instead.
344    ///
345    /// # Examples
346    ///
347    /// ```
348    /// use indexed_arena::{Arena, Idx};
349    ///
350    /// let mut arena: Arena<&str, u32> = Arena::new();
351    /// let idx: Idx<&str, u32> = arena.alloc("hello");
352    /// assert_eq!(arena[idx], "hello");
353    /// ```
354    #[inline]
355    pub fn alloc(&mut self, value: T) -> Idx<T, I> {
356        self.try_alloc(value).expect("arena is full")
357    }
358
359    /// Fallible version of [`Arena::alloc`].
360    ///
361    /// This method returns `None` if the arena is full.
362    #[inline]
363    pub fn try_alloc(&mut self, value: T) -> Option<Idx<T, I>> {
364        if self.data.len() >= I::MAX {
365            None
366        } else {
367            let id = I::from_usize(self.data.len());
368            self.data.push(value);
369            Some(Idx { raw: id, phantom: PhantomData })
370        }
371    }
372
373    /// Allocates multiple elements in the arena and returns the index span covering them.
374    ///
375    /// # Panics
376    ///
377    /// Panics if the arena cannot allocate all elements (i.e. if the arena becomes full).
378    ///
379    /// # Examples
380    ///
381    /// ```
382    /// use indexed_arena::{Arena, IdxSpan};
383    ///
384    /// let mut arena: Arena<i32, u32> = Arena::new();
385    /// let span: IdxSpan<i32, u32> = arena.alloc_many([10, 20, 30]);
386    /// assert_eq!(&arena[span], &[10, 20, 30]);
387    /// ```
388    #[inline]
389    pub fn alloc_many(&mut self, values: impl IntoIterator<Item = T>) -> IdxSpan<T, I> {
390        self.try_alloc_many(values).expect("arena is full")
391    }
392
393    /// Fallible version of [`Arena::alloc_many`].
394    ///
395    /// This method returns `None` if the arena becomes full.
396    #[inline]
397    pub fn try_alloc_many(&mut self, values: impl IntoIterator<Item = T>) -> Option<IdxSpan<T, I>> {
398        let start = I::from_usize(self.data.len());
399        let mut len = self.data.len();
400        for value in values {
401            if len >= I::MAX {
402                return None;
403            }
404            self.data.push(value);
405            len += 1;
406        }
407        let end = I::from_usize(len);
408        Some(IdxSpan::new(start..end))
409    }
410
411    /// Returns a iterator over the elements and their indices in the arena.
412    ///
413    /// # Examples
414    ///
415    /// ```
416    /// # use indexed_arena::Arena;
417    /// let mut arena = Arena::<_, u32>::new();
418    ///
419    /// let idx1 = arena.alloc(20);
420    /// let idx2 = arena.alloc(40);
421    /// let idx3 = arena.alloc(60);
422    ///
423    /// let mut iter = arena.iter();
424    /// assert_eq!(iter.next(), Some((idx1, &20)));
425    /// assert_eq!(iter.next(), Some((idx2, &40)));
426    /// assert_eq!(iter.next(), Some((idx3, &60)));
427    /// assert_eq!(iter.next(), None);
428    /// ```
429    #[inline]
430    pub fn iter(&self) -> Iter<'_, T, I> {
431        Iter { iter: self.data.iter().enumerate(), phantom: PhantomData }
432    }
433
434    /// Returns a mutable iterator over the elements and their indices in the arena.
435    ///
436    /// # Examples
437    ///
438    /// ```
439    /// # use indexed_arena::Arena;
440    /// let mut arena = Arena::<_, u32>::new();
441    /// let idx1 = arena.alloc(20);
442    ///
443    /// assert_eq!(arena[idx1], 20);
444    ///
445    /// let mut iterator = arena.iter_mut();
446    /// *iterator.next().unwrap().1 = 10;
447    /// drop(iterator);
448    ///
449    /// assert_eq!(arena[idx1], 10);
450    /// ```
451    #[inline]
452    pub fn iter_mut(&mut self) -> IterMut<'_, T, I> {
453        IterMut { iter: self.data.iter_mut().enumerate(), phantom: PhantomData }
454    }
455
456    /// Returns an iterator over the values in the arena.
457    ///
458    /// # Examples
459    ///
460    /// ```
461    /// # use indexed_arena::Arena;
462    /// let mut arena = Arena::<_, u32>::new();
463    /// arena.alloc_many([1, 2, 3]);
464    ///
465    /// let mut iter = arena.values();
466    /// assert_eq!(iter.next(), Some(&1));
467    /// assert_eq!(iter.next(), Some(&2));
468    /// assert_eq!(iter.next(), Some(&3));
469    /// assert_eq!(iter.next(), None);
470    /// ```
471    #[inline]
472    pub fn values(&self) -> Values<'_, T, I> {
473        Values { iter: self.data.iter(), phantom: PhantomData }
474    }
475
476    /// Returns a mutable iterator over the values in the arena.
477    ///
478    /// # Examples
479    ///
480    /// ```
481    /// # use indexed_arena::Arena;
482    /// let mut arena = Arena::<_, u32>::new();
483    /// arena.alloc_many([1, 2, 3]);
484    ///
485    /// let mut iter = arena.values_mut();
486    /// *iter.next().unwrap() = 10;
487    /// *iter.next().unwrap() = 20;
488    /// *iter.next().unwrap() = 30;
489    /// assert_eq!(iter.next(), None);
490    ///
491    /// let mut values = arena.values().cloned().collect::<Vec<_>>();
492    /// assert_eq!(values, vec![10, 20, 30]);
493    /// ```
494    #[inline]
495    pub fn values_mut(&mut self) -> ValuesMut<'_, T, I> {
496        ValuesMut { iter: self.data.iter_mut(), phantom: PhantomData }
497    }
498
499    /// Shrinks the capacity of the arena to fit the number of elements.
500    ///
501    /// # Examples
502    ///
503    /// ```
504    /// # use indexed_arena::Arena;
505    /// let mut arena = Arena::<_, u32>::with_capacity(10);
506    /// arena.alloc_many(&[1, 2, 3]);
507    /// assert!(arena.capacity() >= 10);
508    ///
509    /// arena.shrink_to_fit();
510    /// assert!(arena.capacity() >= 3);
511    /// ```
512    #[inline]
513    pub fn shrink_to_fit(&mut self) {
514        self.data.shrink_to_fit();
515    }
516}
517
518impl<T, I: Id> Default for Arena<T, I> {
519    #[inline]
520    fn default() -> Self {
521        Self::new()
522    }
523}
524
525impl<T, I: Id> Index<Idx<T, I>> for Arena<T, I> {
526    type Output = T;
527
528    #[inline]
529    fn index(&self, idx: Idx<T, I>) -> &Self::Output {
530        &self.data[idx.raw.into_usize()]
531    }
532}
533
534impl<T, I: Id> Index<IdxSpan<T, I>> for Arena<T, I> {
535    type Output = [T];
536
537    #[inline]
538    fn index(&self, span: IdxSpan<T, I>) -> &Self::Output {
539        &self.data[span.start.into_usize()..span.end.into_usize()]
540    }
541}
542
543impl<T, I: Id> IndexMut<Idx<T, I>> for Arena<T, I> {
544    #[inline]
545    fn index_mut(&mut self, index: Idx<T, I>) -> &mut Self::Output {
546        &mut self.data[index.raw.into_usize()]
547    }
548}
549
550impl<T, I: Id> IndexMut<IdxSpan<T, I>> for Arena<T, I> {
551    #[inline]
552    fn index_mut(&mut self, span: IdxSpan<T, I>) -> &mut Self::Output {
553        &mut self.data[span.start.into_usize()..span.end.into_usize()]
554    }
555}
556
557impl<T: Clone, I: Id> Clone for Arena<T, I> {
558    #[inline]
559    fn clone(&self) -> Self {
560        Self { data: self.data.clone(), phantom: PhantomData }
561    }
562}
563
564impl<T: fmt::Debug, I: Id> fmt::Debug for Arena<T, I> {
565    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
566        f.debug_struct("Arena").field("len", &self.len()).field("data", &self.data).finish()
567    }
568}
569
570impl<T: PartialEq, I: Id> PartialEq for Arena<T, I> {
571    #[inline]
572    fn eq(&self, other: &Self) -> bool {
573        self.data == other.data
574    }
575}
576
577impl<T: Eq, I: Id> Eq for Arena<T, I> {}
578
579impl<T: Hash, I: Id> Hash for Arena<T, I> {
580    #[inline]
581    fn hash<H: Hasher>(&self, state: &mut H) {
582        self.data.hash(state)
583    }
584}
585
586impl<'a, T, I: Id> IntoIterator for &'a Arena<T, I> {
587    type Item = (Idx<T, I>, &'a T);
588    type IntoIter = Iter<'a, T, I>;
589
590    #[inline]
591    fn into_iter(self) -> Self::IntoIter {
592        self.iter()
593    }
594}
595
596impl<T, I: Id> IntoIterator for Arena<T, I> {
597    type Item = (Idx<T, I>, T);
598    type IntoIter = IntoIter<T, I>;
599
600    #[inline]
601    fn into_iter(self) -> Self::IntoIter {
602        IntoIter { iter: self.data.into_iter().enumerate(), phantom: PhantomData }
603    }
604}
605
606macro_rules! iter_iterator_impls {
607    ($ty:ty, type Item = $item_ty:ty;) => {
608        impl<'a, T, I: Id> Iterator for $ty {
609            type Item = $item_ty;
610            #[inline]
611            fn next(&mut self) -> Option<Self::Item> {
612                let (id, value) = self.iter.next().map(|(i, v)| (I::from_usize(i), v))?;
613                Some((Idx { raw: id, phantom: PhantomData }, value))
614            }
615            #[inline]
616            fn size_hint(&self) -> (usize, Option<usize>) {
617                self.iter.size_hint()
618            }
619            #[inline]
620            fn count(self) -> usize {
621                self.iter.count()
622            }
623            #[inline]
624            fn last(self) -> Option<Self::Item> {
625                self.iter
626                    .last()
627                    .map(|(i, v)| (Idx { raw: I::from_usize(i), phantom: PhantomData }, v))
628            }
629            #[inline]
630            fn nth(&mut self, n: usize) -> Option<Self::Item> {
631                let (id, value) = self.iter.nth(n).map(|(i, v)| (I::from_usize(i), v))?;
632                Some((Idx { raw: id, phantom: PhantomData }, value))
633            }
634        }
635        impl<'a, T, I: Id> DoubleEndedIterator for $ty {
636            #[inline]
637            fn next_back(&mut self) -> Option<Self::Item> {
638                let (id, value) = self.iter.next_back().map(|(i, v)| (I::from_usize(i), v))?;
639                Some((Idx { raw: id, phantom: PhantomData }, value))
640            }
641        }
642        impl<'a, T, I: Id> ExactSizeIterator for $ty {
643            #[inline]
644            fn len(&self) -> usize {
645                self.iter.len()
646            }
647        }
648        impl<'a, T, I: Id> iter::FusedIterator for $ty {}
649    };
650}
651
652pub struct Iter<'a, T, I: Id> {
653    iter: iter::Enumerate<slice::Iter<'a, T>>,
654    phantom: PhantomData<I>,
655}
656
657iter_iterator_impls! {
658    Iter<'a, T, I>,
659    type Item = (Idx<T, I>, &'a T);
660}
661
662impl<T, I: Id> Clone for Iter<'_, T, I> {
663    #[inline]
664    fn clone(&self) -> Self {
665        Self { iter: self.iter.clone(), phantom: PhantomData }
666    }
667}
668
669pub struct IterMut<'a, T, I: Id> {
670    iter: iter::Enumerate<slice::IterMut<'a, T>>,
671    phantom: PhantomData<I>,
672}
673
674iter_iterator_impls! {
675    IterMut<'a, T, I>,
676    type Item = (Idx<T, I>, &'a mut T);
677}
678
679pub struct IntoIter<T, I: Id> {
680    iter: iter::Enumerate<alloc::vec::IntoIter<T>>,
681    phantom: PhantomData<I>,
682}
683
684iter_iterator_impls! {
685    IntoIter<T, I>,
686    type Item = (Idx<T, I>, T);
687}
688
689impl<T: Clone, I: Id> Clone for IntoIter<T, I> {
690    #[inline]
691    fn clone(&self) -> Self {
692        Self { iter: self.iter.clone(), phantom: PhantomData }
693    }
694}
695
696macro_rules! values_iterator_impls {
697    ($ty:ty, type Item = $item_ty:ty;) => {
698        impl<'a, T, I: Id> Iterator for $ty {
699            type Item = $item_ty;
700            #[inline]
701            fn next(&mut self) -> Option<Self::Item> {
702                self.iter.next()
703            }
704            #[inline]
705            fn size_hint(&self) -> (usize, Option<usize>) {
706                self.iter.size_hint()
707            }
708            #[inline]
709            fn count(self) -> usize {
710                self.iter.count()
711            }
712            #[inline]
713            fn last(self) -> Option<Self::Item> {
714                self.iter.last()
715            }
716            #[inline]
717            fn nth(&mut self, n: usize) -> Option<Self::Item> {
718                self.iter.nth(n)
719            }
720        }
721        impl<'a, T, I: Id> DoubleEndedIterator for $ty {
722            #[inline]
723            fn next_back(&mut self) -> Option<Self::Item> {
724                self.iter.next_back()
725            }
726        }
727        impl<'a, T, I: Id> ExactSizeIterator for $ty {
728            #[inline]
729            fn len(&self) -> usize {
730                self.iter.len()
731            }
732        }
733        impl<'a, T, I: Id> iter::FusedIterator for $ty {}
734    };
735}
736
737pub struct Values<'a, T, I: Id> {
738    iter: slice::Iter<'a, T>,
739    phantom: PhantomData<I>,
740}
741
742values_iterator_impls! {
743    Values<'a, T, I>,
744    type Item = &'a T;
745}
746
747impl<T, I: Id> Clone for Values<'_, T, I> {
748    #[inline]
749    fn clone(&self) -> Self {
750        Self { iter: self.iter.clone(), phantom: PhantomData }
751    }
752}
753
754pub struct ValuesMut<'a, T, I: Id> {
755    iter: slice::IterMut<'a, T>,
756    phantom: PhantomData<I>,
757}
758
759values_iterator_impls! {
760    ValuesMut<'a, T, I>,
761    type Item = &'a mut T;
762}