2 /// Trait for fixed size arrays.
3 pub unsafe trait Array
{
4 /// The array’s element type
7 /// The smallest index type that indexes the array.
10 fn as_ptr(&self) -> *const Self::Item
;
12 fn as_mut_ptr(&mut self) -> *mut Self::Item
;
14 fn capacity() -> usize;
17 pub trait Index
: PartialEq
+ Copy
{
18 fn to_usize(self) -> usize;
19 fn from(usize) -> Self;
22 use std
::slice
::{from_raw_parts}
;
24 pub trait ArrayExt
: Array
{
26 fn as_slice(&self) -> &[Self::Item
] {
28 from_raw_parts(self.as_ptr(), Self::capacity())
33 impl<A
> ArrayExt
for A
where A
: Array { }
37 fn to_usize(self) -> usize { self as usize }
39 fn from(ix
: usize) -> Self { ix as u8 }
44 fn to_usize(self) -> usize { self as usize }
46 fn from(ix
: usize) -> Self { ix as u16 }
51 fn to_usize(self) -> usize { self as usize }
53 fn from(ix
: usize) -> Self { ix as u32 }
56 impl Index
for usize {
58 fn to_usize(self) -> usize { self }
60 fn from(ix
: usize) -> Self { ix }
63 macro_rules
! fix_array_impl
{
64 ($index_type
:ty
, $len
:expr
) => (
65 unsafe impl<T
> Array
for [T
; $len
] {
67 type Index
= $index_type
;
69 fn as_ptr(&self) -> *const T { self as *const _ as *const _ }
71 fn as_mut_ptr(&mut self) -> *mut T { self as *mut _ as *mut _}
73 fn capacity() -> usize { $len }
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
,)*);
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,);