| 1 |
//! Implements actual generic array abstraction for all supported types from `typenum` (1 to 255) |
| 2 |
|
| 3 |
use typenum::*; |
| 4 |
|
| 5 |
macro_rules! impl_generic_array { |
| 6 |
($($type: ty),*) => { |
| 7 |
$( |
| 8 |
impl private::Sealed for $type {} |
| 9 |
impl Capacity for $type { |
| 10 |
type Array = [u8; Self::USIZE]; |
| 11 |
} |
| 12 |
|
| 13 |
impl private::Sealed for <$type as Capacity>::Array {} |
| 14 |
impl ArraySlice for <$type as Capacity>::Array { |
| 15 |
const CAPACITY: usize = <$type as Unsigned>::USIZE; |
| 16 |
|
| 17 |
#[inline] |
| 18 |
fn as_slice(&self) -> &[u8] { |
| 19 |
self |
| 20 |
} |
| 21 |
|
| 22 |
#[inline] |
| 23 |
unsafe fn as_mut_slice(&mut self) -> &mut [u8] { |
| 24 |
self |
| 25 |
} |
| 26 |
|
| 27 |
#[inline] |
| 28 |
fn zeroed() -> Self { |
| 29 |
[0; Self::CAPACITY] |
| 30 |
} |
| 31 |
} |
| 32 |
)* |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/// Private module to hide access to sealed trait |
| 37 |
mod private { |
| 38 |
/// Trait impossible to be implemented outside of this crate, seals other traits |
| 39 |
pub trait Sealed {} |
| 40 |
} |
| 41 |
|
| 42 |
/// Implements needed types for all types of arrays (bigger than 32 don't have the default traits) |
| 43 |
#[doc(hidden)] |
| 44 |
pub trait ArraySlice: private::Sealed { |
| 45 |
/// Capacity represented by type |
| 46 |
const CAPACITY: usize; |
| 47 |
|
| 48 |
/// Returns slice of the entire array |
| 49 |
fn as_slice(&self) -> &[u8]; |
| 50 |
/// Returns mutable slice of the entire array |
| 51 |
unsafe fn as_mut_slice(&mut self) -> &mut [u8]; |
| 52 |
/// Returns array filled with zeroes |
| 53 |
fn zeroed() -> Self; |
| 54 |
} |
| 55 |
|
| 56 |
/// Converts between `typenum` types and its corresponding array |
| 57 |
#[doc(hidden)] |
| 58 |
pub trait Capacity: Unsigned + private::Sealed { |
| 59 |
/// Array with specified capacity |
| 60 |
type Array: ArraySlice + Copy; |
| 61 |
} |
| 62 |
|
| 63 |
impl_generic_array!( |
| 64 |
U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, U19, U20, U21, |
| 65 |
U22, U23, U24, U25, U26, U27, U28, U29, U30, U31, U32, U33, U34, U35, U36, U37, U38, U39, U40, |
| 66 |
U41, U42, U43, U44, U45, U46, U47, U48, U49, U50, U51, U52, U53, U54, U55, U56, U57, U58, U59, |
| 67 |
U60, U61, U62, U63, U64, U65, U66, U67, U68, U69, U70, U71, U72, U73, U74, U75, U76, U77, U78, |
| 68 |
U79, U80, U81, U82, U83, U84, U85, U86, U87, U88, U89, U90, U91, U92, U93, U94, U95, U96, U97, |
| 69 |
U98, U99, U100, U101, U102, U103, U104, U105, U106, U107, U108, U109, U110, U111, U112, U113, |
| 70 |
U114, U115, U116, U117, U118, U119, U120, U121, U122, U123, U124, U125, U126, U127, U128, U129, |
| 71 |
U130, U131, U132, U133, U134, U135, U136, U137, U138, U139, U140, U141, U142, U143, U144, U145, |
| 72 |
U146, U147, U148, U149, U150, U151, U152, U153, U154, U155, U156, U157, U158, U159, U160, U161, |
| 73 |
U162, U163, U164, U165, U166, U167, U168, U169, U170, U171, U172, U173, U174, U178, U179, U180, |
| 74 |
U181, U182, U183, U184, U185, U186, U187, U188, U189, U190, U191, U192, U193, U194, U195, U196, |
| 75 |
U197, U198, U199, U200, U201, U202, U203, U204, U205, U206, U207, U208, U209, U210, U211, U212, |
| 76 |
U213, U214, U215, U216, U217, U218, U219, U220, U221, U222, U223, U224, U225, U226, U227, U228, |
| 77 |
U229, U230, U231, U232, U233, U234, U235, U236, U237, U238, U239, U240, U241, U242, U243, U244, |
| 78 |
U245, U246, U247, U248, U249, U250, U251, U252, U253, U254, U255 |
| 79 |
); |