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 RUMVec<u8> { fn size(&self) -> usize { self.len() } }
35impl SizedRUMType for &RUMVec<u8> { fn size(&self) -> usize { self.len() } }
36impl SizedRUMType for RUMString { fn size(&self) -> usize { self.len() } }
37impl SizedRUMType for &RUMString { fn size(&self) -> usize { self.len() } }
38impl SizedRUMType for &str { fn size(&self) -> usize { self.len() } }
39
40pub trait AsRUMSlice: AsRUMPtr + SizedRUMType {
41    #[inline(always)]
42    fn as_rum_slice(&self) -> &[u8] { as_slice(self.as_rum_ptr(),  self.size()) }
43    #[inline(always)]
44    fn as_rum_slice_mut(&mut self) -> &mut [u8] {  as_slice_mut(self.as_mut_rum_ptr(),  self.size()) }
45
46    #[inline(always)]
47    fn contains(&self, x: &u8) -> bool {
48        self.as_rum_slice().contains(x)
49    }
50}
51
52#[inline]
53pub fn as_slice<'a>(src: *const u8, size: usize) -> &'a [u8] {
54    unsafe { std::slice::from_raw_parts(src, size) }
55}
56
57#[inline]
58pub fn as_slice_mut<'a>(src: *mut u8, size: usize) -> &'a mut [u8] {
59    unsafe { std::slice::from_raw_parts_mut(src, size) }
60}
61
62/********************** Basic Impl for AsSlice************************/
63impl AsRUMPtr for RUMVec<u8> {
64    #[inline(always)]
65    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
66    #[inline(always)]
67    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
68}
69impl AsRUMPtr for &RUMVec<u8> {
70    #[inline(always)]
71    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
72    #[inline(always)]
73    fn as_mut_rum_ptr(&mut self) -> *mut u8 { panic!("Asked for mutable pointer to &RUMVec<u8> which is immutable!") }
74}
75impl AsRUMPtr for &mut RUMVec<u8> {
76    #[inline(always)]
77    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
78    #[inline(always)]
79    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
80}
81impl AsRUMPtr for &[u8] {
82    #[inline(always)]
83    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
84    #[inline(always)]
85    fn as_mut_rum_ptr(&mut self) -> *mut u8 { panic!("Asked for mutable pointer to &[u8] which is immutable!") }
86}
87impl AsRUMPtr for &mut [u8] {
88    #[inline(always)]
89    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
90    #[inline(always)]
91    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
92}
93impl AsRUMPtr for RUMString {
94    #[inline(always)]
95    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
96    #[inline(always)]
97    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
98}
99impl AsRUMPtr for &RUMString {
100    #[inline(always)]
101    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
102    #[inline(always)]
103    fn as_mut_rum_ptr(&mut self) -> *mut u8 { panic!("Asked for mutable pointer to &RUMString which is immutable!") }
104}
105impl AsRUMPtr for &mut RUMString {
106    #[inline(always)]
107    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
108    #[inline(always)]
109    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
110}
111impl AsRUMPtr for &str {
112    #[inline(always)]
113    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
114    #[inline(always)]
115    fn as_mut_rum_ptr(&mut self) -> *mut u8 { panic!("Asked for mutable pointer to &str which is immutable!") }
116}
117impl AsRUMPtr for &mut str {
118    #[inline(always)]
119    fn as_rum_ptr(&self) -> *const u8 { self.as_ptr() }
120    #[inline(always)]
121    fn as_mut_rum_ptr(&mut self) -> *mut u8 { self.as_mut_ptr() }
122}
123
124/********************** Basic Impl for AsSlice************************/
125impl AsRUMSlice for RUMVec<u8> {}
126impl AsRUMSlice for &RUMVec<u8> {}
127impl AsRUMSlice for &[u8] {}
128impl AsRUMSlice for RUMString {}
129impl AsRUMSlice for &RUMString {}
130impl AsRUMSlice for &str {}