Skip to main content

rumtk_arena/mem/
traits.rs

1/*
2 *     rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 *     This toolkit aims to be reliable, simple, performant, and standards compliant.
4 *     Copyright (C) 2026  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 *     Copyright (C) 2026  MedicalMasses L.L.C. <contact@medicalmasses.com>
6 *
7 *     This program is free software: you can redistribute it and/or modify
8 *     it under the terms of the GNU General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 *
12 *     This program is distributed in the hope that it will be useful,
13 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *     GNU General Public License for more details.
16 *
17 *     You should have received a copy of the GNU General Public License
18 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20
21use crate::base::*;
22
23pub trait AsRUMPtr {
24    fn as_rum_ptr(&self) -> *const u8;
25    fn as_mut_rum_ptr(&mut self) -> *mut u8;
26}
27
28pub trait SizedRUMType {
29    fn size(&self) -> usize;
30}
31
32impl SizedRUMType for [u8] { fn size(&self) -> usize { self.len() } }
33impl SizedRUMType for &[u8] { fn size(&self) -> usize { self.len() } }
34impl SizedRUMType for &mut [u8] { fn size(&self) -> usize { self.len() } }
35impl SizedRUMType for &&[u8] { fn size(&self) -> usize { self.len() } }
36impl SizedRUMType for &mut &mut [u8] { fn size(&self) -> usize { self.len() } }
37impl SizedRUMType for RUMVec<u8> { fn size(&self) -> usize { self.len() } }
38impl SizedRUMType for &RUMVec<u8> { fn size(&self) -> usize { self.len() } }
39impl SizedRUMType for &mut RUMVec<u8> { fn size(&self) -> usize { self.len() } }
40impl SizedRUMType for && RUMVec<u8> { fn size(&self) -> usize { self.len() } }
41impl SizedRUMType for &mut &mut RUMVec<u8> { fn size(&self) -> usize { self.len() } }
42impl SizedRUMType for RUMString { fn size(&self) -> usize { self.len() } }
43impl SizedRUMType for &RUMString { fn size(&self) -> usize { self.len() } }
44impl SizedRUMType for &mut RUMString { fn size(&self) -> usize { self.len() } }
45impl SizedRUMType for &&RUMString { fn size(&self) -> usize { self.len() } }
46impl SizedRUMType for &mut &mut RUMString { fn size(&self) -> usize { self.len() } }
47impl SizedRUMType for &str { fn size(&self) -> usize { self.len() } }
48impl SizedRUMType for &&str { fn size(&self) -> usize { self.len() } }
49impl SizedRUMType for &mut &mut str { fn size(&self) -> usize { self.len() } }
50
51pub trait AsRUMSlice: AsRUMPtr + SizedRUMType {
52    #[inline(always)]
53    fn as_rum_slice(&self) -> &[u8] { as_slice(self.as_rum_ptr(),  self.size()) }
54    #[inline(always)]
55    fn as_rum_slice_mut(&mut self) -> &mut [u8] {  as_slice_mut(self.as_mut_rum_ptr(),  self.size()) }
56
57    #[inline(always)]
58    fn contains(&self, x: &u8) -> bool {
59        self.as_rum_slice().contains(x)
60    }
61}
62
63#[inline]
64pub fn as_slice<'a>(src: *const u8, size: usize) -> &'a [u8] {
65    unsafe { std::slice::from_raw_parts(src, size) }
66}
67
68#[inline]
69pub fn as_slice_mut<'a>(src: *mut u8, size: usize) -> &'a mut [u8] {
70    unsafe { std::slice::from_raw_parts_mut(src, size) }
71}
72
73/********************** Basic Impl for AsSlice************************/
74impl AsRUMPtr for RUMVec<u8> {
75    #[inline(always)]
76    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
77    #[inline(always)]
78    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
79}
80impl AsRUMPtr for &RUMVec<u8> {
81    #[inline(always)]
82    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
83    #[inline(always)]
84    fn as_mut_rum_ptr(&mut self) -> *mut u8 { panic!("Asked for mutable pointer to &RUMVec<u8> which is immutable!") }
85}
86impl AsRUMPtr for &mut RUMVec<u8> {
87    #[inline(always)]
88    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
89    #[inline(always)]
90    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
91}
92impl AsRUMPtr for &&RUMVec<u8> {
93    #[inline(always)]
94    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
95    #[inline(always)]
96    fn as_mut_rum_ptr(&mut self) -> *mut u8 { panic!("Asked for mutable pointer to &RUMVec<u8> which is immutable!") }
97}
98impl AsRUMPtr for &mut &mut RUMVec<u8> {
99    #[inline(always)]
100    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
101    #[inline(always)]
102    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
103}
104impl AsRUMPtr for &[u8] {
105    #[inline(always)]
106    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
107    #[inline(always)]
108    fn as_mut_rum_ptr(&mut self) -> *mut u8 { panic!("Asked for mutable pointer to &[u8] which is immutable!") }
109}
110impl AsRUMPtr for &mut [u8] {
111    #[inline(always)]
112    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
113    #[inline(always)]
114    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
115}
116impl AsRUMPtr for &&[u8] {
117    #[inline(always)]
118    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
119    #[inline(always)]
120    fn as_mut_rum_ptr(&mut self) -> *mut u8 { panic!("Asked for mutable pointer to &[u8] which is immutable!") }
121}
122impl AsRUMPtr for &mut &mut [u8] {
123    #[inline(always)]
124    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
125    #[inline(always)]
126    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
127}
128impl AsRUMPtr for RUMString {
129    #[inline(always)]
130    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
131    #[inline(always)]
132    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
133}
134impl AsRUMPtr for &RUMString {
135    #[inline(always)]
136    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
137    #[inline(always)]
138    fn as_mut_rum_ptr(&mut self) -> *mut u8 { panic!("Asked for mutable pointer to &RUMString which is immutable!") }
139}
140impl AsRUMPtr for &mut RUMString {
141    #[inline(always)]
142    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
143    #[inline(always)]
144    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
145}
146impl AsRUMPtr for &&RUMString {
147    #[inline(always)]
148    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
149    #[inline(always)]
150    fn as_mut_rum_ptr(&mut self) -> *mut u8 { panic!("Asked for mutable pointer to &RUMString which is immutable!") }
151}
152impl AsRUMPtr for &mut &mut RUMString {
153    #[inline(always)]
154    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
155    #[inline(always)]
156    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
157}
158impl AsRUMPtr for &str {
159    #[inline(always)]
160    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
161    #[inline(always)]
162    fn as_mut_rum_ptr(&mut self) -> *mut u8 { panic!("Asked for mutable pointer to &str which is immutable!") }
163}
164impl AsRUMPtr for &&str {
165    #[inline(always)]
166    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
167    #[inline(always)]
168    fn as_mut_rum_ptr(&mut self) -> *mut u8 { panic!("Asked for mutable pointer to &str which is immutable!") }
169}
170impl AsRUMPtr for &mut str {
171    #[inline(always)]
172    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
173    #[inline(always)]
174    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
175}
176impl AsRUMPtr for &mut &mut str {
177    #[inline(always)]
178    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
179    #[inline(always)]
180    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
181}
182
183/********************** Basic Impl for AsSlice************************/
184impl AsRUMSlice for RUMVec<u8> {}
185impl AsRUMSlice for &RUMVec<u8> {}
186impl AsRUMSlice for &&RUMVec<u8> {}
187impl AsRUMSlice for &mut &mut RUMVec<u8> {}
188impl AsRUMSlice for &[u8] {}
189impl AsRUMSlice for &mut [u8] {}
190impl AsRUMSlice for &&[u8] {}
191impl AsRUMSlice for &mut &mut [u8] {}
192impl AsRUMSlice for RUMString {}
193impl AsRUMSlice for &RUMString {}
194impl AsRUMSlice for &&RUMString {}
195impl AsRUMSlice for &mut &mut RUMString {}
196impl AsRUMSlice for &str {}
197impl AsRUMSlice for &&str {}
198impl AsRUMSlice for &mut &mut str {}