crazyflie_lib/subsystems/memory/raw.rs
1use crate::{subsystems::memory::{memory_types, MemoryBackend}, Result};
2
3use memory_types::{FromMemoryBackend};
4
5/// This is used to get raw memory access to any memory device.
6pub struct RawMemory {
7 memory: MemoryBackend,
8}
9
10impl FromMemoryBackend for RawMemory {
11 async fn from_memory_backend(memory: MemoryBackend) -> Result<Self> {
12 Ok(Self { memory })
13 }
14
15 async fn initialize_memory_backend(memory: MemoryBackend) -> Result<Self> {
16 Ok(Self { memory })
17 }
18
19 fn close_memory(self) -> MemoryBackend {
20 self.memory
21 }
22}
23
24impl RawMemory {
25 /// Read raw data from the memory device at the specified address and length.
26 ///
27 /// # Arguments
28 /// * `address` - The starting address to read from
29 /// * `length` - The number of bytes to read
30 /// # Returns
31 /// A `Result` containing a vector of bytes read from the memory or an `Error
32 /// if the operation fails
33 pub async fn read(&self, address: usize, length: usize) -> Result<Vec<u8>> {
34 self.memory.read(address, length).await
35 }
36
37 /// Write raw data to the memory device at the specified address.
38 ///
39 /// # Arguments
40 /// * `address` - The starting address to write to
41 /// * `data` - A mutable slice of bytes to write to the memory
42 /// # Returns
43 /// A `Result` indicating success or failure of the write operation
44 pub async fn write(&self, address: usize, data: &[u8]) -> Result<()> {
45 self.memory.write(address, data).await
46 }
47}