rust_assembler/
stack_str.rs1use core::{
2 ops::{ Index, IndexMut, Range, RangeFrom, RangeTo, RangeFull, RangeInclusive, RangeToInclusive },
3 slice
4};
5
6pub mod usize_to_str;
7
8
9#[derive(Debug)]
10pub struct StackStr<const N: usize>
11{
12 pub bytes: [u8; N],
13 pub str_: *const str
14}
15
16impl<'a, const N: usize> StackStr<N>
17{
18
19 pub fn str(&self) -> &str {
20 unsafe {
21 &*self.str_
22 }
23 }
24
25}
26
27impl<'a, const N: usize> Index<usize> for StackStr<N>
28{
29 type Output = u8;
30
31 fn index(&self, index: usize) -> &Self::Output {
32 unsafe {
33 let ptr = self.bytes.as_ptr().add(index);
34 &*ptr
35 }
36 }
37}
38impl<'a, const N: usize> IndexMut<usize> for StackStr<N> {
39
40 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
41 unsafe {
42 let ptr = self.bytes.as_mut_ptr().add(index);
43 &mut *ptr
44 }
45 }
46}
47
48
49
50impl<'a, const N: usize> Index<Range<usize>> for StackStr<N>
51{
52 type Output = [u8];
53
54 fn index(&self, index: Range<usize>) -> &Self::Output {
55 unsafe {
56 let start = self.bytes.as_ptr().add(index.start);
57 let len = index.end - index.start;
58
59 slice::from_raw_parts(start, len)
60 }
61 }
62}
63impl<'a, const N: usize> Index<RangeFrom<usize>> for StackStr<N>
64{
65 type Output = [u8];
66
67 fn index(&self, index: RangeFrom<usize>) -> &Self::Output {
68 unsafe {
69 let start = self.bytes.as_ptr().add(index.start);
70 let len = self.bytes.len() - index.start;
71
72 slice::from_raw_parts(start, len)
73 }
74 }
75}
76impl<'a, const N: usize> Index<RangeTo<usize>> for StackStr<N>
77{
78 type Output = [u8];
79
80 fn index(&self, index: RangeTo<usize>) -> &Self::Output {
81 unsafe {
82 let start = self.bytes.as_ptr();
83 let len = index.end;
84
85 slice::from_raw_parts(start, len)
86 }
87 }
88}
89impl<'a, const N: usize> Index<RangeFull> for StackStr<N>
90{
91 type Output = [u8];
92
93 fn index(&self, _index: RangeFull) -> &Self::Output {
94 unsafe {
95 let start = self.bytes.as_ptr();
96 let len = self.bytes.len();
97
98 slice::from_raw_parts(start, len)
99 }
100 }
101}