assembly_fdb/
handle.rs

1use assembly_core::buffer::{Buffer, CastError, MinimallyAligned, Repr};
2use assembly_fdb_core::file::ArrayHeader;
3use latin1str::Latin1Str;
4use std::{convert::TryFrom, mem::size_of, ops::Deref, result::Result};
5
6/// Base type for a handle to an in-memory data structure
7///
8/// This is basic layout of a handle to an in-memory FDB database.
9/// Internally, there is a pointer (`&[u8]`/`Box<[u8]>`/`Rc<[u8]>`/`Arc<Mmap>`/…)
10/// the memory slice as well as a value that represents the
11/// current target.
12#[derive(Clone, Debug)]
13pub struct BaseHandle<P: Deref, T>
14where
15    <P as Deref>::Target: AsRef<[u8]>,
16{
17    /// The memory pointer
18    pub(super) mem: P,
19    /// The raw value
20    pub(super) raw: T,
21}
22
23impl<P, T> Copy for BaseHandle<P, T>
24where
25    P: Deref + Copy,
26    T: Copy,
27    <P as Deref>::Target: AsRef<[u8]>,
28{
29}
30
31impl<P: Deref> BaseHandle<P, ()>
32where
33    <P as Deref>::Target: AsRef<[u8]>,
34{
35    /// Creates a new handle
36    pub fn new(mem: P) -> Self {
37        Self { mem, raw: () }
38    }
39}
40
41impl<T, P: Deref> BaseHandle<P, Option<T>>
42where
43    <P as Deref>::Target: AsRef<[u8]>,
44{
45    /// Turns a handle of an option into an option of a handle
46    pub fn transpose(self) -> Option<BaseHandle<P, T>> {
47        if let Some(raw) = self.raw {
48            Some(BaseHandle { mem: self.mem, raw })
49        } else {
50            None
51        }
52    }
53}
54
55impl<P: Deref, T> BaseHandle<P, T>
56where
57    <P as Deref>::Target: AsRef<[u8]>,
58{
59    /// Get a reference to the raw value inside
60    pub fn raw(&self) -> &T {
61        &self.raw
62    }
63
64    /// Get a reference to the raw value inside
65    pub fn raw_mut(&mut self) -> &mut T {
66        &mut self.raw
67    }
68
69    /// Get the byte slice for the whole database
70    pub fn as_bytes(&self) -> &[u8] {
71        self.mem.deref().as_ref()
72    }
73
74    /// Replace the value that is stored with the memory pointer
75    pub fn replace<O>(self, raw: O) -> BaseHandle<P, O> {
76        BaseHandle { mem: self.mem, raw }
77    }
78}
79
80/// The basic handle into a byte buffer
81pub type Handle<'a, T> = BaseHandle<&'a [u8], T>;
82
83impl<'a, T> Handle<'a, T> {
84    /// Returns a copy of the contained buffer
85    pub fn buf(self) -> &'a [u8] {
86        self.mem
87    }
88
89    /// Get the raw value out of the handle
90    pub fn into_raw(self) -> T {
91        self.raw
92    }
93
94    /// Wrap a value as a handle
95    pub(crate) fn wrap<R>(&self, raw: R) -> Handle<'a, R> {
96        Handle { mem: self.mem, raw }
97    }
98
99    /// Map a cast reference
100    pub(crate) fn try_map_cast<R: MinimallyAligned>(
101        &self,
102        offset: u32,
103    ) -> Result<RefHandle<'a, R>, CastError> {
104        let raw: &'a R = self.mem.try_cast(offset)?;
105        Ok(self.wrap(raw))
106    }
107
108    /// Map a casted slice
109    pub(crate) fn try_map_cast_slice<R: MinimallyAligned>(
110        &self,
111        offset: u32,
112        count: u32,
113    ) -> Result<RefHandle<'a, [R]>, CastError> {
114        let raw: &'a [R] = self.mem.try_cast_slice(offset, count)?;
115        Ok(self.wrap(raw))
116    }
117
118    /// Map a casted array
119    pub(crate) fn try_map_cast_array<R: MinimallyAligned>(
120        &self,
121        array: ArrayHeader,
122    ) -> Result<RefHandle<'a, [R]>, CastError> {
123        let raw: &'a [R] = self.mem.try_cast_slice(array.base_offset, array.count)?;
124        Ok(self.wrap(raw))
125    }
126
127    /// Map something with a closure
128    pub fn map<X>(self, mapper: impl Fn(&'a [u8], T) -> X) -> Handle<'a, X> {
129        let raw = mapper(self.mem, self.raw);
130        Handle { mem: self.mem, raw }
131    }
132
133    /// Map the value with a closure
134    pub fn map_val<X>(self, mapper: impl Fn(T) -> X) -> Handle<'a, X> {
135        let raw = mapper(self.raw);
136        Handle { mem: self.mem, raw }
137    }
138
139    /// Map something with a closure
140    pub fn try_map<X, E>(
141        self,
142        mapper: impl Fn(&'a [u8], T) -> Result<X, E>,
143    ) -> Result<Handle<'a, X>, E> {
144        let raw = mapper(self.mem, self.raw)?;
145        Ok(Handle { mem: self.mem, raw })
146    }
147}
148
149impl<'a, T> Iterator for Handle<'a, T>
150where
151    T: Iterator,
152{
153    type Item = Handle<'a, T::Item>;
154
155    /// Returns a copy of the contained buffer
156    fn next(&mut self) -> Option<Self::Item> {
157        self.raw.next().map(|raw| Handle { mem: self.mem, raw })
158    }
159}
160
161/// Try from a handle
162pub trait TryFromHandle<'a, T>: Sized {
163    /// Error type
164    type Error;
165    /// Conversion function
166    fn try_from(h: Handle<'a, T>) -> Result<Self, Self::Error>;
167}
168
169/// A handle that contains a reference
170pub type RefHandle<'a, T> = Handle<'a, &'a T>;
171
172impl<'a, T> RefHandle<'a, [T]> {
173    /// Get the reference at `index`
174    pub fn get(self, index: usize) -> Option<RefHandle<'a, T>> {
175        self.raw.get(index).map(|raw| self.wrap(raw))
176    }
177}
178
179impl<'a, T: Repr> RefHandle<'a, T> {
180    /// Extract a value from a reference
181    pub fn map_extract(self) -> Handle<'a, T::Value> {
182        self.wrap(self.raw.extract())
183    }
184}
185
186/// A handle that contains a slice iterator
187pub type SliceIterHandle<'a, T> = Handle<'a, std::slice::Iter<'a, T>>;
188
189/// Get a buffer as a latin1 string
190pub fn get_string(buf: &[u8], offset: u32) -> Result<&Latin1Str, CastError> {
191    let start = offset as usize;
192    let buf = buf.get(start..).ok_or(CastError::OutOfBounds { offset })?;
193    Ok(Latin1Str::from_bytes_until_nul(buf))
194}
195
196/// Get i64
197pub fn get_i64(buf: &[u8], addr: u32) -> Result<i64, CastError> {
198    let start = addr as usize;
199    let end = start + size_of::<u64>();
200    if end > buf.len() {
201        Err(CastError::OutOfBounds { offset: addr })
202    } else {
203        let (_, base) = buf.split_at(start);
204        let (bytes, _) = base.split_at(size_of::<u64>());
205        let val = i64::from_le_bytes(<[u8; 8]>::try_from(bytes).unwrap());
206        Ok(val)
207    }
208}