Skip to main content

glycin_utils/memory/
local.rs

1use std::ops::{Deref, DerefMut};
2
3use crate::{ByteData, FungibleMemory, MemoryAllocationError};
4
5#[derive(Debug, Clone)]
6pub struct LocalMemory(Vec<u8>);
7
8impl LocalMemory {
9    pub fn into_inner(self) -> Vec<u8> {
10        self.0
11    }
12}
13
14impl ByteData for LocalMemory {
15    fn new(size: u64) -> std::io::Result<Self> {
16        Ok(Self(vec![0; size as usize]))
17    }
18
19    fn into_fungible(self) -> FungibleMemory {
20        FungibleMemory::LocalMemory(self.0)
21    }
22
23    #[cfg(feature = "external")]
24    fn from_shared(shared: crate::SharedMemory) -> Self {
25        Self(shared.to_vec())
26    }
27
28    fn into_other<O: ByteData>(self) -> Result<O, MemoryAllocationError> {
29        O::try_from_vec(self.0)
30    }
31
32    fn try_from_vec(value: Vec<u8>) -> Result<Self, MemoryAllocationError> {
33        Ok(Self(value))
34    }
35
36    fn try_from_slice(value: &[u8]) -> Result<Self, MemoryAllocationError> {
37        Ok(Self(value.to_vec()))
38    }
39
40    async fn final_seal(&mut self) -> Result<(), MemoryAllocationError> {
41        Ok(())
42    }
43
44    async fn initial_seal(&mut self) -> Result<(), MemoryAllocationError> {
45        Ok(())
46    }
47
48    #[cfg(feature = "glib")]
49    fn into_gbytes(self) -> Result<glib::Bytes, MemoryAllocationError> {
50        Ok(glib::Bytes::from_owned(self.0))
51    }
52}
53
54impl Deref for LocalMemory {
55    type Target = [u8];
56
57    fn deref(&self) -> &[u8] {
58        &self.0
59    }
60}
61
62impl DerefMut for LocalMemory {
63    fn deref_mut(&mut self) -> &mut [u8] {
64        &mut self.0
65    }
66}
67
68impl From<Vec<u8>> for LocalMemory {
69    fn from(value: Vec<u8>) -> Self {
70        Self(value)
71    }
72}