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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::{
ConstraintViolation,
type_math::{ TypeNum, Operator }
};
use std::{
convert::TryFrom, error::Error, marker::PhantomData, slice::SliceIndex,
ops::{ Deref, DerefMut, Index, IndexMut }
};
macro_rules! impl_conv {
($type:ty where $($impl_args:tt)*) => {
impl<$($impl_args)*> $type {
pub fn len(&self) -> usize {
self.slice.len()
}
pub fn slice(&self) -> &[T] {
self.slice
}
pub fn slice_mut(&mut self) -> &mut[T] {
self.slice
}
}
impl<$($impl_args)*> Into<&'a[T]> for $type {
fn into(self) -> &'a[T] {
self.slice
}
}
impl<$($impl_args)*> Into<&'a mut[T]> for $type {
fn into(self) -> &'a mut[T] {
self.slice
}
}
impl<$($impl_args)*> Deref for $type {
type Target = [T];
fn deref(&self) -> &Self::Target {
self.slice
}
}
impl<$($impl_args)*> DerefMut for $type {
fn deref_mut(&mut self) -> &mut Self::Target {
self.slice
}
}
impl<$($impl_args)*> AsRef<[T]> for $type {
fn as_ref(&self) -> &[T] {
self.slice
}
}
impl<$($impl_args)*> AsMut<[T]> for $type {
fn as_mut(&mut self) -> &mut[T] {
self.slice
}
}
impl<$($impl_args)*, I: SliceIndex<[T]>> Index<I> for $type {
type Output = I::Output;
fn index(&self, index: I) -> &Self::Output {
&self.slice[index]
}
}
impl<$($impl_args)*, I: SliceIndex<[T]>> IndexMut<I> for $type {
fn index_mut(&mut self, index: I) -> &mut Self::Output {
&mut self.slice[index]
}
}
};
}
#[derive(Debug)]
pub struct FixedMut<'a, T, Val: TypeNum> {
slice: &'a mut[T],
constraint: PhantomData<Val>
}
impl<'a, T, Val: TypeNum> TryFrom<&'a mut[T]> for FixedMut<'a, T, Val> {
type Error = Box<Error + 'static>;
fn try_from(slice: &'a mut[T]) -> Result<Self, Self::Error> {
match slice.len() {
len if len == Val::VALUE => Ok(Self{ slice, constraint: PhantomData }),
len => Err(ConstraintViolation::fixed::<Val>(len))?
}
}
}
impl_conv!(FixedMut<'a, T, Val> where 'a, T, Val: TypeNum);
#[derive(Debug)]
pub struct RangedMut<'a, T, Start: TypeNum, End: TypeNum> {
slice: &'a mut[T],
constraint: PhantomData<(Start, End)>
}
impl<'a, T, Start: TypeNum, End: TypeNum> TryFrom<&'a mut[T]> for RangedMut<'a, T, Start, End> {
type Error = Box<Error + 'static>;
fn try_from(slice: &'a mut[T]) -> Result<Self, Self::Error> {
match slice.len() {
len if len >= Start::VALUE && len < End::VALUE =>
Ok(Self{ slice, constraint: PhantomData }),
len => Err(ConstraintViolation::ranged::<Start, End>(len))?
}
}
}
impl_conv!(RangedMut<'a, T, Start, End> where 'a, T, Start: TypeNum, End: TypeNum);
#[derive(Debug)]
pub struct RelativeMut<'a, T, Op: Operator, By: TypeNum> {
slice: &'a mut[T],
constraint: PhantomData<(Op, By)>
}
impl<'a, T, Op: Operator, By: TypeNum> RelativeMut<'a, T, Op, By> {
pub fn try_from(slice: &'a mut[T], relative_to: usize) -> Result<Self, Box<Error + 'static>> {
Self::validate(slice.len(), relative_to)?;
Ok(Self::from(slice))
}
pub fn slice(self, relative_to: usize) -> Result<&'a[T], Box<Error + 'static>> {
Self::validate(self.slice.len(), relative_to)?;
Ok(self.slice)
}
pub fn slice_mut(self, relative_to: usize) -> Result<&'a mut[T], Box<Error + 'static>> {
Self::validate(self.slice.len(), relative_to)?;
Ok(self.slice)
}
fn validate(len: usize, relative_to: usize) -> Result<(), Box<Error + 'static>> {
let expected = Op::r#do(relative_to, By::VALUE)?;
match len == expected {
true => Ok(()),
false => Err(ConstraintViolation::relative::<Op, By>(len, relative_to))?
}
}
}
impl<'a, T, Op: Operator, By: TypeNum> From<&'a mut[T]> for RelativeMut<'a, T, Op, By> {
fn from(slice: &'a mut[T]) -> Self {
RelativeMut{ slice, constraint: PhantomData }
}
}