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
use core::{
    ops::{ Index, IndexMut, Range, RangeFrom, RangeTo, RangeFull, RangeInclusive, RangeToInclusive },
    slice,
};

pub mod usize_to_stack_str;


#[derive(Debug)]
pub struct StackStr<const N: usize>
{
    pub bytes: [u8; N],
    pub slice: Range<usize>,
}

impl<'a, const N: usize> StackStr<N>
{

    pub fn from(bytes: [u8; N], slice: Range<usize>) -> Self {
        Self {
            bytes,
            slice
        }
    }

    pub fn str(&self) -> &str {
        unsafe {
            core::str::from_utf8_unchecked(
                &self[self.slice.start..self.slice.end]
            )
        }
    }

}

impl<'a, const N: usize> Index<usize> for StackStr<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<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<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<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<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<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)
        }
    }
}