Expand description

Continuously read and write to disk, using random offsets and lengths

RandomAccessDisk is a complete implementation of random-access-storage for in-memory storage.

See also random-access-memory for in-memory storage that can be swapped with this.

Features

sparse (default)

Deleting may create sparse files, on by default. Creation of sparse files is tested on OSX, linux and Windows.

NB: If this is on, unsafe code is used to make direct platform-specific calls!

async-std (default)

Use the async-std runtime, on by default. Either this or tokio is mandatory.

tokio

Use the tokio runtime. Either this or async_std is mandatory.

Examples

Reading, writing, deleting and truncating:

use random_access_storage::RandomAccess;
use random_access_disk::RandomAccessDisk;

let path = tempfile::Builder::new().prefix("basic").tempfile().unwrap().into_temp_path();
let mut storage = RandomAccessDisk::open(path.to_path_buf()).await.unwrap();
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_disk::RandomAccessDisk;
use std::fmt::Debug;

let path = tempfile::Builder::new().prefix("swappable").tempfile().unwrap().into_temp_path();
let mut storage = RandomAccessDisk::open(path.to_path_buf()).await.unwrap();
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()
}

Structs