1#![feature(min_const_generics)]
11#![no_std]
12
13extern crate alloc;
14
15use alloc::boxed::Box;
16use core::{
17 convert::{AsMut, AsRef},
18 fmt::{self, Debug},
19 hash::{Hash, Hasher},
20 ops::{Deref, DerefMut},
21};
22
23#[cfg(feature = "serde")]
24pub mod deserialize;
25#[cfg(feature = "serde")]
26pub mod serialize;
27
28pub struct BoxedSlice<T, const N: usize>(Box<[T; N]>);
43
44impl<T, const N: usize> BoxedSlice<T, N> {
45 pub fn new(initial_value: T) -> Self
48 where
49 T: Copy,
50 {
51 Self(Box::new([initial_value; N]))
52 }
53}
54
55impl<T, const N: usize> Default for BoxedSlice<T, N>
56where
57 T: Default + Copy,
58{
59 fn default() -> Self {
60 Self(Box::new([T::default(); N]))
61 }
62}
63
64impl<T, F, const N: usize> From<F> for BoxedSlice<T, N>
65where
66 F: Into<[T; N]>,
67{
68 fn from(src: F) -> Self {
69 Self(Box::new(src.into()))
70 }
71}
72
73impl<T, const N: usize> AsRef<[T]> for BoxedSlice<T, N> {
74 fn as_ref(&self) -> &[T] {
75 self.0.as_ref()
76 }
77}
78
79impl<T, const N: usize> AsMut<[T]> for BoxedSlice<T, N> {
80 fn as_mut(&mut self) -> &mut [T] {
81 self.0.as_mut()
82 }
83}
84
85impl<T, const N: usize> Deref for BoxedSlice<T, N> {
86 type Target = Box<[T; N]>;
87
88 fn deref(&self) -> &Self::Target {
89 &self.0
90 }
91}
92
93impl<T, const N: usize> DerefMut for BoxedSlice<T, N> {
94 fn deref_mut(&mut self) -> &mut Self::Target {
95 &mut self.0
96 }
97}
98
99impl<T, const N: usize> Clone for BoxedSlice<T, N>
100where
101 T: Clone,
102{
103 fn clone(&self) -> Self {
104 Self(self.0.clone())
105 }
106}
107
108impl<T, const N: usize> PartialEq for BoxedSlice<T, N>
109where
110 T: PartialEq,
111{
112 fn eq(&self, other: &Self) -> bool {
113 self.0.eq(&other.0)
114 }
115}
116
117impl<T, const N: usize> Eq for BoxedSlice<T, N> where T: Eq {}
118
119impl<T, const N: usize> Hash for BoxedSlice<T, N>
120where
121 T: Hash,
122{
123 fn hash<H: Hasher>(&self, state: &mut H) {
124 self.0.hash(state)
125 }
126}
127
128impl<T, const N: usize> Debug for BoxedSlice<T, N>
129where
130 T: Debug,
131{
132 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133 Debug::fmt(&self.0, f)
134 }
135}