1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::mem::MaybeUninit;
#[inline(always)]
pub const fn unlikely(b: bool) -> bool {
#[allow(clippy::needless_bool)]
if (1i32).checked_div(if b { 0 } else { 1 }).is_none() {
true
} else {
false
}
}
#[derive(Clone, Copy)]
pub struct DisplayHash<'a>(pub &'a [u8; 32]);
impl std::fmt::Display for DisplayHash<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut output = [0u8; 64];
hex::encode_to_slice(self.0, &mut output).ok();
let output = unsafe { std::str::from_utf8_unchecked(&output) };
f.write_str(output)
}
}
impl std::fmt::Debug for DisplayHash<'_> {
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
}
}
pub struct ArrayVec<T, const N: usize> {
inner: [MaybeUninit<T>; N],
len: u8,
}
impl<T, const N: usize> ArrayVec<T, N> {
const _ASSERT_LEN: () = assert!(N <= u8::MAX as usize);
#[inline]
pub fn len(&self) -> usize {
self.len as usize
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
}
#[inline]
pub unsafe fn push(&mut self, item: T) {
debug_assert!((self.len as usize) < N);
*self.inner.get_unchecked_mut(self.len as usize) = MaybeUninit::new(item);
self.len += 1;
}
#[inline]
pub unsafe fn into_inner(self) -> [MaybeUninit<T>; N] {
let this = std::mem::ManuallyDrop::new(self);
std::ptr::read(&this.inner)
}
}
impl<T, const N: usize> Default for ArrayVec<T, N> {
#[inline]
fn default() -> Self {
Self {
inner: unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() },
len: 0,
}
}
}
impl<R, const N: usize> AsRef<[R]> for ArrayVec<R, N> {
#[inline]
fn as_ref(&self) -> &[R] {
unsafe { std::slice::from_raw_parts(self.inner.as_ptr() as *const R, self.len as usize) }
}
}
impl<T: Clone, const N: usize> Clone for ArrayVec<T, N> {
fn clone(&self) -> Self {
let mut res = Self::default();
for item in self.as_ref() {
unsafe { res.push(item.clone()) };
}
res
}
}
impl<T, const N: usize> Drop for ArrayVec<T, N> {
fn drop(&mut self) {
debug_assert!(self.len as usize <= N);
let references_ptr = self.inner.as_mut_ptr() as *mut T;
for i in 0..self.len {
unsafe { std::ptr::drop_in_place(references_ptr.add(i as usize)) };
}
}
}