extism_convert/memory_handle.rs
1/// `MemoryHandle` describes where in memory a block of data is stored
2#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
3pub struct MemoryHandle {
4 /// The offset of the region in Extism linear memory
5 pub offset: u64,
6
7 /// The length of the memory region
8 pub length: u64,
9}
10
11impl MemoryHandle {
12 /// Create a new `MemoryHandle` from an offset in memory and length
13 ///
14 /// # Safety
15 /// This function is unsafe because the specified memory region may not be valid.
16 pub unsafe fn new(offset: u64, length: u64) -> MemoryHandle {
17 MemoryHandle { offset, length }
18 }
19
20 /// `NULL` equivalent
21 pub fn null() -> MemoryHandle {
22 MemoryHandle {
23 offset: 0,
24 length: 0,
25 }
26 }
27
28 /// Get the offset of a memory handle
29 pub fn offset(&self) -> u64 {
30 self.offset
31 }
32
33 /// Get the length of the memory region
34 pub fn len(&self) -> usize {
35 self.length as usize
36 }
37
38 /// Returns `true` when the length is 0
39 pub fn is_empty(&self) -> bool {
40 self.length == 0
41 }
42}