hexga_core 0.0.11-beta.48

Basic set of functionalities common to all hexga crate
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
//! Generalisation over collection, such as `get`, `get_mut`, `get_many_mut`, `swap`, `replace`, `set`...
//!
//! Each trait follow this convention when adding a new function `foo`  :
//!
//! `fn try_get_foo(...) -> Result<O,E>`
//!
//! `fn foo(...) -> Option<O>` (or `bool` instead of `Option<()>` when `try_get_foo` return a `Result<(), E>`)
//!
//! `fn foo_or_panic(...) -> O`
//!
//! `unsafe fn foo_unchecked(...) -> O`

use super::*;

/// The collection have a quick way to access each element, where the index is copyable
pub trait Get<Idx>: Collection
{
    type Output: ?Sized;

    /// Returns a reference to the value.
    fn get(&self, index: Idx) -> Option<&Self::Output>;
    /// Returns a reference to the value.
    #[inline(always)]
    #[track_caller]
    fn get_or_panic(&self, index: Idx) -> &Self::Output { self.get(index).expect("invalid index") }
    /// Returns a reference to the value.
    #[inline(always)]
    #[track_caller]
    unsafe fn get_unchecked(&self, index: Idx) -> &Self::Output
    {
        self.get(index).expect("invalid index")
    }

    /// True if `get(index)` return [Some], false otherwise.
    #[inline(always)]
    fn is_index_valid(&self, index: Idx) -> bool { self.get(index).is_some() }
    /// True if `get(index)` return [None], false otherwise.
    #[inline(always)]
    fn is_index_invalid(&self, index: Idx) -> bool { self.get(index).is_none() }
}

pub trait TryGet<Idx>: Get<Idx>
{
    type Error;
    /// Returns a reference to the value.
    fn try_get(&self, index: Idx) -> Result<&Self::Output, Self::Error>;
}

pub trait GetMut<Idx>: Get<Idx>
{
    /// Returns a mutable reference to the value.
    fn get_mut(&mut self, index: Idx) -> Option<&mut Self::Output>;
    #[inline(always)]
    #[track_caller]
    fn get_mut_or_panic(&mut self, index: Idx) -> &mut Self::Output
    {
        self.get_mut(index).expect("invalid index")
    }
    /// Returns a mutable reference to the value.
    #[inline(always)]
    #[track_caller]
    unsafe fn get_unchecked_mut(&mut self, index: Idx) -> &mut Self::Output
    {
        self.get_mut(index).expect("invalid index")
    }

    /// Replace the value and return the old one.
    ///
    /// This operation is an [`involution`](https://en.wikipedia.org/wiki/Involution_(mathematics)):
    /// Replacing a value twice (first with a new value, then with the previously returned value) leaves the collection unchanged.
    #[inline(always)]
    fn replace(&mut self, index: Idx, value: Self::Output) -> Option<Self::Output>
    where
        Self::Output: Sized,
    {
        self.get_mut(index)
            .map(|dest| core::mem::replace(dest, value))
    }
    /// Replace the value and return the old one.
    ///
    /// This operation is an [`involution`](https://en.wikipedia.org/wiki/Involution_(mathematics)):
    /// Replacing a value twice (first with a new value, then with the previously returned value) leaves the collection unchanged.
    #[inline(always)]
    #[track_caller]
    fn replace_or_panic(&mut self, index: Idx, value: Self::Output) -> Self::Output
    where
        Self::Output: Sized,
    {
        self.replace(index, value).expect("invalid index")
    }
    /// Replace the value and return the old one.
    ///
    /// This operation is an [`involution`](https://en.wikipedia.org/wiki/Involution_(mathematics)):
    /// Replacing a value twice (first with a new value, then with the previously returned value) leaves the collection unchanged.
    #[inline(always)]
    #[track_caller]
    unsafe fn replace_unchecked(&mut self, index: Idx, value: Self::Output) -> Self::Output
    where
        Self::Output: Sized,
    {
        core::mem::replace(unsafe { self.get_unchecked_mut(index) }, value)
    }

    /// Set the value and drop the previous one.
    #[inline(always)]
    fn set(&mut self, index: Idx, value: Self::Output) -> bool
    where
        Self::Output: Sized,
    {
        self.replace(index, value).map(|_| ()).is_some()
    }
    /// Set the value and drop the previous one.
    #[inline(always)]
    #[track_caller]
    fn set_or_panic(&mut self, index: Idx, value: Self::Output) -> &mut Self
    where
        Self::Output: Sized,
    {
        assert!(self.set(index, value), "invalid index");
        self
    }
    #[inline(always)]
    #[track_caller]
    unsafe fn set_unchecked(&mut self, index: Idx, value: Self::Output) -> &mut Self
    where
        Self::Output: Sized,
    {
        unsafe { self.replace_unchecked(index, value) };
        self
    }
}

pub trait TryGetMut<Idx>: TryGet<Idx>
{
    // Should TryGetMut have it own error type ?

    /// Returns a mutable reference to the value.
    fn try_get_mut(&mut self, index: Idx) -> Result<&mut Self::Output, Self::Error>;

    /// Replace the value and return the old one.
    ///
    /// This operation is an [`involution`](https://en.wikipedia.org/wiki/Involution_(mathematics)):
    /// Replacing a value twice (first with a new value, then with the previously returned value) leaves the collection unchanged.
    #[inline(always)]
    fn try_replace(&mut self, index: Idx, value: Self::Output) -> Result<Self::Output, Self::Error>
    where
        Self::Output: Sized,
    {
        self.try_get_mut(index)
            .map(|dest| core::mem::replace(dest, value))
    }

    /// Set the value and drop the previous one.
    #[inline(always)]
    fn try_set(&mut self, index: Idx, value: Self::Output) -> Result<(), Self::Error>
    where
        Self::Output: Sized,
    {
        self.try_replace(index, value).map(|_| ())
    }
}

pub type ManyMutError = core::slice::GetDisjointMutError;

pub trait GetManyMut<Idx>: GetMut<Idx>
{
    /// Returns multiples mutables references to the values.
    /// All values that can be accessed with the indices must be disjoint.
    #[doc(alias = "get_disjoint_mut")]
    fn get_many_mut<const N: usize>(&mut self, indices: [Idx; N])
    -> Option<[&mut Self::Output; N]>
    {
        self.try_get_many_mut(indices).ok()
    }

    fn try_get_many_mut<const N: usize>(
        &mut self,
        indices: [Idx; N],
    ) -> Result<[&mut Self::Output; N], ManyMutError>;

    /// Returns multiples mutables references to the values.
    /// All values that can be accessed with the indices must be disjoint.
    #[inline(always)]
    #[track_caller]
    #[doc(alias = "get_disjoint_mut_or_panic")]
    fn get_many_mut_or_panic<const N: usize>(&mut self, indices: [Idx; N])
    -> [&mut Self::Output; N]
    {
        self.get_many_mut(indices).expect("invalid index")
    }

    /// Returns multiples mutables references to the values.
    /// All values that can be accessed with the indices must be disjoint.
    #[inline(always)]
    #[track_caller]
    #[doc(alias = "get_disjoint_unchecked_mut")]
    unsafe fn get_many_unchecked_mut<const N: usize>(
        &mut self,
        indices: [Idx; N],
    ) -> [&mut Self::Output; N]
    {
        self.get_many_mut(indices).expect("invalid index")
    }

    /// Swaps the values at two mutable locations, without deinitializing either one.
    ///
    /// Swap is symmetric: `foo.try_swap(a, b)` is equivalent to `foo.try_swap(b, a)` and vis versa
    ///
    /// Do nothings if some value  overlap or don't exist.
    #[inline(always)]
    fn swap(&mut self, a: Idx, b: Idx) -> bool
    where
        Self::Output: Sized,
    {
        self.get_many_mut([a, b])
            .map(|[a, b]| core::mem::swap(a, b))
            .is_some()
    }
    /// Swaps the values at two mutable locations, without deinitializing either one.
    ///
    /// Swap is symmetric: `foo.try_swap(a, b)` is equivalent to `foo.try_swap(b, a)` and vis versa
    ///
    /// Panics if any value overlap or don't exist
    #[inline(always)]
    #[track_caller]
    fn swap_or_panic(&mut self, a: Idx, b: Idx)
    where
        Self::Output: Sized,
    {
        assert!(self.swap(a, b), "invalid index")
    }
    /// Swaps the values at two mutable locations, without deinitializing either one.
    ///
    /// Swap is symmetric: `foo.try_swap(a, b)` is equivalent to `foo.try_swap(b, a)` and vis versa
    ///
    /// Do nothings if some value  overlap or don't exist.
    #[inline(always)]
    #[track_caller]
    unsafe fn swap_unchecked(&mut self, a: Idx, b: Idx)
    where
        Self::Output: Sized,
    {
        let [a, b] = unsafe { self.get_many_unchecked_mut([a, b]) };
        core::mem::swap(a, b);
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct IndexInvalid<Idx = usize>
{
    pub index: Idx,
}
impl<Idx> IndexInvalid<Idx>
{
    pub fn new(index: Idx) -> Self { Self { index } }
}

pub type IndexOutOfRange<Idx = usize, B = core::ops::Range<Idx>> = IndexOutOfBounds<Idx, B>;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct IndexOutOfBounds<Idx = usize, B = core::ops::Range<Idx>>
{
    pub index: Idx,
    pub bound: B,
}
impl<Idx, B> IndexOutOfBounds<Idx, B>
{
    pub fn new(index: Idx, bound: B) -> Self { Self { index, bound } }
}

impl<Idx, T> Get<Idx> for [T]
where
    Idx: SliceIndex<[T]>,
{
    type Output = <Self as Index<Idx>>::Output;
    #[inline(always)]
    fn get(&self, index: Idx) -> Option<&Self::Output> { self.get(index) }
    #[inline(always)]
    #[track_caller]
    unsafe fn get_unchecked(&self, index: Idx) -> &Self::Output
    {
        unsafe { self.get_unchecked(index) }
    }
}
impl<Idx, T> Get<Idx> for &[T]
where
    Idx: SliceIndex<[T]>,
{
    type Output = <[T] as Index<Idx>>::Output;
    #[inline(always)]
    fn get(&self, index: Idx) -> Option<&Self::Output> { (*self).get(index) }
    #[inline(always)]
    #[track_caller]
    unsafe fn get_unchecked(&self, index: Idx) -> &Self::Output
    {
        unsafe { (*self).get_unchecked(index) }
    }
}
impl<Idx, T> Get<Idx> for &mut [T]
where
    Idx: SliceIndex<[T]>,
{
    type Output = <[T] as Index<Idx>>::Output;
    #[inline(always)]
    fn get(&self, index: Idx) -> Option<&Self::Output> { (**self).get(index) }
    #[inline(always)]
    #[track_caller]
    unsafe fn get_unchecked(&self, index: Idx) -> &Self::Output
    {
        unsafe { (**self).get_unchecked(index) }
    }
}
impl<Idx, T> TryGet<Idx> for [T]
where
    Idx: SliceIndex<[T]> + Clone,
{
    type Error = IndexOutOfBounds<Idx, Range<usize>>;
    #[inline(always)]
    fn try_get(&self, index: Idx) -> Result<&Self::Output, Self::Error>
    {
        self.get(index.clone()).ok_or(IndexOutOfBounds {
            index,
            bound: 0..self.len(),
        })
    }
}
impl<Idx, T> TryGet<Idx> for &[T]
where
    Idx: SliceIndex<[T]> + Clone,
{
    type Error = IndexOutOfBounds<Idx, Range<usize>>;
    #[inline(always)]
    fn try_get(&self, index: Idx) -> Result<&Self::Output, Self::Error> { (*self).try_get(index) }
}
impl<Idx, T> TryGet<Idx> for &mut [T]
where
    Idx: SliceIndex<[T]> + Clone,
{
    type Error = IndexOutOfBounds<Idx, Range<usize>>;
    #[inline(always)]
    fn try_get(&self, index: Idx) -> Result<&Self::Output, Self::Error> { (**self).try_get(index) }
}

impl<Idx, T> GetMut<Idx> for [T]
where
    Idx: SliceIndex<[T]>,
{
    #[inline(always)]
    fn get_mut(&mut self, index: Idx) -> Option<&mut Self::Output> { self.get_mut(index) }
    #[inline(always)]
    #[track_caller]
    unsafe fn get_unchecked_mut(&mut self, index: Idx) -> &mut Self::Output
    {
        unsafe { self.get_unchecked_mut(index) }
    }
}
impl<Idx, T> GetMut<Idx> for &mut [T]
where
    Idx: SliceIndex<[T]>,
{
    #[inline(always)]
    fn get_mut(&mut self, index: Idx) -> Option<&mut Self::Output> { (*self).get_mut(index) }
    #[inline(always)]
    #[track_caller]
    unsafe fn get_unchecked_mut(&mut self, index: Idx) -> &mut Self::Output
    {
        unsafe { (*self).get_unchecked_mut(index) }
    }
}
impl<Idx, T> TryGetMut<Idx> for [T]
where
    Idx: SliceIndex<[T]> + Clone,
{
    #[inline(always)]
    fn try_get_mut(&mut self, index: Idx) -> Result<&mut Self::Output, Self::Error>
    {
        let len = self.len();
        self.get_mut(index.clone()).ok_or(IndexOutOfBounds {
            index,
            bound: 0..len,
        })
    }
}
impl<Idx, T> TryGetMut<Idx> for &mut [T]
where
    Idx: SliceIndex<[T]> + Clone,
{
    #[inline(always)]
    fn try_get_mut(&mut self, index: Idx) -> Result<&mut Self::Output, Self::Error>
    {
        (**self).try_get_mut(index)
    }
}
impl<Idx, T> GetManyMut<Idx> for [T]
where
    Idx: SliceIndex<[T]> + GetDisjointMutIndex,
{
    #[inline(always)]
    fn get_many_mut<const N: usize>(&mut self, indices: [Idx; N])
    -> Option<[&mut Self::Output; N]>
    {
        self.get_disjoint_mut(indices).ok()
    }

    #[inline(always)]
    fn try_get_many_mut<const N: usize>(
        &mut self,
        indices: [Idx; N],
    ) -> Result<[&mut Self::Output; N], ManyMutError>
    {
        self.get_disjoint_mut(indices)
    }

    #[inline(always)]
    #[track_caller]
    unsafe fn get_many_unchecked_mut<const N: usize>(
        &mut self,
        indices: [Idx; N],
    ) -> [&mut Self::Output; N]
    {
        unsafe { self.get_disjoint_unchecked_mut(indices) }
    }
}
impl<Idx, T> GetManyMut<Idx> for &mut [T]
where
    Idx: SliceIndex<[T]> + GetDisjointMutIndex,
{
    #[inline(always)]
    fn get_many_mut<const N: usize>(&mut self, indices: [Idx; N])
    -> Option<[&mut Self::Output; N]>
    {
        (*self).get_many_mut(indices)
    }

    #[inline(always)]
    fn try_get_many_mut<const N: usize>(
        &mut self,
        indices: [Idx; N],
    ) -> Result<[&mut Self::Output; N], ManyMutError>
    {
        (*self).try_get_many_mut(indices)
    }

    #[inline(always)]
    #[track_caller]
    unsafe fn get_many_unchecked_mut<const N: usize>(
        &mut self,
        indices: [Idx; N],
    ) -> [&mut Self::Output; N]
    {
        unsafe { (*self).get_many_unchecked_mut(indices) }
    }
}

impl<Idx, T, const N: usize> TryGet<Idx> for [T; N]
where
    [T]: TryGet<Idx>,
{
    type Error = <[T] as TryGet<Idx>>::Error;
    #[inline(always)]
    fn try_get(&self, index: Idx) -> Result<&Self::Output, Self::Error>
    {
        TryGet::try_get(self.as_slice(), index)
    }
}

impl<Idx, T, const N: usize> Get<Idx> for [T; N]
where
    [T]: Get<Idx>,
{
    type Output = <[T] as Get<Idx>>::Output;
    #[inline(always)]
    fn get(&self, index: Idx) -> Option<&Self::Output> { Get::get(self.as_slice(), index) }
    #[inline(always)]
    #[track_caller]
    unsafe fn get_unchecked(&self, index: Idx) -> &Self::Output
    {
        unsafe { Get::get_unchecked(self.as_slice(), index) }
    }
}

impl<Idx, T, const N: usize> TryGetMut<Idx> for [T; N]
where
    [T]: TryGetMut<Idx>,
{
    #[inline(always)]
    fn try_get_mut(&mut self, index: Idx) -> Result<&mut Self::Output, Self::Error>
    {
        TryGetMut::try_get_mut(self.as_mut_slice(), index)
    }
}

impl<Idx, T, const N: usize> GetMut<Idx> for [T; N]
where
    [T]: GetMut<Idx>,
{
    #[inline(always)]
    fn get_mut(&mut self, index: Idx) -> Option<&mut Self::Output>
    {
        GetMut::get_mut(self.as_mut_slice(), index)
    }
    #[inline(always)]
    unsafe fn get_unchecked_mut(&mut self, index: Idx) -> &mut Self::Output
    {
        unsafe { GetMut::get_unchecked_mut(self.as_mut_slice(), index) }
    }
}
impl<Idx, T, const N: usize> GetManyMut<Idx> for [T; N]
where
    [T]: GetManyMut<Idx>,
{
    #[inline(always)]
    fn get_many_mut<const N2: usize>(
        &mut self,
        indices: [Idx; N2],
    ) -> Option<[&mut Self::Output; N2]>
    {
        GetManyMut::get_many_mut(self.as_mut_slice(), indices)
    }

    #[inline(always)]
    fn try_get_many_mut<const N2: usize>(
        &mut self,
        indices: [Idx; N2],
    ) -> Result<[&mut Self::Output; N2], ManyMutError>
    {
        GetManyMut::try_get_many_mut(self.as_mut_slice(), indices)
    }

    #[inline(always)]
    #[track_caller]
    unsafe fn get_many_unchecked_mut<const N2: usize>(
        &mut self,
        indices: [Idx; N2],
    ) -> [&mut Self::Output; N2]
    {
        unsafe { GetManyMut::get_many_unchecked_mut(self.as_mut_slice(), indices) }
    }
}

impl<Idx, T> TryGet<Idx> for Vec<T>
where
    [T]: TryGet<Idx>,
{
    type Error = <[T] as TryGet<Idx>>::Error;
    #[inline(always)]
    fn try_get(&self, index: Idx) -> Result<&Self::Output, Self::Error>
    {
        TryGet::try_get(self.as_slice(), index)
    }
}
impl<Idx, T> Get<Idx> for Vec<T>
where
    [T]: Get<Idx>,
{
    type Output = <[T] as Get<Idx>>::Output;
    #[inline(always)]
    fn get(&self, index: Idx) -> Option<&Self::Output> { Get::get(self.as_slice(), index) }
    #[track_caller]
    #[inline(always)]
    unsafe fn get_unchecked(&self, index: Idx) -> &Self::Output
    {
        unsafe { Get::get_unchecked(self.as_slice(), index) }
    }
}
impl<Idx, T> TryGetMut<Idx> for Vec<T>
where
    [T]: TryGetMut<Idx>,
{
    #[inline(always)]
    fn try_get_mut(&mut self, index: Idx) -> Result<&mut Self::Output, Self::Error>
    {
        TryGetMut::try_get_mut(self.as_mut_slice(), index)
    }
}
impl<Idx, T> GetMut<Idx> for Vec<T>
where
    [T]: GetMut<Idx>,
{
    #[inline(always)]
    fn get_mut(&mut self, index: Idx) -> Option<&mut Self::Output>
    {
        GetMut::get_mut(self.as_mut_slice(), index)
    }
    #[inline(always)]
    unsafe fn get_unchecked_mut(&mut self, index: Idx) -> &mut Self::Output
    {
        unsafe { GetMut::get_unchecked_mut(self.as_mut_slice(), index) }
    }
}
impl<Idx, T> GetManyMut<Idx> for Vec<T>
where
    [T]: GetManyMut<Idx>,
{
    #[track_caller]
    #[inline(always)]
    unsafe fn get_many_unchecked_mut<const N: usize>(
        &mut self,
        indices: [Idx; N],
    ) -> [&mut Self::Output; N]
    {
        unsafe { GetManyMut::get_many_unchecked_mut(self.as_mut_slice(), indices) }
    }

    #[inline(always)]
    fn try_get_many_mut<const N: usize>(
        &mut self,
        indices: [Idx; N],
    ) -> Result<[&mut Self::Output; N], ManyMutError>
    {
        GetManyMut::try_get_many_mut(self.as_mut_slice(), indices)
    }
    #[inline(always)]
    fn get_many_mut<const N: usize>(&mut self, indices: [Idx; N])
    -> Option<[&mut Self::Output; N]>
    {
        GetManyMut::get_many_mut(self.as_mut_slice(), indices)
    }
}

impl<T> TryGet<usize> for VecDeque<T>
{
    type Error = IndexOutOfBounds<usize, Range<usize>>;
    #[inline(always)]
    fn try_get(&self, index: usize) -> Result<&Self::Output, Self::Error>
    {
        self.get(index).ok_or(IndexOutOfBounds {
            index,
            bound: 0..self.len(),
        })
    }
}
impl<T> Get<usize> for VecDeque<T>
{
    type Output = <Self as Index<usize>>::Output;
    #[inline(always)]
    fn get(&self, index: usize) -> Option<&Self::Output> { self.get(index) }
}
impl<T> TryGetMut<usize> for VecDeque<T>
{
    #[inline(always)]
    fn try_get_mut(&mut self, index: usize) -> Result<&mut Self::Output, Self::Error>
    {
        let len = self.len();
        self.get_mut(index).ok_or(IndexOutOfBounds {
            index,
            bound: 0..len,
        })
    }
}
impl<T> GetMut<usize> for VecDeque<T>
{
    #[inline(always)]
    fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output> { self.get_mut(index) }
}

impl<T> GetManyMut<usize> for VecDeque<T>
{
    #[track_caller]
    #[inline(always)]
    unsafe fn get_many_unchecked_mut<const N: usize>(
        &mut self,
        indices: [usize; N],
    ) -> [&mut Self::Output; N]
    {
        unsafe {
            // Can probably be improved using `self.as_mut_slices()`
            self.make_contiguous().get_disjoint_unchecked_mut(indices)
        }
    }
    #[inline(always)]
    fn try_get_many_mut<const N: usize>(
        &mut self,
        indices: [usize; N],
    ) -> Result<[&mut Self::Output; N], ManyMutError>
    {
        self.make_contiguous().try_get_many_mut(indices)
    }
    #[inline(always)]
    fn get_many_mut<const N: usize>(
        &mut self,
        indices: [usize; N],
    ) -> Option<[&mut Self::Output; N]>
    {
        self.make_contiguous().get_many_mut(indices)
    }
}

impl<Idx> Get<Idx> for str
where
    Idx: SliceIndex<str>,
{
    type Output = <Self as Index<Idx>>::Output;
    #[inline(always)]
    fn get(&self, index: Idx) -> Option<&Self::Output> { self.get(index) }
    #[inline(always)]
    #[track_caller]
    unsafe fn get_unchecked(&self, index: Idx) -> &Self::Output
    {
        unsafe { self.get_unchecked(index) }
    }
}
impl<Idx> TryGet<Idx> for str
where
    Idx: SliceIndex<str> + Clone,
{
    type Error = IndexOutOfBounds<Idx, Range<usize>>;
    fn try_get(&self, index: Idx) -> Result<&Self::Output, Self::Error>
    {
        self.get(index.clone()).ok_or(IndexOutOfBounds {
            index,
            bound: 0..self.len(),
        })
    }
}

impl<Idx> Get<Idx> for &str
where
    Idx: SliceIndex<str>,
{
    type Output = <str as Index<Idx>>::Output;
    #[inline(always)]
    fn get(&self, index: Idx) -> Option<&Self::Output> { (*self).get(index) }
    #[inline(always)]
    #[track_caller]
    unsafe fn get_unchecked(&self, index: Idx) -> &Self::Output
    {
        unsafe { (*self).get_unchecked(index) }
    }
}
impl<Idx> TryGet<Idx> for &str
where
    Idx: SliceIndex<str> + Clone,
{
    type Error = IndexOutOfBounds<Idx, Range<usize>>;
    fn try_get(&self, index: Idx) -> Result<&Self::Output, Self::Error> { (*self).try_get(index) }
}

impl<Idx> Get<Idx> for &mut str
where
    Idx: SliceIndex<str>,
{
    type Output = <str as Index<Idx>>::Output;
    #[inline(always)]
    fn get(&self, index: Idx) -> Option<&Self::Output> { (**self).get(index) }
    #[inline(always)]
    #[track_caller]
    unsafe fn get_unchecked(&self, index: Idx) -> &Self::Output
    {
        unsafe { (**self).get_unchecked(index) }
    }
}
impl<Idx> TryGet<Idx> for &mut str
where
    Idx: SliceIndex<str> + Clone,
{
    type Error = IndexOutOfBounds<Idx, Range<usize>>;
    fn try_get(&self, index: Idx) -> Result<&Self::Output, Self::Error> { (**self).try_get(index) }
}

// FIXME: I'm not sure how to delegate it to `str` like delegating the TryGet for `Vec<T>` into the TryGet of `[T]`
impl<Idx> TryGet<Idx> for String
where
    Idx: SliceIndex<str> + Clone,
{
    type Error = IndexOutOfBounds<Idx, Range<usize>>;
    fn try_get(&self, index: Idx) -> Result<&Self::Output, Self::Error>
    {
        self.as_str().try_get(index)
    }
}
impl<Idx> Get<Idx> for String
where
    Idx: SliceIndex<str>,
{
    type Output = <str as Get<Idx>>::Output;
    #[inline(always)]
    fn get(&self, index: Idx) -> Option<&Self::Output> { self.as_str().get(index) }
    #[track_caller]
    #[inline(always)]
    unsafe fn get_unchecked(&self, index: Idx) -> &Self::Output
    {
        unsafe { self.as_str().get_unchecked(index) }
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct MissingKey<K>
{
    pub key: K,
}
impl<K> MissingKey<K>
{
    pub fn new(key: K) -> Self { Self { key } }
}

#[cfg(feature = "std")]
impl<K, V, S, Q> TryGet<&Q> for HashMap<K, V, S>
where
    K: Borrow<Q>,
    Q: ?Sized + Hash + Eq + Clone,
    K: Eq + Hash,
    S: BuildHasher,
{
    type Error = MissingKey<Q>;

    fn try_get(&self, key: &Q) -> Result<&Self::Output, Self::Error>
    {
        self.get(key).ok_or_else(|| MissingKey::new(key.clone()))
    }
}

#[cfg(feature = "std")]
impl<K, V, S, Q> Get<&Q> for HashMap<K, V, S>
where
    K: Borrow<Q>,
    Q: ?Sized + Hash + Eq,
    K: Eq + Hash,
    S: BuildHasher,
{
    type Output = V;
    #[inline(always)]
    fn get(&self, k: &Q) -> Option<&Self::Output> { self.get(k) }
}

#[cfg(feature = "std")]
impl<K, V, S, Q> TryGetMut<&Q> for HashMap<K, V, S>
where
    K: Borrow<Q>,
    Q: ?Sized + Hash + Eq + Clone,
    K: Eq + Hash,
    S: BuildHasher,
{
    #[inline(always)]
    fn try_get_mut(&mut self, key: &Q) -> Result<&mut Self::Output, Self::Error>
    where
        K: Borrow<Q>,
    {
        self.get_mut(key)
            .ok_or_else(|| MissingKey::new(key.clone()))
    }
}
#[cfg(feature = "std")]
impl<K, V, S, Q> GetMut<&Q> for HashMap<K, V, S>
where
    K: Borrow<Q>,
    Q: ?Sized + Hash + Eq,
    K: Eq + Hash,
    S: BuildHasher,
{
    #[inline(always)]
    fn get_mut(&mut self, k: &Q) -> Option<&mut Self::Output>
    where
        K: Borrow<Q>,
    {
        self.get_mut(k)
    }
}

#[cfg(feature = "std")]
impl<K, V, S, Q> GetManyMut<&Q> for HashMap<K, V, S>
where
    K: Borrow<Q>,
    Q: ?Sized + Hash + Eq,
    K: Eq + Hash,
    S: BuildHasher,
{
    #[inline(always)]
    fn try_get_many_mut<const N: usize>(
        &mut self,
        keys: [&Q; N],
    ) -> Result<[&mut Self::Output; N], ManyMutError>
    {
        // I wanted to check this is the get_many_mut fail, but the borrow checker is complaining for no reason about self being already borrowed.
        for k in keys
        {
            if self.get_mut(k).is_none()
            {
                return Err(ManyMutError::IndexOutOfBounds);
            }
        }

        if let Some(r) = self.get_many_mut(keys)
        {
            return Ok(r);
        }

        Err(ManyMutError::OverlappingIndices)
    }

    fn get_many_mut<const N: usize>(&mut self, keys: [&Q; N]) -> Option<[&mut Self::Output; N]>
    {
        // Use try_map https://doc.rust-lang.org/std/primitive.array.html#method.try_map when #stabilized
        // [Option<T>, N] to Option<[T;N]> (same for result)
        let r = self.get_disjoint_mut(keys);

        if r.iter().any(|x| x.is_none())
        {
            None
        }
        else
        {
            Some(r.map(|x| x.unwrap()))
        }
    }
}

impl<K, V, Q> TryGet<&Q> for BTreeMap<K, V>
where
    K: Borrow<Q>,
    Q: ?Sized + Ord + Clone,
    K: Ord,
{
    type Error = MissingKey<Q>;
    fn try_get(&self, index: &Q) -> Result<&Self::Output, Self::Error>
    {
        self.get(index)
            .ok_or_else(|| MissingKey::new(index.clone()))
    }
}
impl<K, V, Q> Get<&Q> for BTreeMap<K, V>
where
    K: Borrow<Q>,
    Q: ?Sized + Ord,
    K: Ord,
{
    type Output = V;
    #[inline(always)]
    fn get(&self, k: &Q) -> Option<&Self::Output>
    where
        K: Borrow<Q>,
    {
        self.get(k)
    }
}

impl<K, V, Q> TryGetMut<&Q> for BTreeMap<K, V>
where
    K: Borrow<Q>,
    Q: ?Sized + Ord + Clone,
    K: Ord,
{
    fn try_get_mut(&mut self, index: &Q) -> Result<&mut Self::Output, Self::Error>
    {
        self.get_mut(index)
            .ok_or_else(|| MissingKey::new(index.clone()))
    }
}
impl<K, V, Q> GetMut<&Q> for BTreeMap<K, V>
where
    K: Borrow<Q>,
    Q: ?Sized + Ord,
    K: Ord,
{
    #[inline(always)]
    fn get_mut(&mut self, k: &Q) -> Option<&mut Self::Output>
    where
        K: Borrow<Q>,
    {
        self.get_mut(k)
    }
}

#[cfg(feature = "std")]
impl<K, S, Q> TryGet<&Q> for HashSet<K, S>
where
    K: Borrow<Q>,
    Q: ?Sized + Hash + Eq + Clone,
    K: Eq + Hash,
    S: BuildHasher,
{
    type Error = MissingKey<Q>;
    fn try_get(&self, index: &Q) -> Result<&Self::Output, Self::Error>
    {
        self.get(index)
            .ok_or_else(|| MissingKey::new(index.clone()))
    }
}
#[cfg(feature = "std")]
impl<K, S, Q> Get<&Q> for HashSet<K, S>
where
    K: Borrow<Q>,
    Q: ?Sized + Hash + Eq,
    K: Eq + Hash,
    S: BuildHasher,
{
    type Output = K;
    #[inline(always)]
    fn get(&self, k: &Q) -> Option<&Self::Output> { self.get(k) }
}

impl<K, Q> TryGet<&Q> for BTreeSet<K>
where
    K: Borrow<Q>,
    Q: ?Sized + Ord + Clone,
    K: Ord,
{
    type Error = MissingKey<Q>;
    fn try_get(&self, index: &Q) -> Result<&Self::Output, Self::Error>
    {
        self.get(index)
            .ok_or_else(|| MissingKey::new(index.clone()))
    }
}
impl<K, Q> Get<&Q> for BTreeSet<K>
where
    K: Borrow<Q>,
    Q: ?Sized + Ord,
    K: Ord,
{
    type Output = K;
    #[inline(always)]
    fn get(&self, k: &Q) -> Option<&Self::Output> { self.get(k) }
}