storages generics

This commit is contained in:
Igor Katson 2024-05-01 22:00:00 +01:00
parent fdc9243a41
commit e4adfa569a
3 changed files with 46 additions and 18 deletions

View file

@ -12,6 +12,12 @@ pub trait StorageFactory: Send + Sync + Any {
fn init_storage(&self, info: &ManagedTorrentInfo) -> anyhow::Result<Box<dyn TorrentStorage>>;
}
impl<U: StorageFactory + ?Sized> StorageFactory for Box<U> {
fn init_storage(&self, info: &ManagedTorrentInfo) -> anyhow::Result<Box<dyn TorrentStorage>> {
(**self).init_storage(info)
}
}
pub trait TorrentStorage: Send + Sync {
fn pread_exact(&self, file_id: usize, offset: u64, buf: &mut [u8]) -> anyhow::Result<()>;
@ -23,3 +29,25 @@ pub trait TorrentStorage: Send + Sync {
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>>;
}
impl<U: TorrentStorage + ?Sized> TorrentStorage for Box<U> {
fn pread_exact(&self, file_id: usize, offset: u64, buf: &mut [u8]) -> anyhow::Result<()> {
(**self).pread_exact(file_id, offset, buf)
}
fn pwrite_all(&self, file_id: usize, offset: u64, buf: &[u8]) -> anyhow::Result<()> {
(**self).pwrite_all(file_id, offset, buf)
}
fn remove_file(&self, file_id: usize, filename: &Path) -> anyhow::Result<()> {
(**self).remove_file(file_id, filename)
}
fn ensure_file_length(&self, file_id: usize, length: u64) -> anyhow::Result<()> {
(**self).ensure_file_length(file_id, length)
}
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>> {
(**self).take()
}
}

View file

@ -4,19 +4,19 @@ use rand::Rng;
use super::{StorageFactory, TorrentStorage};
pub struct SlowStorageFactory {
underlying_factory: Box<dyn StorageFactory>,
pub struct SlowStorageFactory<U> {
underlying_factory: U,
}
impl SlowStorageFactory {
pub fn new(underlying: Box<dyn StorageFactory>) -> Self {
impl<U: StorageFactory> SlowStorageFactory<U> {
pub fn new(underlying: U) -> Self {
Self {
underlying_factory: underlying,
}
}
}
impl StorageFactory for SlowStorageFactory {
impl<U: StorageFactory> StorageFactory for SlowStorageFactory<U> {
fn init_storage(
&self,
info: &crate::ManagedTorrentInfo,
@ -27,8 +27,8 @@ impl StorageFactory for SlowStorageFactory {
}
}
struct SlowStorage {
underlying: Box<dyn TorrentStorage>,
struct SlowStorage<U> {
underlying: U,
}
fn random_sleep() {
@ -38,7 +38,7 @@ fn random_sleep() {
std::thread::sleep(sl)
}
impl TorrentStorage for SlowStorage {
impl<U: TorrentStorage> TorrentStorage for SlowStorage<U> {
fn pread_exact(&self, file_id: usize, offset: u64, buf: &mut [u8]) -> anyhow::Result<()> {
random_sleep();
self.underlying.pread_exact(file_id, offset, buf)
@ -58,7 +58,7 @@ impl TorrentStorage for SlowStorage {
}
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>> {
Ok(Box::new(Self {
Ok(Box::new(SlowStorage {
underlying: self.underlying.take()?,
}))
}

View file

@ -1,12 +1,12 @@
use super::{StorageFactory, TorrentStorage};
pub struct TimingStorageFactory {
pub struct TimingStorageFactory<U> {
name: String,
underlying_factory: Box<dyn StorageFactory>,
underlying_factory: U,
}
impl TimingStorageFactory {
pub fn new(name: String, underlying: Box<dyn StorageFactory>) -> Self {
impl<U> TimingStorageFactory<U> {
pub fn new(name: String, underlying: U) -> Self {
Self {
name,
underlying_factory: underlying,
@ -14,7 +14,7 @@ impl TimingStorageFactory {
}
}
impl StorageFactory for TimingStorageFactory {
impl<U: StorageFactory> StorageFactory for TimingStorageFactory<U> {
fn init_storage(
&self,
info: &crate::ManagedTorrentInfo,
@ -26,9 +26,9 @@ impl StorageFactory for TimingStorageFactory {
}
}
struct TimingStorage {
struct TimingStorage<U> {
name: String,
underlying: Box<dyn TorrentStorage>,
underlying: U,
}
macro_rules! timeit {
@ -43,7 +43,7 @@ macro_rules! timeit {
};
}
impl TorrentStorage for TimingStorage {
impl<U: TorrentStorage> TorrentStorage for TimingStorage<U> {
fn pread_exact(&self, file_id: usize, offset: u64, buf: &mut [u8]) -> anyhow::Result<()> {
let storage = &self.name;
let len = buf.len();
@ -79,7 +79,7 @@ impl TorrentStorage for TimingStorage {
}
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>> {
Ok(Box::new(Self {
Ok(Box::new(TimingStorage {
underlying: self.underlying.take()?,
name: self.name.clone(),
}))