#random-access #memory-access #read-write #read-memory #offset #length #continuously

random-access-memory

Continuously read and write to memory using random offsets and lengths

14 releases (5 stable)

3.0.0 Apr 12, 2023
2.0.0 Mar 3, 2020
1.2.0 Mar 3, 2020
1.0.0 Apr 24, 2019
0.1.0 Mar 21, 2018

#924 in Algorithms

Download history 74/week @ 2024-07-21 81/week @ 2024-07-28 47/week @ 2024-08-04 35/week @ 2024-08-11 30/week @ 2024-08-18 36/week @ 2024-08-25 122/week @ 2024-09-01 183/week @ 2024-09-08 18/week @ 2024-09-15 152/week @ 2024-09-22 80/week @ 2024-09-29 9/week @ 2024-10-06 19/week @ 2024-10-13 111/week @ 2024-10-20 44/week @ 2024-10-27 13/week @ 2024-11-03

188 downloads per month
Used in 6 crates (4 directly)

MIT/Apache

18KB
202 lines

random-access-memory

crates.io version build status downloads docs.rs docs

Continuously read and write to memory using random offsets and lengths using abstract interface defined in random-access-storage.

Installation

$ cargo add random-access-memory

License

MIT OR Apache-2.0


lib.rs:

Continuously read and write to memory using random offsets and lengths

[RandomAccessMemory] is a complete implementation of random-access-storage for in-memory storage.

See also random-access-disk for on-disk storage that can be swapped with this.

Examples

Reading, writing, deleting and truncating:

use random_access_storage::RandomAccess;
use random_access_memory::RandomAccessMemory;

let mut storage = RandomAccessMemory::default();
storage.write(0, b"hello").await.unwrap();
storage.write(5, b" world").await.unwrap();
assert_eq!(storage.read(0, 11).await.unwrap(), b"hello world");
assert_eq!(storage.len().await.unwrap(), 11);
storage.del(5, 2).await.unwrap();
assert_eq!(storage.read(5, 2).await.unwrap(), [0, 0]);
assert_eq!(storage.len().await.unwrap(), 11);
storage.truncate(2).await.unwrap();
assert_eq!(storage.len().await.unwrap(), 2);
storage.truncate(5).await.unwrap();
assert_eq!(storage.len().await.unwrap(), 5);
assert_eq!(storage.read(0, 5).await.unwrap(), [b'h', b'e', 0, 0, 0]);

In order to get benefits from the swappable interface, you will in most cases want to use generic functions for storage manipulation:

use random_access_storage::RandomAccess;
use random_access_memory::RandomAccessMemory;
use std::fmt::Debug;

let mut storage = RandomAccessMemory::default();
write_hello_world(&mut storage).await;
assert_eq!(read_hello_world(&mut storage).await, b"hello world");

/// Write with swappable storage
async fn write_hello_world<T>(storage: &mut T)
where T: RandomAccess + Debug + Send,
{
  storage.write(0, b"hello").await.unwrap();
  storage.write(5, b" world").await.unwrap();
}

/// Read with swappable storage
async fn read_hello_world<T>(storage: &mut T) -> Vec<u8>
where T: RandomAccess + Debug + Send,
{
  storage.read(0, 11).await.unwrap()
}

Dependencies

~295–760KB
~17K SLoC