1use generic_array::{ArrayLength, GenericArray};
7use std::fmt;
8use std::marker::Sized;
9
10#[derive(Debug, Copy, Clone)]
13pub struct TryFromSizedBytesError(());
14
15impl fmt::Display for TryFromSizedBytesError {
16 #[inline]
17 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18 fmt::Display::fmt(self.__description(), f)
19 }
20}
21
22impl TryFromSizedBytesError {
23 pub fn new() -> TryFromSizedBytesError {
24 TryFromSizedBytesError(())
25 }
26
27 #[inline]
28 #[doc(hidden)]
29 pub fn __description(&self) -> &str {
30 "could not convert from generic byte slice"
31 }
32}
33
34impl Default for TryFromSizedBytesError {
35 fn default() -> TryFromSizedBytesError {
36 TryFromSizedBytesError::new()
37 }
38}
39
40pub trait SizedBytes: Sized {
45 type Len: ArrayLength<u8> + 'static;
47
48 fn to_arr(&self) -> GenericArray<u8, Self::Len>;
52
53 fn from_arr(arr: &GenericArray<u8, Self::Len>) -> Result<Self, TryFromSizedBytesError>;
55}
56
57impl<N: ArrayLength<u8> + 'static> SizedBytes for GenericArray<u8, N> {
59 type Len = N;
60
61 fn to_arr(&self) -> GenericArray<u8, Self::Len> {
62 self.clone()
63 }
64 fn from_arr(arr: &GenericArray<u8, Self::Len>) -> Result<Self, TryFromSizedBytesError> {
65 Ok(arr.clone())
66 }
67}