[go: up one dir, main page]

lexical-core 0.3.0

Lexical, to- and from-string conversion routines.
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
//! Helper for vector-like classes.

#![allow(dead_code)]

use lib::{iter, marker, ops, ptr, slice};
use stackvector;

#[cfg(all(feature = "correct", feature = "radix"))]
use lib::Vec;

// REMOVE_MANY

/// Remove many elements from a vec-like container.
///
/// Does not change the size of the vector, and may leak
/// if the destructor panics. **Must** call `set_len` after,
/// and ideally before (to 0).
fn remove_many<V, T, R>(vec: &mut V, range: R)
    where V: VecLike<T>,
          R: ops::RangeBounds<usize>
{
    // Get the bounds on the items we're removing.
    let len = vec.len();
    let start = match range.start_bound() {
        ops::Bound::Included(&n) => n,
        ops::Bound::Excluded(&n) => n + 1,
        ops::Bound::Unbounded    => 0,
    };
    let end = match range.end_bound() {
        ops::Bound::Included(&n) => n + 1,
        ops::Bound::Excluded(&n) => n,
        ops::Bound::Unbounded    => len,
    };
    assert!(start <= end);
    assert!(end <= len);

    // Drop the existing items.
    unsafe {
        // Set len temporarily to the start, in case we panic on a drop.
        // This means we leak memory, but we don't allow any double freeing,
        // or use after-free.
        vec.set_len(start);
        // Iteratively drop the range.
        let mut first = vec.as_mut_ptr().add(start);
        let last = vec.as_mut_ptr().add(end);
        while first < last {
            ptr::drop_in_place(first);
            first = first.add(1);
        }

        // Now we need to copy the end range into the buffer.
        let count = len - end;
        if count != 0 {
            let src = vec.as_ptr().add(end);
            let dst = vec.as_mut_ptr().add(start);
            ptr::copy(src, dst, count);
        }

        // Set the proper length, now that we've moved items in.
        vec.set_len(start + count);
    }
}

// AS SLICE

/// Collection that has an `as_slice()` method.
pub trait AsSlice<T>: {
    /// View the collection as a slice.
    fn as_slice(&self) -> &[T];
}

#[cfg(all(feature = "correct", feature = "radix"))]
impl<T> AsSlice<T> for Vec<T> {
    #[inline]
    fn as_slice(&self) -> &[T] {
        Vec::as_slice(self)
    }
}

impl<A: stackvector::Array> AsSlice<A::Item> for stackvector::StackVec<A> {
    #[inline]
    fn as_slice(&self) -> &[A::Item] {
        stackvector::StackVec::as_slice(self)
    }
}

// EXTEND FROM SLICE

/// Collection that can be extended from a slice.
pub trait ExtendFromSlice<T: Clone>: Clone + Default {
    /// Extend collection from slice.
    fn extend_from_slice(&mut self, other: &[T]);
}

#[cfg(all(feature = "correct", feature = "radix"))]
impl<T: Clone> ExtendFromSlice<T> for Vec<T> {
    #[inline]
    fn extend_from_slice(&mut self, other: &[T]) {
        Vec::extend_from_slice(self, other)
    }
}

impl<A: stackvector::Array> ExtendFromSlice<A::Item> for stackvector::StackVec<A>
    where A::Item: Copy + Clone
{
    #[inline]
    fn extend_from_slice(&mut self, other: &[A::Item]) {
        stackvector::StackVec::extend_from_slice(self, other)
    }
}

// LEN

/// Collection that has a `len()` method.
pub trait Len<T>: {
    /// Get the length of the collection.
    fn len(&self) -> usize;
}

impl<T> Len<T> for [T] {
    #[inline]
    fn len(&self) -> usize {
        <[T]>::len(self)
    }
}

#[cfg(all(feature = "correct", feature = "radix"))]
impl<T> Len<T> for Vec<T> {
    #[inline]
    fn len(&self) -> usize {
        Vec::len(self)
    }
}

impl<A: stackvector::Array> Len<A::Item> for stackvector::StackVec<A> {
    #[inline]
    fn len(&self) -> usize {
        stackvector::StackVec::len(self)
    }
}

// RESERVE

/// Collection that can call reserve.
pub trait Reserve<T>: {
    /// Reserve additional capacity for the collection.
    fn reserve(&mut self, capacity: usize);
}

#[cfg(all(feature = "correct", feature = "radix"))]
impl<T> Reserve<T> for Vec<T> {
    #[inline]
    fn reserve(&mut self, capacity: usize) {
        Vec::reserve(self, capacity)
    }
}

impl<A: stackvector::Array> Reserve<A::Item> for stackvector::StackVec<A> {
    #[inline]
    fn reserve(&mut self, capacity: usize) {
        assert!(capacity < self.capacity());
    }
}

// RESERVE EXACT

/// Collection that can call reserve_exact.
pub trait ReserveExact<T>: {
    /// Reserve minimal additional capacity for the collection.
    fn reserve_exact(&mut self, capacity: usize);
}

#[cfg(all(feature = "correct", feature = "radix"))]
impl<T> ReserveExact<T> for Vec<T> {
    #[inline]
    fn reserve_exact(&mut self, capacity: usize) {
        Vec::reserve_exact(self, capacity)
    }
}

impl<A: stackvector::Array> ReserveExact<A::Item> for stackvector::StackVec<A>
{
    #[inline]
    fn reserve_exact(&mut self, capacity: usize) {
        assert!(capacity < self.capacity());
    }
}

// RESIZE

/// Resizable container.
///
/// Implemented for Vec, SmallVec, and StackVec.
pub trait Resize<T: Clone>: Clone + Default {
    /// Resize container to new length, with a default value if adding elements.
    fn resize(&mut self, len: usize, value: T);
}

#[cfg(all(feature = "correct", feature = "radix"))]
impl<T: Clone> Resize<T> for Vec<T> {
    #[inline]
    fn resize(&mut self, len: usize, value: T) {
        Vec::resize(self, len, value)
    }
}

impl<A: stackvector::Array> Resize<A::Item> for stackvector::StackVec<A>
    where A::Item: Clone
{
    #[inline]
    fn resize(&mut self, len: usize, value: A::Item) {
        stackvector::StackVec::resize(self, len, value)
    }
}

/// A trait for reversed-indexing operations.
pub trait RSliceIndex<T: ?Sized> {
    /// Output type for the index.
    type Output: ?Sized;

    /// Get reference to element or subslice.
    fn rget(self, slc: &T) -> Option<&Self::Output>;

    /// Get mutable reference to element or subslice.
    fn rget_mut(self, slc: &mut T) -> Option<&mut Self::Output>;

    /// Get reference to element or subslice without bounds checking.
    unsafe fn rget_unchecked(self, slc: &T) -> &Self::Output;

    /// Get mutable reference to element or subslice without bounds checking.
    unsafe fn rget_unchecked_mut(self, slc: &mut T) -> &mut Self::Output;

    /// Get reference to element or subslice, panic if out-of-bounds.
    fn rindex(self, slc: &T) -> &Self::Output;

    /// Get mutable reference to element or subslice, panic if out-of-bounds.
    fn rindex_mut(self, slc: &mut T) -> &mut Self::Output;
}

impl<T> RSliceIndex<[T]> for usize {
    type Output = T;

    #[inline]
    fn rget(self, slc: &[T]) -> Option<&T> {
        let len = slc.len();
        slc.get(len - self - 1)
    }

    #[inline]
    fn rget_mut(self, slc: &mut [T]) -> Option<&mut T> {
        let len = slc.len();
        slc.get_mut(len - self - 1)
    }

    #[inline]
    unsafe fn rget_unchecked(self, slc: &[T]) -> &T {
        let len = slc.len();
        slc.get_unchecked(len - self - 1)
    }

    #[inline]
    unsafe fn rget_unchecked_mut(self, slc: &mut [T]) -> &mut T {
        let len = slc.len();
        slc.get_unchecked_mut(len - self - 1)
    }

    #[inline]
    fn rindex(self, slc: &[T]) -> &T {
        let len = slc.len();
        &(*slc)[len - self - 1]
    }

    #[inline]
    fn rindex_mut(self, slc: &mut [T]) -> &mut T {
        let len = slc.len();
        &mut (*slc)[len - self - 1]
    }
}

// RGET

/// Get items using reverse-indexing.
pub trait Rget<T> {
    /// Get reference to element or subslice.
    fn rget<I: RSliceIndex<[T]>>(&self, index: I) -> Option<&I::Output>;

    /// Get mutable reference to element or subslice.
    fn rget_mut<I: RSliceIndex<[T]>>(&mut self, index: I) -> Option<&mut I::Output>;

    /// Get reference to element or subslice without bounds checking.
    unsafe fn rget_unchecked<I: RSliceIndex<[T]>>(&self, index: I) -> &I::Output;

    /// Get mutable reference to element or subslice without bounds checking.
    unsafe fn rget_unchecked_mut<I: RSliceIndex<[T]>>(&mut self, index: I) -> &mut I::Output;
}

impl<T> Rget<T> for [T] {
    fn rget<I: RSliceIndex<[T]>>(&self, index: I)
        -> Option<&I::Output>
    {
        index.rget(self)
    }

    fn rget_mut<I: RSliceIndex<[T]>>(&mut self, index: I)
        -> Option<&mut I::Output>
    {
        index.rget_mut(self)
    }

    unsafe fn rget_unchecked<I: RSliceIndex<[T]>>(&self, index: I)
        -> &I::Output
    {
        index.rget_unchecked(self)
    }

    unsafe fn rget_unchecked_mut<I: RSliceIndex<[T]>>(&mut self, index: I)
        -> &mut I::Output
    {
        index.rget_unchecked_mut(self)
    }
}

#[cfg(all(feature = "correct", feature = "radix"))]
impl<T> Rget<T> for Vec<T> {
    fn rget<I: RSliceIndex<[T]>>(&self, index: I)
        -> Option<&I::Output>
    {
        index.rget(self.as_slice())
    }

    fn rget_mut<I: RSliceIndex<[T]>>(&mut self, index: I)
        -> Option<&mut I::Output>
    {
        index.rget_mut(self.as_mut_slice())
    }

    unsafe fn rget_unchecked<I: RSliceIndex<[T]>>(&self, index: I)
        -> &I::Output
    {
        index.rget_unchecked(self.as_slice())
    }

    unsafe fn rget_unchecked_mut<I: RSliceIndex<[T]>>(&mut self, index: I)
        -> &mut I::Output
    {
        index.rget_unchecked_mut(self.as_mut_slice())
    }
}

impl<A: stackvector::Array> Rget<A::Item> for stackvector::StackVec<A> {
    fn rget<I: RSliceIndex<[A::Item]>>(&self, index: I)
        -> Option<&I::Output>
    {
        index.rget(self.as_slice())
    }

    fn rget_mut<I: RSliceIndex<[A::Item]>>(&mut self, index: I)
        -> Option<&mut I::Output>
    {
        index.rget_mut(self.as_mut_slice())
    }

    unsafe fn rget_unchecked<I: RSliceIndex<[A::Item]>>(&self, index: I)
        -> &I::Output
    {
        index.rget_unchecked(self.as_slice())
    }

    unsafe fn rget_unchecked_mut<I: RSliceIndex<[A::Item]>>(&mut self, index: I)
        -> &mut I::Output
    {
        index.rget_unchecked_mut(self.as_mut_slice())
    }
}

// RINDEX

/// Get items using reverse-indexing.
pub trait RIndex<T> {
    /// Get reference to element or subslice.
    fn rindex<I: RSliceIndex<[T]>>(&self, index: I) -> &I::Output;
}

impl<T> RIndex<T> for [T] {
    fn rindex<I: RSliceIndex<[T]>>(&self, index: I) -> &I::Output
    {
        index.rindex(self)
    }
}

#[cfg(all(feature = "correct", feature = "radix"))]
impl<T> RIndex<T> for Vec<T> {
    fn rindex<I: RSliceIndex<[T]>>(&self, index: I) -> &I::Output
    {
        index.rindex(self.as_slice())
    }
}

impl<A: stackvector::Array> RIndex<A::Item> for stackvector::StackVec<A> {
    fn rindex<I: RSliceIndex<[A::Item]>>(&self, index: I) -> &I::Output
    {
        index.rindex(self.as_slice())
    }
}

// RINDEXMUT

/// Get mutable items using reverse-indexing.
pub trait RIndexMut<T> {
    /// Get mutable reference to element or subslice.
    fn rindex_mut<I: RSliceIndex<[T]>>(&mut self, index: I) -> &mut I::Output;
}

impl<T> RIndexMut<T> for [T] {
    fn rindex_mut<I: RSliceIndex<[T]>>(&mut self, index: I) -> &mut I::Output
    {
        index.rindex_mut(self)
    }
}

#[cfg(all(feature = "correct", feature = "radix"))]
impl<T> RIndexMut<T> for Vec<T> {
    fn rindex_mut<I: RSliceIndex<[T]>>(&mut self, index: I) -> &mut I::Output
    {
        index.rindex_mut(self.as_mut_slice())
    }
}

impl<A: stackvector::Array> RIndexMut<A::Item> for stackvector::StackVec<A> {
    fn rindex_mut<I: RSliceIndex<[A::Item]>>(&mut self, index: I) -> &mut I::Output
    {
        index.rindex_mut(self.as_mut_slice())
    }
}

/// REVERSE VIEW

/// Reverse, immutable view of a sequence.
pub struct ReverseView<'a, T> {
    inner: &'a [T],
}

impl<'a, T> ops::Index<usize> for ReverseView<'a, T> {
    type Output = T;

    #[inline]
    fn index(&self, index: usize) -> &T {
        self.inner.rindex(index)
    }
}

/// REVERSE VIEW MUT

/// Reverse, mutable view of a sequence.
pub struct ReverseViewMut<'a, T> {
    inner: &'a mut [T],
}

impl<'a, T> ops::Index<usize> for ReverseViewMut<'a, T> {
    type Output = T;

    #[inline]
    fn index(&self, index: usize) -> &T {
        self.inner.rindex(index)
    }
}

impl<'a, T> ops::IndexMut<usize> for ReverseViewMut<'a, T> {
    #[inline]
    fn index_mut(&mut self, index: usize) -> &mut T {
        self.inner.rindex_mut(index)
    }
}

// SLICELIKE

/// Slice-like container.
pub trait SliceLike<T>:
    ops::Index<usize, Output=T> +
    ops::IndexMut<usize> +
    ops::Index<ops::Range<usize>, Output=[T]> +
    ops::IndexMut<ops::Range<usize>> +
    ops::Index<ops::RangeFrom<usize>, Output=[T]> +
    ops::IndexMut<ops::RangeFrom<usize>> +
    ops::Index<ops::RangeTo<usize>, Output=[T]> +
    ops::IndexMut<ops::RangeTo<usize>> +
    ops::Index<ops::RangeFull, Output=[T]> +
    ops::IndexMut<ops::RangeFull> +
    Len<T> +
    Rget<T> +
    RIndex<T> +
    RIndexMut<T>
{
    // GET/SET

    /// Get an immutable reference to item at index.
    #[inline]
    fn get<I: slice::SliceIndex<[T]>>(&self, index: I) -> Option<&I::Output>;

    /// Get a mutable reference to item at index.
    #[inline]
    fn get_mut<I: slice::SliceIndex<[T]>>(&mut self, index: I) -> Option<&mut I::Output>;

    /// Get an immutable reference to item at index.
    #[inline]
    unsafe fn get_unchecked<I: slice::SliceIndex<[T]>>(&self, index: I) -> &I::Output;

    /// Get a mutable reference to item at index.
    #[inline]
    unsafe fn get_unchecked_mut<I: slice::SliceIndex<[T]>>(&mut self, index: I) -> &mut I::Output;

    // FRONT

    /// Get an immutable reference to the front item.
    #[inline]
    fn front(&self) -> Option<&T> {
        self.get(0)
    }

    /// Get an mutable reference to the front item.
    #[inline]
    fn front_mut(&mut self) -> Option<&mut T> {
        debug_assert!(self.len() > 0);
        self.get_mut(0)
    }

    /// Get an immutable reference to the front item.
    #[inline]
    unsafe fn front_unchecked(&self) -> &T {
        debug_assert!(self.len() > 0);
        self.get_unchecked(0)
    }

    /// Get an mutable reference to the front item.
    #[inline]
    unsafe fn front_unchecked_mut(&mut self) -> &mut T {
        debug_assert!(self.len() > 0);
        self.get_unchecked_mut(0)
    }

    // BACK

    /// Get an immutable reference to the back item.
    #[inline]
    fn back(&self) -> Option<&T> {
        self.rget(0)
    }

    /// Get an mutable reference to the back item.
    #[inline]
    fn back_mut(&mut self) -> Option<&mut T> {
        self.rget_mut(0)
    }

    /// Get an immutable reference to the back item.
    #[inline]
    unsafe fn back_unchecked(&self) -> &T {
        self.rget_unchecked(0)
    }

    /// Get an mutable reference to the back item.
    #[inline]
    unsafe fn back_unchecked_mut(&mut self) -> &mut T {
        self.rget_unchecked_mut(0)
    }

    /// Create a reverse view of the vector for indexing.
    #[inline]
    fn rview<'a>(&'a self) -> ReverseView<'a, T> {
        ReverseView { inner: &self[..] }
    }

    /// Create a reverse, mutable view of the vector for indexing.
    #[inline]
    fn rview_mut<'a>(&'a mut self) -> ReverseViewMut<'a, T> {
        ReverseViewMut { inner: &mut self[..] }
    }
}

impl<T> SliceLike<T> for [T] {
    /// Get an immutable reference to item at index.
    #[inline]
    fn get<I: slice::SliceIndex<[T]>>(&self, index: I) -> Option<&I::Output> {
        <[T]>::get(self, index)
    }

    /// Get an mutable reference to item at index.
    #[inline]
    fn get_mut<I: slice::SliceIndex<[T]>>(&mut self, index: I) -> Option<&mut I::Output> {
        <[T]>::get_mut(self, index)
    }

    /// Get an immutable reference to item at index.
    #[inline]
    unsafe fn get_unchecked<I: slice::SliceIndex<[T]>>(&self, index: I) -> &I::Output {
        <[T]>::get_unchecked(self, index)
    }

    /// Get an mutable reference to item at index.
    #[inline]
    unsafe fn get_unchecked_mut<I: slice::SliceIndex<[T]>>(&mut self, index: I) -> &mut I::Output {
        <[T]>::get_unchecked_mut(self, index)
    }
}

#[cfg(all(feature = "correct", feature = "radix"))]
impl<T> SliceLike<T> for Vec<T> {
    /// Get an immutable reference to item at index.
    #[inline]
    fn get<I: slice::SliceIndex<[T]>>(&self, index: I) -> Option<&I::Output> {
        self.as_slice().get(index)
    }

    /// Get an mutable reference to item at index.
    #[inline]
    fn get_mut<I: slice::SliceIndex<[T]>>(&mut self, index: I) -> Option<&mut I::Output> {
        self.as_mut_slice().get_mut(index)
    }

    /// Get an immutable reference to item at index.
    #[inline]
    unsafe fn get_unchecked<I: slice::SliceIndex<[T]>>(&self, index: I) -> &I::Output {
        self.as_slice().get_unchecked(index)
    }

    /// Get an mutable reference to item at index.
    #[inline]
    unsafe fn get_unchecked_mut<I: slice::SliceIndex<[T]>>(&mut self, index: I) -> &mut I::Output {
        self.as_mut_slice().get_unchecked_mut(index)
    }
}

impl<A: stackvector::Array> SliceLike<A::Item> for stackvector::StackVec<A> {
    /// Get an immutable reference to item at index.
    #[inline]
    fn get<I: slice::SliceIndex<[A::Item]>>(&self, index: I) -> Option<&I::Output> {
        self.as_slice().get(index)
    }

    /// Get an mutable reference to item at index.
    #[inline]
    fn get_mut<I: slice::SliceIndex<[A::Item]>>(&mut self, index: I) -> Option<&mut I::Output> {
        self.as_mut_slice().get_mut(index)
    }

    /// Get an immutable reference to item at index.
    #[inline]
    unsafe fn get_unchecked<I: slice::SliceIndex<[A::Item]>>(&self, index: I) -> &I::Output {
        self.as_slice().get_unchecked(index)
    }

    /// Get an mutable reference to item at index.
    #[inline]
    unsafe fn get_unchecked_mut<I: slice::SliceIndex<[A::Item]>>(&mut self, index: I) -> &mut I::Output {
        self.as_mut_slice().get_unchecked_mut(index)
    }
}

// VECLIKE

/// Vector-like container.
///
/// Implemented for Vec, SmallVec, and StackVec.
pub trait VecLike<T>:
    iter::FromIterator<T> +
    iter::IntoIterator +
    ops::Index<usize, Output=T> +
    ops::IndexMut<usize> +
    ops::Index<ops::Range<usize>, Output=[T]> +
    ops::IndexMut<ops::Range<usize>> +
    ops::Index<ops::RangeFrom<usize>, Output=[T]> +
    ops::IndexMut<ops::RangeFrom<usize>> +
    ops::Index<ops::RangeTo<usize>, Output=[T]> +
    ops::IndexMut<ops::RangeTo<usize>> +
    ops::Index<ops::RangeFull, Output=[T]> +
    ops::IndexMut<ops::RangeFull> +
    ops::DerefMut<Target = [T]> +
    AsSlice<T> +
    Extend<T> +
    SliceLike<T> +
    Default
{
    /// Append an element to the vector.
    fn push(&mut self, value: T);

    /// Pop an element from the end of the vector.
    fn pop(&mut self) -> Option<T>;

    /// Insert many elements at index, pushing everything else to the back.
    fn insert_many<I: iter::IntoIterator<Item=T>>(&mut self, index: usize, iterable: I);

    /// Remove many elements from range.
    fn remove_many<R: ops::RangeBounds<usize>>(&mut self, range: R);

    /// Set the buffer length (unsafe).
    unsafe fn set_len(&mut self, new_len: usize);

    /// Clear the buffer
    fn clear(&mut self);
}

#[cfg(all(feature = "correct", feature = "radix"))]
impl<T> VecLike<T> for Vec<T> {
    #[inline]
    fn push(&mut self, value: T) {
        Vec::push(self, value);
    }

    #[inline]
    fn pop(&mut self) -> Option<T> {
        Vec::pop(self)
    }

    #[inline]
    unsafe fn set_len(&mut self, new_len: usize) {
        Vec::set_len(self, new_len);
    }

    #[inline]
    fn clear(&mut self) {
        Vec::clear(self);
    }

    #[inline]
    fn insert_many<I: iter::IntoIterator<Item=T>>(&mut self, index: usize, iterable: I) {
        self.splice(index..index, iterable);
    }

    #[inline]
    fn remove_many<R: ops::RangeBounds<usize>>(&mut self, range: R) {
        remove_many(self, range)
    }
}

impl<A: stackvector::Array> VecLike<A::Item> for stackvector::StackVec<A> {
    #[inline]
    fn push(&mut self, value: A::Item) {
        stackvector::StackVec::push(self, value);
    }

    #[inline]
    fn pop(&mut self) -> Option<A::Item> {
        stackvector::StackVec::pop(self)
    }

    #[inline]
    unsafe fn set_len(&mut self, new_len: usize) {
        stackvector::StackVec::set_len(self, new_len);
    }

    #[inline]
    fn clear(&mut self) {
        stackvector::StackVec::clear(self);
    }

    #[inline]
    fn insert_many<I: iter::IntoIterator<Item=A::Item>>(&mut self, index: usize, iterable: I) {
        stackvector::StackVec::insert_many(self, index, iterable)
    }

    #[inline]
    fn remove_many<R: ops::RangeBounds<usize>>(&mut self, range: R) {
        remove_many(self, range)
    }
}

// CLONEABLE VECLIKE

/// Vector-like container with cloneable values.
///
/// Implemented for Vec, SmallVec, and StackVec.
pub trait CloneableVecLike<T: Clone + Copy + Send>:
    Send +
    ExtendFromSlice<T> +
    Resize<T> +
    Reserve<T> +
    ReserveExact<T> +
    VecLike<T>
{}

#[cfg(all(feature = "correct", feature = "radix"))]
impl<T: Clone + Copy + Send> CloneableVecLike<T> for Vec<T> {
}

impl<A: stackvector::Array> CloneableVecLike<A::Item> for stackvector::StackVec<A>
    where A::Item: Clone + Copy + Send
{}

// TESTS
// -----

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(all(feature = "correct", feature = "radix"))]
    #[test]
    fn remove_many_test() {
        let mut x = vec![0, 1, 2, 3, 4, 5];
        x.remove_many(0..3);
        assert_eq!(x, vec![3, 4, 5]);
        assert_eq!(x.len(), 3);

        let mut x = vec![0, 1, 2, 3, 4, 5];
        x.remove_many(..);
        assert_eq!(x, vec![]);

        let mut x = vec![0, 1, 2, 3, 4, 5];
        x.remove_many(3..);
        assert_eq!(x, vec![0, 1, 2]);

        let mut x = vec![0, 1, 2, 3, 4, 5];
        x.remove_many(..3);
        assert_eq!(x, vec![3, 4, 5]);
    }
}