]> git.proxmox.com Git - rustc.git/blob - src/vendor/arrayvec/src/array.rs
New upstream version 1.25.0+dfsg1
[rustc.git] / src / vendor / arrayvec / src / array.rs
1
2 /// Trait for fixed size arrays.
3 pub unsafe trait Array {
4 /// The array’s element type
5 type Item;
6 #[doc(hidden)]
7 /// The smallest index type that indexes the array.
8 type Index: Index;
9 #[doc(hidden)]
10 fn as_ptr(&self) -> *const Self::Item;
11 #[doc(hidden)]
12 fn as_mut_ptr(&mut self) -> *mut Self::Item;
13 #[doc(hidden)]
14 fn capacity() -> usize;
15 }
16
17 pub trait Index : PartialEq + Copy {
18 fn to_usize(self) -> usize;
19 fn from(usize) -> Self;
20 }
21
22 use std::slice::{from_raw_parts};
23
24 pub trait ArrayExt : Array {
25 #[inline(always)]
26 fn as_slice(&self) -> &[Self::Item] {
27 unsafe {
28 from_raw_parts(self.as_ptr(), Self::capacity())
29 }
30 }
31 }
32
33 impl<A> ArrayExt for A where A: Array { }
34
35 impl Index for u8 {
36 #[inline(always)]
37 fn to_usize(self) -> usize { self as usize }
38 #[inline(always)]
39 fn from(ix: usize) -> Self { ix as u8 }
40 }
41
42 impl Index for u16 {
43 #[inline(always)]
44 fn to_usize(self) -> usize { self as usize }
45 #[inline(always)]
46 fn from(ix: usize) -> Self { ix as u16 }
47 }
48
49 impl Index for u32 {
50 #[inline(always)]
51 fn to_usize(self) -> usize { self as usize }
52 #[inline(always)]
53 fn from(ix: usize) -> Self { ix as u32 }
54 }
55
56 impl Index for usize {
57 #[inline(always)]
58 fn to_usize(self) -> usize { self }
59 #[inline(always)]
60 fn from(ix: usize) -> Self { ix }
61 }
62
63 macro_rules! fix_array_impl {
64 ($index_type:ty, $len:expr ) => (
65 unsafe impl<T> Array for [T; $len] {
66 type Item = T;
67 type Index = $index_type;
68 #[inline(always)]
69 fn as_ptr(&self) -> *const T { self as *const _ as *const _ }
70 #[inline(always)]
71 fn as_mut_ptr(&mut self) -> *mut T { self as *mut _ as *mut _}
72 #[inline(always)]
73 fn capacity() -> usize { $len }
74 }
75 )
76 }
77
78 macro_rules! fix_array_impl_recursive {
79 ($index_type:ty, ) => ();
80 ($index_type:ty, $len:expr, $($more:expr,)*) => (
81 fix_array_impl!($index_type, $len);
82 fix_array_impl_recursive!($index_type, $($more,)*);
83 );
84 }
85
86 fix_array_impl_recursive!(u8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
87 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
88 32, 40, 48, 50, 56, 64, 72, 96, 100, 128, 160, 192, 200, 224,);
89 fix_array_impl_recursive!(u16, 256, 384, 512, 768, 1024, 2048, 4096, 8192, 16384, 32768,);
90 // This array size doesn't exist on 16-bit
91 #[cfg(any(target_pointer_width="32", target_pointer_width="64"))]
92 fix_array_impl_recursive!(u32, 1 << 16,);
93