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