Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
language: rust
rust:
- nightly
- stable

before_script: |
rustup update stable &&
rustup component add --toolchain stable rustfmt-preview &&
rustup component add --toolchain stable clippy-preview
rustup component add rustfmt &&
rustup component add clippy
script: |
cargo +stable fmt -- --check &&
cargo +stable clippy -- -D clippy::all &&
cargo fmt -- --check &&
cargo clippy -- -D clippy::all &&
cargo build --verbose &&
cargo test --verbose
cache: cargo
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ readme = "README.md"
edition = "2018"

[dependencies]
async-trait = "0.1.24"
futures-io = "0.3.4"
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,52 @@ can easily be swapped, depending on the environment.

## Usage
```rust
extern crate random_access_storage;

use random_access_storage::{RandomAccessMethods, RandomAccess};
use async_trait::async_trait;

struct S;
#[async_trait]
impl RandomAccessMethods for S {
type Error = std::io::Error;

fn open(&mut self) -> Result<(), Self::Error> {
async fn open(&mut self) -> Result<(), Self::Error> {
unimplemented!();
}

fn write(&mut self, offset: u64, data: &[u8]) -> Result<(), Self::Error> {
async fn write(&mut self, offset: u64, data: &[u8]) -> Result<(), Self::Error> {
unimplemented!();
}

fn read(&mut self, offset: u64, length: u64) -> Result<Vec<u8>, Self::Error> {
async fn read(&mut self, offset: u64, length: u64) -> Result<Vec<u8>, Self::Error> {
unimplemented!();
}

fn read_to_writer(
async fn read_to_writer(
&mut self,
offset: u64,
length: u64,
writer: &mut impl std::io::Writer
writer: &mut (impl futures::io::AsyncWriter + Send)
) -> Result<(), Self::Error> {
unimplemented!();
}

fn del(&mut self, offset: u64, length: u64) -> Result<(), Self::Error> {
async fn del(&mut self, offset: u64, length: u64) -> Result<(), Self::Error> {
unimplemented!();
}

fn truncate(&mut self, length: u64) -> Result<(), Self::Error> {
async fn truncate(&mut self, length: u64) -> Result<(), Self::Error> {
unimplemented!();
}

fn len(&mut self) -> Result<u64, Self::Error> {
async fn len(&mut self) -> Result<u64, Self::Error> {
unimplemented!();
}

fn is_empty(&mut self) -> Result<bool, Self::Error> {
async fn is_empty(&mut self) -> Result<bool, Self::Error> {
unimplemented!();
}

fn sync_all(&mut self) -> Result<(), Self::Error> {
async fn sync_all(&mut self) -> Result<(), Self::Error> {
unimplemented!();
}
}
Expand Down
29 changes: 18 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,50 @@
#![cfg_attr(feature = "nightly", doc(include = "../README.md"))]
#![cfg_attr(test, deny(warnings))]

use std::io;

/// The `RandomAccess` trait allows for reading from and writing to a
/// randomly accessible storage of bytes.
#[async_trait::async_trait]
pub trait RandomAccess {
/// An error.
type Error;

/// Write bytes at an offset to the backend.
fn write(&mut self, offset: u64, data: &[u8]) -> Result<(), Self::Error>;
async fn write(
&mut self,
offset: u64,
data: &[u8],
) -> Result<(), Self::Error>;

/// Read a sequence of bytes at an offset from the backend.
fn read(&mut self, offset: u64, length: u64) -> Result<Vec<u8>, Self::Error>;
async fn read(
&mut self,
offset: u64,
length: u64,
) -> Result<Vec<u8>, Self::Error>;

/// Read a sequence of bytes at an offset from the backend.
fn read_to_writer(
async fn read_to_writer(
&mut self,
offset: u64,
length: u64,
buf: &mut impl io::Write,
buf: &mut (impl futures_io::AsyncWrite + Send),
) -> Result<(), Self::Error>;

/// Delete a sequence of bytes at an offset from the backend.
fn del(&mut self, offset: u64, length: u64) -> Result<(), Self::Error>;
async fn del(&mut self, offset: u64, length: u64) -> Result<(), Self::Error>;

/// Resize the sequence of bytes, possibly discarding or zero-padding bytes
/// from the end.
fn truncate(&mut self, length: u64) -> Result<(), Self::Error>;
async fn truncate(&mut self, length: u64) -> Result<(), Self::Error>;

/// Get the size of the storage in bytes.
fn len(&self) -> Result<u64, Self::Error>;
async fn len(&self) -> Result<u64, Self::Error>;

/// Whether the storage is empty.
/// For some storage backends it may be cheaper to calculate whether the
/// storage is empty than to calculate the length.
fn is_empty(&mut self) -> Result<bool, Self::Error>;
async fn is_empty(&mut self) -> Result<bool, Self::Error>;

/// Flush buffered data on the underlying storage resource.
fn sync_all(&mut self) -> Result<(), Self::Error>;
async fn sync_all(&mut self) -> Result<(), Self::Error>;
}