@@ -3,28 +3,9 @@ use core::ops::Deref;
3
3
4
4
5
5
6
- /// Our own `core::array::TryFromSliceError`
7
- ///
8
- /// Used in [`array_ref_try_from_slice`].
9
- // We need a local copy of this type, because we need to instantiate it, but it
10
- // has a private field.
11
- struct TryFromSliceError ( ( ) ) ;
6
+ mod from_slice;
7
+
12
8
13
- /// Const version of `<&[T; N]>::try_from(&[T])`
14
- ///
15
- /// Original Source:
16
- /// https://github.com/rust-lang/rust/blob/eb82facb1626166188d49599a3313fc95201f556/library/core/src/array/mod.rs#L203-L215
17
- const fn array_ref_try_from_slice < ' a , T , const N : usize > (
18
- slice : & [ T ] ,
19
- ) -> Result < & [ T ; N ] , TryFromSliceError > {
20
- if slice. len ( ) == N {
21
- let ptr = slice. as_ptr ( ) as * const [ T ; N ] ;
22
- // SAFETY: ok because we just checked that the length fits
23
- unsafe { Ok ( & * ptr) }
24
- } else {
25
- Err ( TryFromSliceError ( ( ) ) )
26
- }
27
- }
28
9
29
10
/// A string stored as byte array.
30
11
///
@@ -56,8 +37,8 @@ const fn array_ref_try_from_slice<'a, T, const N: usize>(
56
37
/// let text: &str = &text_buffer; // Just derefs to `str`
57
38
/// assert_eq!(text, "dai 大賢者 kenja")
58
39
/// ```
59
-
60
40
#[ derive( Debug , Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
41
+ #[ repr( transparent) ]
61
42
pub struct ByteString < const N : usize > ( pub [ u8 ; N ] ) ;
62
43
63
44
impl < const N : usize > ByteString < N > {
@@ -68,7 +49,7 @@ impl<const N: usize> ByteString<N> {
68
49
69
50
/// Wraps the given byte slice
70
51
pub const fn from_bytes ( bytes : & [ u8 ] ) -> Option < Self > {
71
- let res = array_ref_try_from_slice ( bytes) ;
52
+ let res = from_slice :: array_ref_try_from_slice ( bytes) ;
72
53
73
54
match res {
74
55
Ok ( array) => Some ( Self ( * array) ) ,
0 commit comments