]> git.proxmox.com Git - rustc.git/blob - src/librustc_data_structures/indexed_vec.rs
New upstream version 1.35.0+dfsg1
[rustc.git] / src / librustc_data_structures / indexed_vec.rs
1 use std::fmt::Debug;
2 use std::iter::{self, FromIterator};
3 use std::slice;
4 use std::marker::PhantomData;
5 use std::ops::{Index, IndexMut, Range, RangeBounds};
6 use std::fmt;
7 use std::hash::Hash;
8 use std::vec;
9 use std::u32;
10
11 use rustc_serialize as serialize;
12
13 /// Represents some newtyped `usize` wrapper.
14 ///
15 /// Purpose: avoid mixing indexes for different bitvector domains.
16 pub trait Idx: Copy + 'static + Ord + Debug + Hash {
17 fn new(idx: usize) -> Self;
18
19 fn index(self) -> usize;
20
21 fn increment_by(&mut self, amount: usize) {
22 let v = self.index() + amount;
23 *self = Self::new(v);
24 }
25 }
26
27 impl Idx for usize {
28 #[inline]
29 fn new(idx: usize) -> Self { idx }
30 #[inline]
31 fn index(self) -> usize { self }
32 }
33
34 impl Idx for u32 {
35 #[inline]
36 fn new(idx: usize) -> Self { assert!(idx <= u32::MAX as usize); idx as u32 }
37 #[inline]
38 fn index(self) -> usize { self as usize }
39 }
40
41 /// Creates a struct type `S` that can be used as an index with
42 /// `IndexVec` and so on.
43 ///
44 /// There are two ways of interacting with these indices:
45 ///
46 /// - The `From` impls are the preferred way. So you can do
47 /// `S::from(v)` with a `usize` or `u32`. And you can convert back
48 /// to an integer with `u32::from(s)`.
49 ///
50 /// - Alternatively, you can use the methods `S::new(v)` and `s.index()`
51 /// to create/return a value.
52 ///
53 /// Internally, the index uses a u32, so the index must not exceed
54 /// `u32::MAX`. You can also customize things like the `Debug` impl,
55 /// what traits are derived, and so forth via the macro.
56 #[macro_export]
57 macro_rules! newtype_index {
58 // ---- public rules ----
59
60 // Use default constants
61 ($(#[$attrs:meta])* $v:vis struct $name:ident { .. }) => (
62 newtype_index!(
63 // Leave out derives marker so we can use its absence to ensure it comes first
64 @attrs [$(#[$attrs])*]
65 @type [$name]
66 // shave off 256 indices at the end to allow space for packing these indices into enums
67 @max [0xFFFF_FF00]
68 @vis [$v]
69 @debug_format ["{}"]);
70 );
71
72 // Define any constants
73 ($(#[$attrs:meta])* $v:vis struct $name:ident { $($tokens:tt)+ }) => (
74 newtype_index!(
75 // Leave out derives marker so we can use its absence to ensure it comes first
76 @attrs [$(#[$attrs])*]
77 @type [$name]
78 // shave off 256 indices at the end to allow space for packing these indices into enums
79 @max [0xFFFF_FF00]
80 @vis [$v]
81 @debug_format ["{}"]
82 $($tokens)+);
83 );
84
85 // ---- private rules ----
86
87 // Base case, user-defined constants (if any) have already been defined
88 (@derives [$($derives:ident,)*]
89 @attrs [$(#[$attrs:meta])*]
90 @type [$type:ident]
91 @max [$max:expr]
92 @vis [$v:vis]
93 @debug_format [$debug_format:tt]) => (
94 $(#[$attrs])*
95 #[derive(Copy, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
96 #[rustc_layout_scalar_valid_range_end($max)]
97 $v struct $type {
98 private: u32
99 }
100
101 impl Clone for $type {
102 fn clone(&self) -> Self {
103 *self
104 }
105 }
106
107 impl $type {
108 $v const MAX_AS_U32: u32 = $max;
109
110 $v const MAX: $type = $type::from_u32_const($max);
111
112 #[inline]
113 $v fn from_usize(value: usize) -> Self {
114 assert!(value <= ($max as usize));
115 unsafe {
116 $type::from_u32_unchecked(value as u32)
117 }
118 }
119
120 #[inline]
121 $v fn from_u32(value: u32) -> Self {
122 assert!(value <= $max);
123 unsafe {
124 $type::from_u32_unchecked(value)
125 }
126 }
127
128 /// Hacky variant of `from_u32` for use in constants.
129 /// This version checks the "max" constraint by using an
130 /// invalid array dereference.
131 #[inline]
132 $v const fn from_u32_const(value: u32) -> Self {
133 // This will fail at const eval time unless `value <=
134 // max` is true (in which case we get the index 0).
135 // It will also fail at runtime, of course, but in a
136 // kind of wacky way.
137 let _ = ["out of range value used"][
138 !(value <= $max) as usize
139 ];
140
141 unsafe {
142 $type { private: value }
143 }
144 }
145
146 #[inline]
147 $v const unsafe fn from_u32_unchecked(value: u32) -> Self {
148 unsafe { $type { private: value } }
149 }
150
151 /// Extracts the value of this index as an integer.
152 #[inline]
153 $v fn index(self) -> usize {
154 self.as_usize()
155 }
156
157 /// Extracts the value of this index as a `u32`.
158 #[inline]
159 $v fn as_u32(self) -> u32 {
160 self.private
161 }
162
163 /// Extracts the value of this index as a `usize`.
164 #[inline]
165 $v fn as_usize(self) -> usize {
166 self.as_u32() as usize
167 }
168 }
169
170 impl Idx for $type {
171 #[inline]
172 fn new(value: usize) -> Self {
173 Self::from(value)
174 }
175
176 #[inline]
177 fn index(self) -> usize {
178 usize::from(self)
179 }
180 }
181
182 impl ::std::iter::Step for $type {
183 #[inline]
184 fn steps_between(start: &Self, end: &Self) -> Option<usize> {
185 <usize as ::std::iter::Step>::steps_between(
186 &Idx::index(*start),
187 &Idx::index(*end),
188 )
189 }
190
191 #[inline]
192 fn replace_one(&mut self) -> Self {
193 ::std::mem::replace(self, Self::new(1))
194 }
195
196 #[inline]
197 fn replace_zero(&mut self) -> Self {
198 ::std::mem::replace(self, Self::new(0))
199 }
200
201 #[inline]
202 fn add_one(&self) -> Self {
203 Self::new(Idx::index(*self) + 1)
204 }
205
206 #[inline]
207 fn sub_one(&self) -> Self {
208 Self::new(Idx::index(*self) - 1)
209 }
210
211 #[inline]
212 fn add_usize(&self, u: usize) -> Option<Self> {
213 Idx::index(*self).checked_add(u).map(Self::new)
214 }
215 }
216
217 impl From<$type> for u32 {
218 #[inline]
219 fn from(v: $type) -> u32 {
220 v.as_u32()
221 }
222 }
223
224 impl From<$type> for usize {
225 #[inline]
226 fn from(v: $type) -> usize {
227 v.as_usize()
228 }
229 }
230
231 impl From<usize> for $type {
232 #[inline]
233 fn from(value: usize) -> Self {
234 $type::from_usize(value)
235 }
236 }
237
238 impl From<u32> for $type {
239 #[inline]
240 fn from(value: u32) -> Self {
241 $type::from_u32(value)
242 }
243 }
244
245 newtype_index!(
246 @handle_debug
247 @derives [$($derives,)*]
248 @type [$type]
249 @debug_format [$debug_format]);
250 );
251
252 // base case for handle_debug where format is custom. No Debug implementation is emitted.
253 (@handle_debug
254 @derives [$($_derives:ident,)*]
255 @type [$type:ident]
256 @debug_format [custom]) => ();
257
258 // base case for handle_debug, no debug overrides found, so use default
259 (@handle_debug
260 @derives []
261 @type [$type:ident]
262 @debug_format [$debug_format:tt]) => (
263 impl ::std::fmt::Debug for $type {
264 fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
265 write!(fmt, $debug_format, self.as_u32())
266 }
267 }
268 );
269
270 // Debug is requested for derive, don't generate any Debug implementation.
271 (@handle_debug
272 @derives [Debug, $($derives:ident,)*]
273 @type [$type:ident]
274 @debug_format [$debug_format:tt]) => ();
275
276 // It's not Debug, so just pop it off the front of the derives stack and check the rest.
277 (@handle_debug
278 @derives [$_derive:ident, $($derives:ident,)*]
279 @type [$type:ident]
280 @debug_format [$debug_format:tt]) => (
281 newtype_index!(
282 @handle_debug
283 @derives [$($derives,)*]
284 @type [$type]
285 @debug_format [$debug_format]);
286 );
287
288 // Append comma to end of derives list if it's missing
289 (@attrs [$(#[$attrs:meta])*]
290 @type [$type:ident]
291 @max [$max:expr]
292 @vis [$v:vis]
293 @debug_format [$debug_format:tt]
294 derive [$($derives:ident),*]
295 $($tokens:tt)*) => (
296 newtype_index!(
297 @attrs [$(#[$attrs])*]
298 @type [$type]
299 @max [$max]
300 @vis [$v]
301 @debug_format [$debug_format]
302 derive [$($derives,)*]
303 $($tokens)*);
304 );
305
306 // By not including the @derives marker in this list nor in the default args, we can force it
307 // to come first if it exists. When encodable is custom, just use the derives list as-is.
308 (@attrs [$(#[$attrs:meta])*]
309 @type [$type:ident]
310 @max [$max:expr]
311 @vis [$v:vis]
312 @debug_format [$debug_format:tt]
313 derive [$($derives:ident,)+]
314 ENCODABLE = custom
315 $($tokens:tt)*) => (
316 newtype_index!(
317 @attrs [$(#[$attrs])*]
318 @derives [$($derives,)+]
319 @type [$type]
320 @max [$max]
321 @vis [$v]
322 @debug_format [$debug_format]
323 $($tokens)*);
324 );
325
326 // By not including the @derives marker in this list nor in the default args, we can force it
327 // to come first if it exists. When encodable isn't custom, add serialization traits by default.
328 (@attrs [$(#[$attrs:meta])*]
329 @type [$type:ident]
330 @max [$max:expr]
331 @vis [$v:vis]
332 @debug_format [$debug_format:tt]
333 derive [$($derives:ident,)+]
334 $($tokens:tt)*) => (
335 newtype_index!(
336 @derives [$($derives,)+ RustcEncodable,]
337 @attrs [$(#[$attrs])*]
338 @type [$type]
339 @max [$max]
340 @vis [$v]
341 @debug_format [$debug_format]
342 $($tokens)*);
343 newtype_index!(@decodable $type);
344 );
345
346 // The case where no derives are added, but encodable is overridden. Don't
347 // derive serialization traits
348 (@attrs [$(#[$attrs:meta])*]
349 @type [$type:ident]
350 @max [$max:expr]
351 @vis [$v:vis]
352 @debug_format [$debug_format:tt]
353 ENCODABLE = custom
354 $($tokens:tt)*) => (
355 newtype_index!(
356 @derives []
357 @attrs [$(#[$attrs])*]
358 @type [$type]
359 @max [$max]
360 @vis [$v]
361 @debug_format [$debug_format]
362 $($tokens)*);
363 );
364
365 // The case where no derives are added, add serialization derives by default
366 (@attrs [$(#[$attrs:meta])*]
367 @type [$type:ident]
368 @max [$max:expr]
369 @vis [$v:vis]
370 @debug_format [$debug_format:tt]
371 $($tokens:tt)*) => (
372 newtype_index!(
373 @derives [RustcEncodable,]
374 @attrs [$(#[$attrs])*]
375 @type [$type]
376 @max [$max]
377 @vis [$v]
378 @debug_format [$debug_format]
379 $($tokens)*);
380 newtype_index!(@decodable $type);
381 );
382
383 (@decodable $type:ident) => (
384 impl $type {
385 fn __decodable__impl__hack() {
386 mod __more_hacks_because__self_doesnt_work_in_functions {
387 extern crate serialize;
388 use self::serialize::{Decodable, Decoder};
389 impl Decodable for super::$type {
390 fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
391 d.read_u32().map(Self::from)
392 }
393 }
394 }
395 }
396 }
397 );
398
399 // Rewrite final without comma to one that includes comma
400 (@derives [$($derives:ident,)*]
401 @attrs [$(#[$attrs:meta])*]
402 @type [$type:ident]
403 @max [$max:expr]
404 @vis [$v:vis]
405 @debug_format [$debug_format:tt]
406 $name:ident = $constant:expr) => (
407 newtype_index!(
408 @derives [$($derives,)*]
409 @attrs [$(#[$attrs])*]
410 @type [$type]
411 @max [$max]
412 @vis [$v]
413 @debug_format [$debug_format]
414 $name = $constant,);
415 );
416
417 // Rewrite final const without comma to one that includes comma
418 (@derives [$($derives:ident,)*]
419 @attrs [$(#[$attrs:meta])*]
420 @type [$type:ident]
421 @max [$_max:expr]
422 @vis [$v:vis]
423 @debug_format [$debug_format:tt]
424 $(#[doc = $doc:expr])*
425 const $name:ident = $constant:expr) => (
426 newtype_index!(
427 @derives [$($derives,)*]
428 @attrs [$(#[$attrs])*]
429 @type [$type]
430 @max [$max]
431 @vis [$v]
432 @debug_format [$debug_format]
433 $(#[doc = $doc])* const $name = $constant,);
434 );
435
436 // Replace existing default for max
437 (@derives [$($derives:ident,)*]
438 @attrs [$(#[$attrs:meta])*]
439 @type [$type:ident]
440 @max [$_max:expr]
441 @vis [$v:vis]
442 @debug_format [$debug_format:tt]
443 MAX = $max:expr,
444 $($tokens:tt)*) => (
445 newtype_index!(
446 @derives [$($derives,)*]
447 @attrs [$(#[$attrs])*]
448 @type [$type]
449 @max [$max]
450 @vis [$v]
451 @debug_format [$debug_format]
452 $($tokens)*);
453 );
454
455 // Replace existing default for debug_format
456 (@derives [$($derives:ident,)*]
457 @attrs [$(#[$attrs:meta])*]
458 @type [$type:ident]
459 @max [$max:expr]
460 @vis [$v:vis]
461 @debug_format [$_debug_format:tt]
462 DEBUG_FORMAT = $debug_format:tt,
463 $($tokens:tt)*) => (
464 newtype_index!(
465 @derives [$($derives,)*]
466 @attrs [$(#[$attrs])*]
467 @type [$type]
468 @max [$max]
469 @vis [$v]
470 @debug_format [$debug_format]
471 $($tokens)*);
472 );
473
474 // Assign a user-defined constant
475 (@derives [$($derives:ident,)*]
476 @attrs [$(#[$attrs:meta])*]
477 @type [$type:ident]
478 @max [$max:expr]
479 @vis [$v:vis]
480 @debug_format [$debug_format:tt]
481 $(#[doc = $doc:expr])*
482 const $name:ident = $constant:expr,
483 $($tokens:tt)*) => (
484 $(#[doc = $doc])*
485 pub const $name: $type = $type::from_u32_const($constant);
486 newtype_index!(
487 @derives [$($derives,)*]
488 @attrs [$(#[$attrs])*]
489 @type [$type]
490 @max [$max]
491 @vis [$v]
492 @debug_format [$debug_format]
493 $($tokens)*);
494 );
495 }
496
497 #[derive(Clone, PartialEq, Eq, Hash)]
498 pub struct IndexVec<I: Idx, T> {
499 pub raw: Vec<T>,
500 _marker: PhantomData<fn(&I)>
501 }
502
503 // Whether `IndexVec` is `Send` depends only on the data,
504 // not the phantom data.
505 unsafe impl<I: Idx, T> Send for IndexVec<I, T> where T: Send {}
506
507 impl<I: Idx, T: serialize::Encodable> serialize::Encodable for IndexVec<I, T> {
508 fn encode<S: serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
509 serialize::Encodable::encode(&self.raw, s)
510 }
511 }
512
513 impl<I: Idx, T: serialize::Decodable> serialize::Decodable for IndexVec<I, T> {
514 fn decode<D: serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
515 serialize::Decodable::decode(d).map(|v| {
516 IndexVec { raw: v, _marker: PhantomData }
517 })
518 }
519 }
520
521 impl<I: Idx, T: fmt::Debug> fmt::Debug for IndexVec<I, T> {
522 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
523 fmt::Debug::fmt(&self.raw, fmt)
524 }
525 }
526
527 pub type Enumerated<I, J> = iter::Map<iter::Enumerate<J>, IntoIdx<I>>;
528
529 impl<I: Idx, T> IndexVec<I, T> {
530 #[inline]
531 pub fn new() -> Self {
532 IndexVec { raw: Vec::new(), _marker: PhantomData }
533 }
534
535 #[inline]
536 pub fn from_raw(raw: Vec<T>) -> Self {
537 IndexVec { raw, _marker: PhantomData }
538 }
539
540 #[inline]
541 pub fn with_capacity(capacity: usize) -> Self {
542 IndexVec { raw: Vec::with_capacity(capacity), _marker: PhantomData }
543 }
544
545 #[inline]
546 pub fn from_elem<S>(elem: T, universe: &IndexVec<I, S>) -> Self
547 where T: Clone
548 {
549 IndexVec { raw: vec![elem; universe.len()], _marker: PhantomData }
550 }
551
552 #[inline]
553 pub fn from_elem_n(elem: T, n: usize) -> Self
554 where T: Clone
555 {
556 IndexVec { raw: vec![elem; n], _marker: PhantomData }
557 }
558
559 #[inline]
560 pub fn push(&mut self, d: T) -> I {
561 let idx = I::new(self.len());
562 self.raw.push(d);
563 idx
564 }
565
566 #[inline]
567 pub fn pop(&mut self) -> Option<T> {
568 self.raw.pop()
569 }
570
571 #[inline]
572 pub fn len(&self) -> usize {
573 self.raw.len()
574 }
575
576 /// Gives the next index that will be assigned when `push` is
577 /// called.
578 #[inline]
579 pub fn next_index(&self) -> I {
580 I::new(self.len())
581 }
582
583 #[inline]
584 pub fn is_empty(&self) -> bool {
585 self.raw.is_empty()
586 }
587
588 #[inline]
589 pub fn into_iter(self) -> vec::IntoIter<T> {
590 self.raw.into_iter()
591 }
592
593 #[inline]
594 pub fn into_iter_enumerated(self) -> Enumerated<I, vec::IntoIter<T>>
595 {
596 self.raw.into_iter().enumerate().map(IntoIdx { _marker: PhantomData })
597 }
598
599 #[inline]
600 pub fn iter(&self) -> slice::Iter<'_, T> {
601 self.raw.iter()
602 }
603
604 #[inline]
605 pub fn iter_enumerated(&self) -> Enumerated<I, slice::Iter<'_, T>>
606 {
607 self.raw.iter().enumerate().map(IntoIdx { _marker: PhantomData })
608 }
609
610 #[inline]
611 pub fn indices(&self) -> iter::Map<Range<usize>, IntoIdx<I>> {
612 (0..self.len()).map(IntoIdx { _marker: PhantomData })
613 }
614
615 #[inline]
616 pub fn iter_mut(&mut self) -> slice::IterMut<'_, T> {
617 self.raw.iter_mut()
618 }
619
620 #[inline]
621 pub fn iter_enumerated_mut(&mut self) -> Enumerated<I, slice::IterMut<'_, T>>
622 {
623 self.raw.iter_mut().enumerate().map(IntoIdx { _marker: PhantomData })
624 }
625
626 #[inline]
627 pub fn drain<'a, R: RangeBounds<usize>>(
628 &'a mut self, range: R) -> impl Iterator<Item=T> + 'a {
629 self.raw.drain(range)
630 }
631
632 #[inline]
633 pub fn drain_enumerated<'a, R: RangeBounds<usize>>(
634 &'a mut self, range: R) -> impl Iterator<Item=(I, T)> + 'a {
635 self.raw.drain(range).enumerate().map(IntoIdx { _marker: PhantomData })
636 }
637
638 #[inline]
639 pub fn last(&self) -> Option<I> {
640 self.len().checked_sub(1).map(I::new)
641 }
642
643 #[inline]
644 pub fn shrink_to_fit(&mut self) {
645 self.raw.shrink_to_fit()
646 }
647
648 #[inline]
649 pub fn swap(&mut self, a: I, b: I) {
650 self.raw.swap(a.index(), b.index())
651 }
652
653 #[inline]
654 pub fn truncate(&mut self, a: usize) {
655 self.raw.truncate(a)
656 }
657
658 #[inline]
659 pub fn get(&self, index: I) -> Option<&T> {
660 self.raw.get(index.index())
661 }
662
663 #[inline]
664 pub fn get_mut(&mut self, index: I) -> Option<&mut T> {
665 self.raw.get_mut(index.index())
666 }
667
668 /// Returns mutable references to two distinct elements, a and b. Panics if a == b.
669 #[inline]
670 pub fn pick2_mut(&mut self, a: I, b: I) -> (&mut T, &mut T) {
671 let (ai, bi) = (a.index(), b.index());
672 assert!(ai != bi);
673
674 if ai < bi {
675 let (c1, c2) = self.raw.split_at_mut(bi);
676 (&mut c1[ai], &mut c2[0])
677 } else {
678 let (c2, c1) = self.pick2_mut(b, a);
679 (c1, c2)
680 }
681 }
682
683 pub fn convert_index_type<Ix: Idx>(self) -> IndexVec<Ix, T> {
684 IndexVec {
685 raw: self.raw,
686 _marker: PhantomData,
687 }
688 }
689 }
690
691 impl<I: Idx, T: Clone> IndexVec<I, T> {
692 /// Grows the index vector so that it contains an entry for
693 /// `elem`; if that is already true, then has no
694 /// effect. Otherwise, inserts new values as needed by invoking
695 /// `fill_value`.
696 #[inline]
697 pub fn ensure_contains_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) {
698 let min_new_len = elem.index() + 1;
699 if self.len() < min_new_len {
700 self.raw.resize_with(min_new_len, fill_value);
701 }
702 }
703
704 #[inline]
705 pub fn resize(&mut self, new_len: usize, value: T) {
706 self.raw.resize(new_len, value)
707 }
708
709 #[inline]
710 pub fn resize_to_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) {
711 let min_new_len = elem.index() + 1;
712 self.raw.resize_with(min_new_len, fill_value);
713 }
714 }
715
716 impl<I: Idx, T: Ord> IndexVec<I, T> {
717 #[inline]
718 pub fn binary_search(&self, value: &T) -> Result<I, I> {
719 match self.raw.binary_search(value) {
720 Ok(i) => Ok(Idx::new(i)),
721 Err(i) => Err(Idx::new(i)),
722 }
723 }
724 }
725
726 impl<I: Idx, T> Index<I> for IndexVec<I, T> {
727 type Output = T;
728
729 #[inline]
730 fn index(&self, index: I) -> &T {
731 &self.raw[index.index()]
732 }
733 }
734
735 impl<I: Idx, T> IndexMut<I> for IndexVec<I, T> {
736 #[inline]
737 fn index_mut(&mut self, index: I) -> &mut T {
738 &mut self.raw[index.index()]
739 }
740 }
741
742 impl<I: Idx, T> Default for IndexVec<I, T> {
743 #[inline]
744 fn default() -> Self {
745 Self::new()
746 }
747 }
748
749 impl<I: Idx, T> Extend<T> for IndexVec<I, T> {
750 #[inline]
751 fn extend<J: IntoIterator<Item = T>>(&mut self, iter: J) {
752 self.raw.extend(iter);
753 }
754 }
755
756 impl<I: Idx, T> FromIterator<T> for IndexVec<I, T> {
757 #[inline]
758 fn from_iter<J>(iter: J) -> Self where J: IntoIterator<Item=T> {
759 IndexVec { raw: FromIterator::from_iter(iter), _marker: PhantomData }
760 }
761 }
762
763 impl<I: Idx, T> IntoIterator for IndexVec<I, T> {
764 type Item = T;
765 type IntoIter = vec::IntoIter<T>;
766
767 #[inline]
768 fn into_iter(self) -> vec::IntoIter<T> {
769 self.raw.into_iter()
770 }
771
772 }
773
774 impl<'a, I: Idx, T> IntoIterator for &'a IndexVec<I, T> {
775 type Item = &'a T;
776 type IntoIter = slice::Iter<'a, T>;
777
778 #[inline]
779 fn into_iter(self) -> slice::Iter<'a, T> {
780 self.raw.iter()
781 }
782 }
783
784 impl<'a, I: Idx, T> IntoIterator for &'a mut IndexVec<I, T> {
785 type Item = &'a mut T;
786 type IntoIter = slice::IterMut<'a, T>;
787
788 #[inline]
789 fn into_iter(self) -> slice::IterMut<'a, T> {
790 self.raw.iter_mut()
791 }
792 }
793
794 pub struct IntoIdx<I: Idx> { _marker: PhantomData<fn(&I)> }
795 impl<I: Idx, T> FnOnce<((usize, T),)> for IntoIdx<I> {
796 type Output = (I, T);
797
798 extern "rust-call" fn call_once(self, ((n, t),): ((usize, T),)) -> Self::Output {
799 (I::new(n), t)
800 }
801 }
802
803 impl<I: Idx, T> FnMut<((usize, T),)> for IntoIdx<I> {
804 extern "rust-call" fn call_mut(&mut self, ((n, t),): ((usize, T),)) -> Self::Output {
805 (I::new(n), t)
806 }
807 }
808
809 impl<I: Idx> FnOnce<(usize,)> for IntoIdx<I> {
810 type Output = I;
811
812 extern "rust-call" fn call_once(self, (n,): (usize,)) -> Self::Output {
813 I::new(n)
814 }
815 }
816
817 impl<I: Idx> FnMut<(usize,)> for IntoIdx<I> {
818 extern "rust-call" fn call_mut(&mut self, (n,): (usize,)) -> Self::Output {
819 I::new(n)
820 }
821 }