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
use core::{
ops::{ Index, IndexMut, Range, RangeFrom, RangeTo, RangeFull, RangeInclusive, RangeToInclusive },
slice
};
pub mod usize_to_str;
#[derive(Debug)]
pub struct StackStr<'a, const N: usize>
{
pub bytes: [u8; N],
pub str_: &'a str
}
impl<'a, const N: usize> StackStr<'a, N>
{
pub fn str(&self) -> &str {
self.str()
}
}
impl<'a, const N: usize> Index<usize> for StackStr<'a, N>
{
type Output = u8;
fn index(&self, index: usize) -> &Self::Output {
unsafe {
let ptr = self.bytes.as_ptr().add(index);
&*ptr
}
}
}
impl<'a, const N: usize> IndexMut<usize> for StackStr<'a, N> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
unsafe {
let ptr = self.bytes.as_mut_ptr().add(index);
&mut *ptr
}
}
}
impl<'a, const N: usize> Index<Range<usize>> for StackStr<'a, N>
{
type Output = [u8];
fn index(&self, index: Range<usize>) -> &Self::Output {
unsafe {
let start = self.bytes.as_ptr().add(index.start);
let len = index.end - index.start;
slice::from_raw_parts(start, len)
}
}
}
impl<'a, const N: usize> Index<RangeFrom<usize>> for StackStr<'a, N>
{
type Output = [u8];
fn index(&self, index: RangeFrom<usize>) -> &Self::Output {
unsafe {
let start = self.bytes.as_ptr().add(index.start);
let len = self.bytes.len() - index.start;
slice::from_raw_parts(start, len)
}
}
}
impl<'a, const N: usize> Index<RangeTo<usize>> for StackStr<'a, N>
{
type Output = [u8];
fn index(&self, index: RangeTo<usize>) -> &Self::Output {
unsafe {
let start = self.bytes.as_ptr();
let len = index.end;
slice::from_raw_parts(start, len)
}
}
}
impl<'a, const N: usize> Index<RangeFull> for StackStr<'a, N>
{
type Output = [u8];
fn index(&self, index: RangeFull) -> &Self::Output {
unsafe {
let start = self.bytes.as_ptr();
let len = self.bytes.len();
slice::from_raw_parts(start, len)
}
}
}