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
/*
	Copyright (c) 2020 aspen <aspenuwu@protonmail.com>
	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.
	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:
		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.
		2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
		3. This notice may not be removed or altered from any source distribution.
*/

#![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;

/// A boxed slice, with a compile-time constant size.
/// ```rust
///  use boxed_slice::BoxedSlice;
///  
///  let answer_slice: BoxedSlice<u8, 42> = BoxedSlice::new(42);
///
///  assert_eq!(answer_slice.len(), 42);
///  assert!(answer_slice.iter().all(|num| *num == 42));
///
///  let default_slice: BoxedSlice<u8, 128> = BoxedSlice::default();
///
///  assert_eq!(default_slice.len(), 128);
///  assert!(default_slice.iter().all(|num| *num == u8::default()));
/// ```
pub struct BoxedSlice<T, const N: usize>(Box<[T; N]>);

impl<T, const N: usize> BoxedSlice<T, N> {
	/// Create a new [`BoxedSlice`], filling it with the
	/// given initial value.
	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)
	}
}