boxed_slice/
lib.rs

1/*
2	Copyright (c) 2020 aspen <aspenuwu@protonmail.com>
3	This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
4	Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
5		1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
6		2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
7		3. This notice may not be removed or altered from any source distribution.
8*/
9
10#![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
28/// A boxed slice, with a compile-time constant size.
29/// ```rust
30///  use boxed_slice::BoxedSlice;
31///  
32///  let answer_slice: BoxedSlice<u8, 42> = BoxedSlice::new(42);
33///
34///  assert_eq!(answer_slice.len(), 42);
35///  assert!(answer_slice.iter().all(|num| *num == 42));
36///
37///  let default_slice: BoxedSlice<u8, 128> = BoxedSlice::default();
38///
39///  assert_eq!(default_slice.len(), 128);
40///  assert!(default_slice.iter().all(|num| *num == u8::default()));
41/// ```
42pub struct BoxedSlice<T, const N: usize>(Box<[T; N]>);
43
44impl<T, const N: usize> BoxedSlice<T, N> {
45	/// Create a new [`BoxedSlice`], filling it with the
46	/// given initial value.
47	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}