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
126
127
128
129
130
131
132
133
134
135
#![feature(min_const_generics)]
#![no_std]
extern crate alloc;
use alloc::boxed::Box;
use core::{
convert::{AsMut, AsRef},
fmt::{self, Debug},
hash::{Hash, Hasher},
ops::{Deref, DerefMut},
};
#[cfg(feature = "serde")]
pub mod deserialize;
#[cfg(feature = "serde")]
pub mod serialize;
pub struct BoxedSlice<T, const N: usize>(Box<[T; N]>);
impl<T, const N: usize> BoxedSlice<T, N> {
pub fn new(initial_value: T) -> Self
where
T: Copy,
{
Self(Box::new([initial_value; N]))
}
}
impl<T, const N: usize> Default for BoxedSlice<T, N>
where
T: Default + Copy,
{
fn default() -> Self {
Self(Box::new([T::default(); N]))
}
}
impl<T, F, const N: usize> From<F> for BoxedSlice<T, N>
where
F: Into<[T; N]>,
{
fn from(src: F) -> Self {
Self(Box::new(src.into()))
}
}
impl<T, const N: usize> AsRef<[T]> for BoxedSlice<T, N> {
fn as_ref(&self) -> &[T] {
self.0.as_ref()
}
}
impl<T, const N: usize> AsMut<[T]> for BoxedSlice<T, N> {
fn as_mut(&mut self) -> &mut [T] {
self.0.as_mut()
}
}
impl<T, const N: usize> Deref for BoxedSlice<T, N> {
type Target = Box<[T; N]>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T, const N: usize> DerefMut for BoxedSlice<T, N> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T, const N: usize> Clone for BoxedSlice<T, N>
where
T: Clone,
{
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T, const N: usize> PartialEq for BoxedSlice<T, N>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}
impl<T, const N: usize> Eq for BoxedSlice<T, N> where T: Eq {}
impl<T, const N: usize> Hash for BoxedSlice<T, N>
where
T: Hash,
{
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state)
}
}
impl<T, const N: usize> Debug for BoxedSlice<T, N>
where
T: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}