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
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//! # Type Level Sized Vectors
//!
//! This crate provides a `Vec<N, A>` type, which wraps the standard `Vec<A>`
//! and tracks its size `N` at the type level.
//!
//! Because the size is embedded in the type, we can do things like verifying at
//! compile time that index lookups are within bounds.
//!
//! ```compile_fail
//! # #[macro_use] extern crate sized_vec;
//! # extern crate typenum;
//! # use typenum::U8;
//! # fn main() {
//! let vec = svec![1, 2, 3];
//! // This index lookup won't compile, because index `U8` is outside
//! // the vector's length of `U3`:
//! assert_eq!(5, vec[U8::new()]);
//! # }
//! ```
//!
//! ```
//! # #[macro_use] extern crate sized_vec;
//! # extern crate typenum;
//! # use typenum::U2;
//! # fn main() {
//! let vec = svec![1, 2, 3];
//! // On the other hand, this lookup can be verified to be correct
//! // by the type system:
//! assert_eq!(3, vec[U2::new()]);
//! # }
//! ```
//!
//! ## Limitations
//!
//! If this looks too good to be true, it's because it comes with a number of
//! limitations: you won't be able to perform operations on the vector which
//! could leave it with a length that can't be known at compile time. This
//! includes `Extend::extend()` and filtering operations like `Vec::retain()`.
//!
//! `FromIterator::from_iter` is, notably, also not available, but you can use
//! `Vec::try_from` as a replacement. Note that `try_from` needs to be able to
//! infer the size of the resulting vector at compile time; there's no way to
//! construct a vector of arbitrary length.
//!
//! ```
//! # #[macro_use] extern crate sized_vec;
//! # extern crate typenum;
//! # use sized_vec::Vec;
//! # use typenum::U2;
//! # fn main() {
//! let vec = svec![1, 2, 3, 4, 5];
//! let new_vec = Vec::try_from_iter(vec.into_iter().map(|i| i + 10));
//! assert_eq!(Some(svec![11, 12, 13, 14, 15]), new_vec);
//! # }
//! ```

use std::convert::TryFrom;
use typenum::consts::*;
use typenum::{
    Add1, Bit, Diff, Eq, IsEqual, IsLess, IsLessOrEqual, Le, LeEq, Sub1, Sum, True, Unsigned,
};

use std::borrow::{Borrow, BorrowMut};
use std::convert::{AsMut, AsRef};
use std::fmt::{Debug, Error, Formatter};
use std::marker::PhantomData;
use std::ops::{Add, Index, IndexMut, Sub};
use std::slice::{Iter, IterMut};
use std::vec::IntoIter;

pub trait IsTrue {}
impl IsTrue for True {}

/// A type level range.
pub struct Range<Left, Right>
where
    Left: Unsigned + IsLessOrEqual<Right>,
    Right: Unsigned,
    LeEq<Left, Right>: IsTrue,
{
    left: PhantomData<Left>,
    right: PhantomData<Right>,
}

impl<Left, Right> Range<Left, Right>
where
    Left: Unsigned + IsLessOrEqual<Right>,
    Right: Unsigned,
    LeEq<Left, Right>: IsTrue,
{
    /// Create an instance of this Range.
    pub fn new() -> Self {
        Range {
            left: PhantomData,
            right: PhantomData,
        }
    }

    /// Reify the range type into a `std::ops::Range<usize>` value.
    pub fn to_usize() -> ::std::ops::Range<usize> {
        Left::USIZE..Right::USIZE
    }
}

impl<Left, Right> Default for Range<Left, Right>
where
    Left: Unsigned + IsLessOrEqual<Right>,
    Right: Unsigned,
    LeEq<Left, Right>: IsTrue,
{
    fn default() -> Self {
        Self::new()
    }
}

#[macro_export]
macro_rules! svec {
    () => { $crate::vec::Vec::new() };

    ( $($x:expr),* ) => {{
        $crate::Vec::new()$(.push($x))*
    }};

    ( $($x:expr ,)* ) => {{
        $crate::Vec::new()$(.push($x))*
    }};
}

/// A vector of length `N` containing elements of type `A`.
#[derive(PartialEq, Eq, Clone, Hash, PartialOrd, Ord)]
pub struct Vec<N, A>
where
    N: Unsigned,
{
    len: PhantomData<N>,
    vec: ::std::vec::Vec<A>,
}

impl<A> Vec<U0, A> {
    /// Construct an empty vector.
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Vec {
            len: PhantomData,
            vec: vec![],
        }
    }
}

impl<N, A> Vec<N, A>
where
    N: Unsigned,
{
    #[inline]
    #[must_use]
    fn trust_me<M: Unsigned>(self) -> Vec<M, A> {
        use std::mem::transmute;
        unsafe { transmute(self) }
    }

    #[must_use]
    fn from_vec(vec: ::std::vec::Vec<A>) -> Vec<N, A> {
        Vec {
            len: PhantomData,
            vec,
        }
    }

    /// Construct a vector of size `N` using a function.
    ///
    /// The function is called with an index to generate a value of `A` for each
    /// index in the vector.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Vec;
    /// # use typenum::U3;
    /// # fn main() {
    /// let vec: Vec<U3, _> = Vec::fill(|i| i + 10);
    /// assert_eq!(svec![10, 11, 12], vec);
    /// # }
    /// ```
    #[must_use]
    pub fn fill<F>(f: F) -> Vec<N, A>
    where
        F: FnMut(usize) -> A,
    {
        Vec::from_vec((0..N::USIZE).map(f).collect())
    }

    /// Construct a vector of size `N` containing the same element repeated.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Vec;
    /// # use typenum::U3;
    /// # fn main() {
    /// let vec: Vec<U3, _> = Vec::repeat(5);
    /// assert_eq!(svec![5, 5, 5], vec);
    /// # }
    /// ```
    #[must_use]
    pub fn repeat(a: A) -> Vec<N, A>
    where
        A: Clone,
    {
        Vec::from_vec(::std::iter::repeat(a).take(N::USIZE).collect())
    }

    /// Construct a vector of size `N` from an iterator.
    ///
    /// Returns `None` if the iterator didn't contain exactly `N` elements.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Vec;
    /// # use typenum::U3;
    /// # fn main() {
    /// let good_vec: Option<Vec<U3, _>> = Vec::try_from_iter(1..=3);
    /// assert_eq!(Some(svec![1, 2, 3]), good_vec);
    ///
    /// let bad_vec: Option<Vec<U3, _>> = Vec::try_from_iter(1..=500);
    /// assert_eq!(None, bad_vec);
    /// # }
    /// ```
    #[must_use]
    pub fn try_from_iter<I>(iter: I) -> Option<Self>
    where
        I: IntoIterator<Item = A>,
    {
        let mut vec = ::std::vec::Vec::with_capacity(N::USIZE);
        vec.extend(iter);
        if vec.len() == N::USIZE {
            Some(Vec::from_vec(vec))
        } else {
            None
        }
    }

    /// Construct a vector of size `N` using the default value.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Vec;
    /// # use typenum::U3;
    /// # fn main() {
    /// let vec: Vec<U3, _> = Vec::from_default();
    /// assert_eq!(svec![0, 0, 0], vec);
    /// # }
    /// ```
    #[must_use]
    pub fn from_default() -> Vec<N, A>
    where
        A: Default,
    {
        Vec::from_vec((0..N::USIZE).map(|_| Default::default()).collect())
    }

    /// Push an element onto the end of the vector.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Vec;
    /// # use typenum::{U2, U3};
    /// # fn main() {
    /// let vec: Vec<U2, _> = svec![1, 2];
    /// let new_vec: Vec<U3, _> = vec.push(3);
    /// assert_eq!(svec![1, 2, 3], new_vec);
    /// # }
    /// ```
    ///
    /// ```compile_fail
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Vec;
    /// # use typenum::{U2, U3};
    /// # fn main() {
    /// let vec: Vec<U2, _> = svec![1, 2];
    /// // Type error, because the new length will be U3, not U2:
    /// let new_vec: Vec<U2, _> = vec.push(3);
    /// # }
    /// ```
    #[must_use]
    pub fn push(mut self, a: A) -> Vec<Add1<N>, A>
    where
        N: Add<B1>,
        Add1<N>: Unsigned,
    {
        self.vec.push(a);
        self.trust_me()
    }

    /// Pop an element off the end of the vector.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Vec;
    /// # fn main() {
    /// let vec = svec![1, 2, 3];
    /// let (new_vec, value) = vec.pop();
    /// assert_eq!(svec![1, 2], new_vec);
    /// assert_eq!(3, value);
    /// # }
    /// ```
    ///
    /// ```compile_fail
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Vec;
    /// # use typenum::{U2, U3};
    /// # fn main() {
    /// let vec: Vec<U3, _> = svec![1, 2, 3];
    /// // Type error, because the new length will be U2, not U3:
    /// let (new_vec: Vec<U3, _>, value) = vec.pop();
    /// # }
    /// ```
    #[must_use]
    pub fn pop(mut self) -> (Vec<Diff<N, U1>, A>, A)
    where
        N: Sub<U1>,
        Diff<N, U1>: Unsigned,
    {
        let o = self.vec.pop().unwrap();
        (self.trust_me(), o)
    }

    /// Insert an element into the vector at index `Index`.
    #[must_use]
    pub fn insert<Index>(mut self, _: Index, a: A) -> Vec<Add1<N>, A>
    where
        Index: Unsigned + IsLess<N>,
        Le<Index, N>: IsTrue,
        N: Add<B1>,
        Add1<N>: Unsigned,
    {
        self.vec.insert(Index::USIZE, a);
        self.trust_me()
    }

    /// Remove the element at index `Index` from the vector.
    #[must_use]
    pub fn remove<Index>(mut self, _: Index) -> (Vec<Sub1<N>, A>, A)
    where
        Index: Unsigned + IsLess<N>,
        Le<Index, N>: IsTrue,
        N: Sub<B1>,
        Sub1<N>: Unsigned,
    {
        let o = self.vec.remove(Index::USIZE);
        (self.trust_me(), o)
    }

    /// Remove the element at index `Index` from the vector,
    /// replacing it with the element at the end of the vector.
    #[must_use]
    pub fn swap_remove<Index>(mut self, _: Index) -> (Vec<Sub1<N>, A>, A)
    where
        Index: Unsigned + IsLess<N>,
        Le<Index, N>: IsTrue,
        N: Sub<B1>,
        Sub1<N>: Unsigned,
    {
        let o = self.vec.swap_remove(Index::USIZE);
        (self.trust_me(), o)
    }

    /// Append two vectors together.
    #[must_use]
    pub fn append<M>(mut self, mut other: Vec<M, A>) -> Vec<Sum<N, M>, A>
    where
        N: Add<M>,
        M: Unsigned,
        Sum<N, M>: Unsigned,
    {
        self.vec.append(&mut other.vec);
        self.trust_me()
    }

    /// Get a reference to the element at index `Index`.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Vec;
    /// # use typenum::U1;
    /// # fn main() {
    /// let vec = svec![1, 2, 3];
    /// assert_eq!(&2, vec.get(U1::new()));
    /// # }
    /// ```
    ///
    /// ```compile_fail
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Vec;
    /// # use typenum::U5;
    /// # fn main() {
    /// let vec = svec![1, 2, 3];
    /// // This index is out of bounds, so this won't compile:
    /// assert_eq!(&2, vec.get(U5::new()));
    /// # }
    /// ```
    #[inline]
    #[must_use]
    pub fn get<Index>(&self, _: Index) -> &A
    where
        Index: Unsigned + IsLess<N>,
        Le<Index, N>: IsTrue,
    {
        unsafe { self.vec.get_unchecked(Index::USIZE) }
    }

    /// Get a mutable reference to the element at index `Index`.
    #[inline]
    #[must_use]
    pub fn get_mut<Index>(&mut self, _: Index) -> &mut A
    where
        Index: Unsigned + IsLess<N>,
        Le<Index, N>: IsTrue,
    {
        unsafe { self.vec.get_unchecked_mut(Index::USIZE) }
    }

    /// Get the length of the vector.
    #[inline]
    #[must_use]
    pub fn len(&self) -> usize {
        N::USIZE
    }

    /// Test if the vector is empty.
    #[inline]
    #[must_use]
    pub fn is_empty(&self) -> bool
    where
        N: IsEqual<U0>,
    {
        Eq::<N, U0>::BOOL
    }

    /// Get an iterator over the elements of the vector.
    #[inline]
    #[must_use]
    pub fn iter(&self) -> Iter<A> {
        self.vec.iter()
    }

    /// Get a mutable iterator over the elements of the vector.
    #[inline]
    #[must_use]
    pub fn iter_mut(&mut self) -> IterMut<A> {
        self.vec.iter_mut()
    }

    /// Get a reference to the slice of elements contained in the vector.
    #[inline]
    #[must_use]
    pub fn as_slice(&self) -> &[A] {
        self.vec.as_slice()
    }

    /// Get a mutable reference to the slice of elements contained in the
    /// vector.
    #[inline]
    #[must_use]
    pub fn as_mut_slice(&mut self) -> &mut [A] {
        self.vec.as_mut_slice()
    }

    /// Truncate the vector to fit the size given by the target type.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Vec;
    /// # use typenum::U3;
    /// # fn main() {
    /// let vec_6 = svec![1, 2, 3, 4, 5, 6];
    /// let vec_3: Vec<U3, _> = vec_6.truncate();
    /// assert_eq!(svec![1, 2, 3], vec_3);
    /// # }
    /// ```
    #[must_use]
    pub fn truncate<M>(mut self) -> Vec<M, A>
    where
        M: Unsigned + IsLessOrEqual<N>,
        LeEq<M, N>: IsTrue,
    {
        self.vec.truncate(M::USIZE);
        self.trust_me()
    }

    /// Slice a subset out of the vector.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Range;
    /// # use typenum::{U2, U4};
    /// # fn main() {
    /// let vec = svec![1, 2, 3, 4, 5, 6];
    /// let (vec, sub_vec) = vec.slice(Range::<U2, U4>::new());
    /// assert_eq!(svec![1, 2, 5, 6], vec);
    /// assert_eq!(svec![3, 4], sub_vec);
    /// # }
    /// ```
    #[must_use]
    pub fn slice<Left, Right>(
        mut self,
        _: Range<Left, Right>,
    ) -> (
        Vec<Diff<N, Diff<Right, Left>>, A>,
        Vec<Diff<Right, Left>, A>,
    )
    where
        Diff<N, Diff<Right, Left>>: Unsigned,
        Diff<Right, Left>: Unsigned,
        Left: Unsigned + IsLessOrEqual<Right>,
        Right: Unsigned + Sub<Left> + IsLessOrEqual<N>,
        N: Sub<Diff<Right, Left>>,
        LeEq<Left, Right>: IsTrue,
        LeEq<Right, N>: IsTrue,
    {
        let removed = Vec::from_vec(self.vec.drain(Range::<Left, Right>::to_usize()).collect());
        (self.trust_me(), removed)
    }

    /// Remove a subset from the vector and return an iterator over the removed
    /// elements.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Range;
    /// # use typenum::{U2, U4};
    /// # fn main() {
    /// let vec = svec![1, 2, 3, 4, 5, 6];
    /// let (vec, iter) = vec.drain(Range::<U2, U4>::new());
    /// assert_eq!(svec![1, 2, 5, 6], vec);
    /// assert_eq!(vec![3, 4], iter.collect::<::std::vec::Vec<_>>());
    /// # }
    /// ```
    #[must_use]
    pub fn drain<Left, Right>(
        self,
        range: Range<Left, Right>,
    ) -> (Vec<Diff<N, Diff<Right, Left>>, A>, impl Iterator<Item = A>)
    where
        Diff<N, Diff<Right, Left>>: Unsigned,
        Diff<Right, Left>: Unsigned,
        Left: Unsigned + IsLessOrEqual<Right>,
        Right: Unsigned + Sub<Left> + IsLessOrEqual<N>,
        N: Sub<Diff<Right, Left>>,
        LeEq<Left, Right>: IsTrue,
        LeEq<Right, N>: IsTrue,
    {
        let (remainder, slice) = self.slice(range);
        (remainder.trust_me(), slice.into_iter())
    }

    /// Drop the contents of the vector, leaving an empty vector.
    #[must_use]
    pub fn clear(mut self) -> Vec<U0, A> {
        self.vec.clear();
        self.trust_me()
    }

    /// Split the vector at `Index`, returning the two sides of the split.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use typenum::U3;
    /// # fn main() {
    /// let vec = svec![1, 2, 3, 4, 5, 6];
    /// let (left, right) = vec.split_off(U3::new());
    /// assert_eq!(svec![1, 2, 3], left);
    /// assert_eq!(svec![4, 5, 6], right);
    /// # }
    /// ```
    #[must_use]
    pub fn split_off<Index>(mut self, _: Index) -> (Vec<Index, A>, Vec<Diff<N, Index>, A>)
    where
        Index: Unsigned + IsLessOrEqual<N>,
        N: Sub<Index>,
        Diff<N, Index>: Unsigned,
        LeEq<Index, N>: IsTrue,
    {
        let split = self.vec.split_off(Index::USIZE);
        (self.trust_me(), Vec::from_vec(split))
    }

    /// Resize the vector, dropping elements if the new size is smaller,
    /// and padding with copies of `value` if it is larger.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use sized_vec::Vec;
    /// # use typenum::U5;
    /// # fn main() {
    /// let vec = svec![1, 2, 3];
    /// let vec: Vec<U5, _> = vec.resize(100);
    /// assert_eq!(svec![1, 2, 3, 100, 100], vec);
    /// # }
    /// ```
    #[must_use]
    pub fn resize<M>(mut self, value: A) -> Vec<M, A>
    where
        M: Unsigned,
        A: Clone,
    {
        self.vec.resize(M::USIZE, value);
        self.trust_me()
    }

    /// Map the vector into a vector of elements of `B` using a function.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # fn main() {
    /// let vec = svec![1, 2, 3];
    /// let vec = vec.map(|num| format!("{}", num));
    /// assert_eq!(svec![
    ///     "1".to_string(), "2".to_string(), "3".to_string()
    /// ], vec);
    /// # }
    /// ```
    #[must_use]
    pub fn map<F, B>(self, f: F) -> Vec<N, B>
    where
        F: FnMut(A) -> B,
    {
        Vec::from_vec(self.into_iter().map(f).collect())
    }

    /// Apply a list of functions from `A` to `B` to a vector of `A` in order,
    /// returning a vector of `B`.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # fn main() {
    /// let vec = svec![1, 2, 3];
    /// let fn_vec = vec.clone().map(|i| move |a| a + i);
    /// let vec = vec.apply(fn_vec);
    /// assert_eq!(svec![2, 4, 6], vec);
    /// # }
    /// ```
    #[must_use]
    pub fn apply<F, B>(self, fs: Vec<N, F>) -> Vec<N, B>
    where
        F: FnMut(A) -> B,
    {
        Vec::from_vec(
            self.into_iter()
                .zip(fs.into_iter())
                .map(|(a, mut f)| f(a))
                .collect(),
        )
    }

    /// Merge two vectors together into a new vector using a function.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # fn main() {
    /// let left = svec!["foo", "bar"];
    /// let right = svec!["lol", "omg"];
    /// let vec = left.zip(right, |a, b| format!("{} {}", a, b));
    /// assert_eq!(svec![
    ///     "foo lol".to_string(), "bar omg".to_string()
    /// ], vec);
    /// # }
    /// ```
    #[must_use]
    pub fn zip<B, C, F>(self, other: Vec<N, B>, mut f: F) -> Vec<N, C>
    where
        F: FnMut(A, B) -> C,
    {
        Vec::from_vec(
            self.into_iter()
                .zip(other.into_iter())
                .map(|(a, b)| f(a, b))
                .collect(),
        )
    }

    /// Split a vector into two vectors of the same size using a function.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # fn main() {
    /// let vec = svec![1, 2, 3];
    /// let vec = vec.unzip(|a| (a, a * 2));
    /// assert_eq!((svec![1, 2, 3], svec![2, 4, 6]), vec);
    /// # }
    /// ```
    #[must_use]
    pub fn unzip<B, C, F>(self, f: F) -> (Vec<N, B>, Vec<N, C>)
    where
        F: FnMut(A) -> (B, C),
    {
        let (left, right) = self.into_iter().map(f).unzip();
        (Vec::from_vec(left), Vec::from_vec(right))
    }
}

impl<A> Default for Vec<U0, A> {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<N, A> Debug for Vec<N, A>
where
    N: Unsigned,
    A: Debug,
{
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        self.vec.fmt(f)
    }
}

impl<N, M, A> Add<Vec<M, A>> for Vec<N, A>
where
    N: Unsigned + Add<M>,
    M: Unsigned,
    Sum<N, M>: Unsigned,
{
    type Output = Vec<Sum<N, M>, A>;

    fn add(self, other: Vec<M, A>) -> Self::Output {
        self.append(other)
    }
}

impl<N, A> Into<::std::vec::Vec<A>> for Vec<N, A>
where
    N: Unsigned,
{
    fn into(self) -> ::std::vec::Vec<A> {
        self.vec
    }
}

impl<'a, N, A> Into<&'a [A]> for &'a Vec<N, A>
where
    N: Unsigned,
{
    fn into(self) -> &'a [A] {
        &self.vec
    }
}

impl<'a, N, A> Into<&'a mut [A]> for &'a mut Vec<N, A>
where
    N: Unsigned,
{
    fn into(self) -> &'a mut [A] {
        &mut self.vec
    }
}

impl<N, A> IntoIterator for Vec<N, A>
where
    N: Unsigned,
{
    type Item = A;
    type IntoIter = IntoIter<A>;

    fn into_iter(self) -> Self::IntoIter {
        self.vec.into_iter()
    }
}

impl<'a, N, A> IntoIterator for &'a Vec<N, A>
where
    N: Unsigned,
{
    type Item = &'a A;
    type IntoIter = Iter<'a, A>;

    fn into_iter(self) -> Self::IntoIter {
        self.vec.iter()
    }
}

impl<'a, N, A> IntoIterator for &'a mut Vec<N, A>
where
    N: Unsigned,
{
    type Item = &'a mut A;
    type IntoIter = IterMut<'a, A>;

    fn into_iter(self) -> Self::IntoIter {
        self.vec.iter_mut()
    }
}

impl<N, A> Borrow<[A]> for Vec<N, A>
where
    N: Unsigned,
{
    fn borrow(&self) -> &[A] {
        self.as_slice()
    }
}

impl<N, A> Borrow<::std::vec::Vec<A>> for Vec<N, A>
where
    N: Unsigned,
{
    fn borrow(&self) -> &::std::vec::Vec<A> {
        &self.vec
    }
}

impl<N, A> BorrowMut<[A]> for Vec<N, A>
where
    N: Unsigned,
{
    fn borrow_mut(&mut self) -> &mut [A] {
        self.as_mut_slice()
    }
}

impl<N, A, I> Index<I> for Vec<N, A>
where
    N: Unsigned,
    I: Unsigned + IsLess<N>,
    Le<I, N>: IsTrue,
{
    type Output = A;
    fn index(&self, index: I) -> &Self::Output {
        self.get(index)
    }
}

impl<N, A, I> IndexMut<I> for Vec<N, A>
where
    N: Unsigned,
    I: Unsigned + IsLess<N>,
    Le<I, N>: IsTrue,
{
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        self.get_mut(index)
    }
}

impl<N, A> AsRef<[A]> for Vec<N, A>
where
    N: Unsigned,
{
    fn as_ref(&self) -> &[A] {
        self.as_slice()
    }
}

impl<N, A> AsRef<Vec<N, A>> for Vec<N, A>
where
    N: Unsigned,
{
    fn as_ref(&self) -> &Self {
        self
    }
}

impl<N, A> AsRef<::std::vec::Vec<A>> for Vec<N, A>
where
    N: Unsigned,
{
    fn as_ref(&self) -> &::std::vec::Vec<A> {
        &self.vec
    }
}

impl<N, A> AsMut<[A]> for Vec<N, A>
where
    N: Unsigned,
{
    fn as_mut(&mut self) -> &mut [A] {
        self.as_mut_slice()
    }
}

impl<N, A> AsMut<Vec<N, A>> for Vec<N, A>
where
    N: Unsigned,
{
    fn as_mut(&mut self) -> &mut Self {
        self
    }
}

impl<N, A> TryFrom<std::vec::Vec<A>> for Vec<N, A>
where
    N: Unsigned,
{
    type Error = ();

    /// Construct a vector of size `N` from a `std::vec::Vec`.
    ///
    /// Returns `Err(())` if the source vector didn't contain exactly `N` elements.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use std::convert::TryFrom;
    /// # use sized_vec::Vec;
    /// # use typenum::U3;
    /// # fn main() {
    /// let good_vec: Result<Vec<U3, _>, _> = Vec::try_from(vec![1, 2, 3]);
    /// assert_eq!(Ok(svec![1, 2, 3]), good_vec);
    ///
    /// let bad_vec: Result<Vec<U3, _>, _> = Vec::try_from(vec![1, 2]);
    /// assert_eq!(Err(()), bad_vec);
    /// # }
    /// ```
    #[must_use]
    fn try_from(vec: ::std::vec::Vec<A>) -> Result<Self, Self::Error> {
        if vec.len() == N::USIZE {
            Ok(Vec::from_vec(vec))
        } else {
            Err(())
        }
    }
}

impl<N, A> TryFrom<Box<[A]>> for Vec<N, A>
where
    N: Unsigned,
{
    type Error = ();

    /// Construct a vector of size `N` from a boxed array.
    ///
    /// Returns `Err(())` if the source vector didn't contain exactly `N` elements.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use std::convert::TryFrom;
    /// # use sized_vec::Vec;
    /// # use typenum::U3;
    /// # fn main() {
    /// let boxed: Box<[_]> = Box::new([1, 2, 3]);
    /// let good_vec: Result<Vec<U3, _>, _> = Vec::try_from(boxed);
    /// assert_eq!(Ok(svec![1, 2, 3]), good_vec);
    ///
    /// let boxed: Box<[_]> = Box::new([1, 2]);
    /// let bad_vec: Result<Vec<U3, _>, _> = Vec::try_from(boxed);
    /// assert_eq!(Err(()), bad_vec);
    /// # }
    /// ```
    #[must_use]
    fn try_from(array: Box<[A]>) -> Result<Self, Self::Error> {
        if array.len() == N::USIZE {
            Ok(Vec::from_vec(array.into_vec()))
        } else {
            Err(())
        }
    }
}

impl<'a, N, A> TryFrom<&'a [A]> for Vec<N, A>
where
    A: Clone,
    N: Unsigned,
{
    type Error = ();

    /// Construct a vector of size `N` from a slice of clonable values.
    ///
    /// Returns `Err(())` if the source slice didn't contain exactly `N` elements.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # extern crate typenum;
    /// # use std::convert::TryFrom;
    /// # use sized_vec::Vec;
    /// # use typenum::U3;
    /// # fn main() {
    /// let good_vec: Result<Vec<U3, _>, _> = Vec::try_from([1, 2, 3].as_ref());
    /// assert_eq!(Ok(svec![1, 2, 3]), good_vec);
    ///
    /// let bad_vec: Result<Vec<U3, _>, _> = Vec::try_from([1, 2].as_ref());
    /// assert_eq!(Err(()), bad_vec);
    /// # }
    /// ```
    #[must_use]
    fn try_from(slice: &'a [A]) -> Result<Self, Self::Error> {
        Vec::try_from_iter(slice.iter().cloned()).ok_or(())
    }
}

macro_rules! declare_from_array {
    ($s1:expr, $s2:ty) => {
        impl<A> From<[A; $s1]> for Vec<$s2, A> {
            #[must_use]
            fn from(array: [A; $s1]) -> Self {
                let boxed_array: Box<[A]> = Box::new(array);
                let vec = boxed_array.into_vec();
                debug_assert_eq!($s1, vec.len());
                Vec::from_vec(vec)
            }
        }
    };
}

declare_from_array!(0, U0);
declare_from_array!(1, U1);
declare_from_array!(2, U2);
declare_from_array!(3, U3);
declare_from_array!(4, U4);
declare_from_array!(5, U5);
declare_from_array!(6, U6);
declare_from_array!(7, U7);
declare_from_array!(8, U8);
declare_from_array!(9, U9);
declare_from_array!(10, U10);
declare_from_array!(11, U11);
declare_from_array!(12, U12);
declare_from_array!(13, U13);
declare_from_array!(14, U14);
declare_from_array!(15, U15);
declare_from_array!(16, U16);
declare_from_array!(17, U17);
declare_from_array!(18, U18);
declare_from_array!(19, U19);
declare_from_array!(20, U20);
declare_from_array!(21, U21);
declare_from_array!(22, U22);
declare_from_array!(23, U23);
declare_from_array!(24, U24);
declare_from_array!(25, U25);
declare_from_array!(26, U26);
declare_from_array!(27, U27);
declare_from_array!(28, U28);
declare_from_array!(29, U29);
declare_from_array!(30, U30);
declare_from_array!(31, U31);

#[cfg(any(test, feature = "generic-array"))]
impl<N, A> From<generic_array::GenericArray<A, N>> for Vec<N, A>
where
    N: Unsigned + generic_array::ArrayLength<A>,
{
    /// Construct a vector of size `N` from a `GenericArray`.
    ///
    /// # Examples
    /// ```
    /// # #[macro_use] extern crate sized_vec;
    /// # use std::convert::TryFrom;
    /// # use sized_vec::Vec;
    /// # use typenum::U3;
    /// # use generic_array::GenericArray;
    /// # fn main() {
    /// let array = GenericArray::from([1, 2, 3]);
    /// let good_vec: Vec<U3, _> = Vec::from(array);
    /// assert_eq!(svec![1, 2, 3], good_vec);
    /// # }
    /// ```
    ///
    /// ```compile_fail
    /// # #[macro_use] extern crate sized_vec;
    /// # use std::convert::TryFrom;
    /// # use sized_vec::Vec;
    /// # use typenum::U3;
    /// # use generic_array::GenericArray;
    /// # fn main() {
    /// let array = GenericArray::from([1, 2]);
    /// let bad_vec: Vec<U3, _> = Vec::from(array);
    /// # }
    /// ```
    #[must_use]
    fn from(array: generic_array::GenericArray<A, N>) -> Self {
        Vec::from_vec(array.into_iter().collect())
    }
}

#[cfg(any(test, feature = "serde"))]
mod ser {
    use super::*;
    use serde::de::{Deserialize, Deserializer, Error};
    use serde::ser::{Serialize, Serializer};

    impl<N, A> Serialize for Vec<N, A>
    where
        N: Unsigned,
        A: Serialize,
    {
        fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            self.vec.serialize(ser)
        }
    }

    impl<'de, N, A> Deserialize<'de> for Vec<N, A>
    where
        N: Unsigned,
        A: Deserialize<'de>,
    {
        fn deserialize<D>(des: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            let vec: std::vec::Vec<_> = Deserialize::deserialize(des)?;
            Self::try_from(vec).map_err(|()| {
                <D as Deserializer<'de>>::Error::custom(format!(
                    "length of sized_vec::Vec was not {}",
                    N::USIZE
                ))
            })
        }
    }
}

#[cfg(any(test, feature = "proptest"))]
pub mod proptest {
    use super::*;
    use ::proptest::collection::vec as stdvec;
    use ::proptest::strategy::{BoxedStrategy, Strategy, ValueTree};

    /// A strategy for generating a sized vector.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// proptest! {
    ///     #[test]
    ///     fn proptest_a_vector(ref vec in sized_vec::<U16, _>(".*")) {
    ///         assert_eq!(16, vec.len());
    ///     }
    /// }
    /// ```
    pub fn sized_vec<N, A>(element: A) -> BoxedStrategy<Vec<N, <A::Tree as ValueTree>::Value>>
    where
        N: Unsigned + 'static,
        A: Strategy + 'static,
        <A::Tree as ValueTree>::Value: Clone,
    {
        stdvec(element, N::USIZE).prop_map(Vec::from_vec).boxed()
    }
}

#[cfg(test)]
mod tests {
    use super::proptest::sized_vec;
    use super::*;
    use ::proptest::proptest;
    use pretty_assertions::assert_eq;
    use serde_json::{from_str, to_string};

    #[test]
    fn basics() {
        let v = svec![1, 2, 3];
        assert_eq!(U3::USIZE, v.len());
        assert_eq!(&2, v.get(U1::new()));
        assert_eq!((svec![1, 2], 3), v.pop());
        let v1 = svec![1, 2, 3];
        let v2 = svec![4, 5, 6];
        let v3 = v1.append(v2);
        assert_eq!(6, v3.len());
        assert_eq!(svec![1, 2, 3, 4, 5, 6], v3);
        let v4: Vec<U4, _> = v3.truncate();
        assert_eq!(4, v4.len());
        assert_eq!(svec![1, 2, 3, 4,], v4);
        assert_eq!(2, v4[U1::new()]);
    }

    #[test]
    fn serialise() {
        let v = svec![1, 2, 3, 4, 5];
        assert_eq!(v, from_str(&to_string(&v).unwrap()).unwrap());
    }

    #[test]
    fn static_array_conversion() {
        let v = Vec::from([1, 2, 3, 4, 5]);
        assert_eq!(svec![1, 2, 3, 4, 5], v);
    }

    proptest! {
        #[test]
        fn test_the_proptest(ref vec in sized_vec::<U16, _>(".*")) {
            assert_eq!(16, vec.len())
        }
    }

}