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