|
| 1 | +//! Annotating wrapper around storage objects. |
| 2 | +//! |
| 3 | +//! Wraps other storage objects, adding an arbitrary tag to them. |
| 4 | +//! |
| 5 | +//! This may be useful when using the “mapping” interface, to identify the storage objects returned |
| 6 | +//! in raw mappings. |
| 7 | +//! |
| 8 | +//! Example: |
| 9 | +//! ``` |
| 10 | +//! # use imago::{FormatAccess, Mapping}; |
| 11 | +//! # use imago::annotated::Annotated; |
| 12 | +//! # use imago::null::Null; |
| 13 | +//! # use imago::raw::Raw; |
| 14 | +//! # tokio::runtime::Builder::new_current_thread() |
| 15 | +//! # .build() |
| 16 | +//! # .unwrap() |
| 17 | +//! # .block_on(async move { |
| 18 | +//! # |
| 19 | +//! const TEST_TAG: u32 = 42; |
| 20 | +//! |
| 21 | +//! let disk_size = 16 << 30; |
| 22 | +//! let test_offset = 1 << 30; |
| 23 | +//! |
| 24 | +//! let inner_storage = Null::new(disk_size); |
| 25 | +//! let annotated_storage = Annotated::new(inner_storage, TEST_TAG); |
| 26 | +//! let image = Raw::open_image(annotated_storage, false).await?; |
| 27 | +//! let image = FormatAccess::new(image); |
| 28 | +//! |
| 29 | +//! let mapping = image.get_mapping(test_offset, 1).await?.0; |
| 30 | +//! let Mapping::Raw { |
| 31 | +//! storage, |
| 32 | +//! offset, |
| 33 | +//! writable, |
| 34 | +//! } = mapping |
| 35 | +//! else { |
| 36 | +//! panic!("Raw mapping expected"); |
| 37 | +//! }; |
| 38 | +//! assert_eq!(*storage.tag(), TEST_TAG); |
| 39 | +//! assert_eq!(offset, test_offset); |
| 40 | +//! # |
| 41 | +//! # Ok::<(), std::io::Error>(()) |
| 42 | +//! # }).unwrap() |
| 43 | +//! ``` |
| 44 | +
|
| 45 | +use crate::io_buffers::{IoVector, IoVectorMut}; |
| 46 | +use crate::storage::drivers::CommonStorageHelper; |
| 47 | +use crate::{Storage, StorageOpenOptions}; |
| 48 | +use std::fmt::{self, Debug, Display, Formatter}; |
| 49 | +use std::io; |
| 50 | +use std::ops::{Deref, DerefMut}; |
| 51 | +use std::path::{Path, PathBuf}; |
| 52 | + |
| 53 | +/// Annotating wrapper around storage objects. |
| 54 | +/// |
| 55 | +/// Wraps other storage objects, adding an arbitrary tag to them. |
| 56 | +// TODO: Remove the `Default` requirement. We want to implement `Storage::open()` if `Default` is |
| 57 | +// implemented, though, but return an error if it is not. Doing that probably requires |
| 58 | +// specialization, though. |
| 59 | +#[derive(Debug)] |
| 60 | +pub struct Annotated<Tag: Debug + Default + Display + Send + Sync, S: Storage> { |
| 61 | + /// Wrapped storage object. |
| 62 | + inner: S, |
| 63 | + |
| 64 | + /// Tag. |
| 65 | + tag: Tag, |
| 66 | +} |
| 67 | + |
| 68 | +impl<T: Debug + Default + Display + Send + Sync, S: Storage> Annotated<T, S> { |
| 69 | + /// Wrap `storage`, adding the tag `tag`. |
| 70 | + pub fn new(storage: S, tag: T) -> Self { |
| 71 | + Annotated { |
| 72 | + inner: storage, |
| 73 | + tag, |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// Get the tag. |
| 78 | + pub fn tag(&self) -> &T { |
| 79 | + &self.tag |
| 80 | + } |
| 81 | + |
| 82 | + /// Allow modifying or changing the tag. |
| 83 | + pub fn tag_mut(&mut self) -> &mut T { |
| 84 | + &mut self.tag |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl<T: Debug + Default + Display + Send + Sync, S: Storage> From<S> for Annotated<T, S> { |
| 89 | + fn from(storage: S) -> Self { |
| 90 | + Self::new(storage, T::default()) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl<T: Debug + Default + Display + Send + Sync, S: Storage> Storage for Annotated<T, S> { |
| 95 | + async fn open(opts: StorageOpenOptions) -> io::Result<Self> { |
| 96 | + Ok(S::open(opts).await?.into()) |
| 97 | + } |
| 98 | + |
| 99 | + #[cfg(feature = "sync-wrappers")] |
| 100 | + fn open_sync(opts: StorageOpenOptions) -> io::Result<Self> { |
| 101 | + Ok(S::open_sync(opts)?.into()) |
| 102 | + } |
| 103 | + |
| 104 | + fn mem_align(&self) -> usize { |
| 105 | + self.inner.mem_align() |
| 106 | + } |
| 107 | + |
| 108 | + fn req_align(&self) -> usize { |
| 109 | + self.inner.req_align() |
| 110 | + } |
| 111 | + |
| 112 | + fn size(&self) -> io::Result<u64> { |
| 113 | + self.inner.size() |
| 114 | + } |
| 115 | + |
| 116 | + fn resolve_relative_path<P: AsRef<Path>>(&self, relative: P) -> io::Result<PathBuf> { |
| 117 | + self.inner.resolve_relative_path(relative) |
| 118 | + } |
| 119 | + |
| 120 | + async unsafe fn pure_readv(&self, bufv: IoVectorMut<'_>, offset: u64) -> io::Result<()> { |
| 121 | + // Caller guarantees safety |
| 122 | + unsafe { self.inner.pure_readv(bufv, offset) }.await |
| 123 | + } |
| 124 | + |
| 125 | + async unsafe fn pure_writev(&self, bufv: IoVector<'_>, offset: u64) -> io::Result<()> { |
| 126 | + // Caller guarantees safety |
| 127 | + unsafe { self.inner.pure_writev(bufv, offset) }.await |
| 128 | + } |
| 129 | + |
| 130 | + async unsafe fn pure_write_zeroes(&self, offset: u64, length: u64) -> io::Result<()> { |
| 131 | + // Caller guarantees safety |
| 132 | + unsafe { self.inner.pure_write_zeroes(offset, length) }.await |
| 133 | + } |
| 134 | + |
| 135 | + async unsafe fn pure_discard(&self, offset: u64, length: u64) -> io::Result<()> { |
| 136 | + // Caller guarantees safety |
| 137 | + unsafe { self.inner.pure_discard(offset, length) }.await |
| 138 | + } |
| 139 | + |
| 140 | + async fn flush(&self) -> io::Result<()> { |
| 141 | + self.inner.flush().await |
| 142 | + } |
| 143 | + |
| 144 | + async fn sync(&self) -> io::Result<()> { |
| 145 | + self.inner.sync().await |
| 146 | + } |
| 147 | + |
| 148 | + fn get_storage_helper(&self) -> &CommonStorageHelper { |
| 149 | + // Share storage helper from inner (to e.g. get same request serialization) |
| 150 | + self.inner.get_storage_helper() |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +impl<T: Debug + Default + Display + Send + Sync, S: Storage> Deref for Annotated<T, S> { |
| 155 | + type Target = S; |
| 156 | + |
| 157 | + fn deref(&self) -> &S { |
| 158 | + &self.inner |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl<T: Debug + Default + Display + Send + Sync, S: Storage> DerefMut for Annotated<T, S> { |
| 163 | + fn deref_mut(&mut self) -> &mut S { |
| 164 | + &mut self.inner |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +impl<T: Debug + Default + Display + Send + Sync, S: Storage> Display for Annotated<T, S> { |
| 169 | + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 170 | + write!(f, "annotated({})[{}]", self.tag, self.inner) |
| 171 | + } |
| 172 | +} |
0 commit comments