use crate::io_buffers::{IoVector, IoVectorMut};
use crate::storage::drivers::CommonStorageHelper;
use crate::storage::PreallocateMode;
use crate::{Storage, StorageCreateOptions, StorageOpenOptions};
use std::fmt::{self, Debug, Display, Formatter};
use std::io;
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
#[derive(Debug)]
pub struct Annotated<Tag: Debug + Default + Display + Send + Sync, S: Storage> {
inner: S,
tag: Tag,
}
impl<T: Debug + Default + Display + Send + Sync, S: Storage> Annotated<T, S> {
pub fn new(storage: S, tag: T) -> Self {
Annotated {
inner: storage,
tag,
}
}
pub fn tag(&self) -> &T {
&self.tag
}
pub fn tag_mut(&mut self) -> &mut T {
&mut self.tag
}
}
impl<T: Debug + Default + Display + Send + Sync, S: Storage> From<S> for Annotated<T, S> {
fn from(storage: S) -> Self {
Self::new(storage, T::default())
}
}
impl<T: Debug + Default + Display + Send + Sync, S: Storage> Storage for Annotated<T, S> {
async fn open(opts: StorageOpenOptions) -> io::Result<Self> {
Ok(S::open(opts).await?.into())
}
#[cfg(feature = "sync-wrappers")]
fn open_sync(opts: StorageOpenOptions) -> io::Result<Self> {
Ok(S::open_sync(opts)?.into())
}
async fn create_open(opts: StorageCreateOptions) -> io::Result<Self> {
Ok(S::create_open(opts).await?.into())
}
fn mem_align(&self) -> usize {
self.inner.mem_align()
}
fn req_align(&self) -> usize {
self.inner.req_align()
}
fn size(&self) -> io::Result<u64> {
self.inner.size()
}
fn resolve_relative_path<P: AsRef<Path>>(&self, relative: P) -> io::Result<PathBuf> {
self.inner.resolve_relative_path(relative)
}
fn get_filename(&self) -> Option<PathBuf> {
self.inner.get_filename()
}
async unsafe fn pure_readv(&self, bufv: IoVectorMut<'_>, offset: u64) -> io::Result<()> {
unsafe { self.inner.pure_readv(bufv, offset) }.await
}
async unsafe fn pure_writev(&self, bufv: IoVector<'_>, offset: u64) -> io::Result<()> {
unsafe { self.inner.pure_writev(bufv, offset) }.await
}
async unsafe fn pure_write_zeroes(&self, offset: u64, length: u64) -> io::Result<()> {
unsafe { self.inner.pure_write_zeroes(offset, length) }.await
}
async unsafe fn pure_discard(&self, offset: u64, length: u64) -> io::Result<()> {
unsafe { self.inner.pure_discard(offset, length) }.await
}
async fn flush(&self) -> io::Result<()> {
self.inner.flush().await
}
async fn sync(&self) -> io::Result<()> {
self.inner.sync().await
}
async unsafe fn invalidate_cache(&self) -> io::Result<()> {
unsafe { self.inner.invalidate_cache() }.await
}
fn get_storage_helper(&self) -> &CommonStorageHelper {
self.inner.get_storage_helper()
}
async fn resize(&self, new_size: u64, prealloc_mode: PreallocateMode) -> io::Result<()> {
self.inner.resize(new_size, prealloc_mode).await
}
}
impl<T: Debug + Default + Display + Send + Sync, S: Storage> Deref for Annotated<T, S> {
type Target = S;
fn deref(&self) -> &S {
&self.inner
}
}
impl<T: Debug + Default + Display + Send + Sync, S: Storage> DerefMut for Annotated<T, S> {
fn deref_mut(&mut self) -> &mut S {
&mut self.inner
}
}
impl<T: Debug + Default + Display + Send + Sync, S: Storage> Display for Annotated<T, S> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "annotated({})[{}]", self.tag, self.inner)
}
}