diff --git a/crates/librqbit/src/storage/mod.rs b/crates/librqbit/src/storage/mod.rs index 7a65bfb..a557617 100644 --- a/crates/librqbit/src/storage/mod.rs +++ b/crates/librqbit/src/storage/mod.rs @@ -12,6 +12,12 @@ pub trait StorageFactory: Send + Sync + Any { fn init_storage(&self, info: &ManagedTorrentInfo) -> anyhow::Result>; } +impl StorageFactory for Box { + fn init_storage(&self, info: &ManagedTorrentInfo) -> anyhow::Result> { + (**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>; } + +impl TorrentStorage for Box { + 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> { + (**self).take() + } +} diff --git a/crates/librqbit/src/storage/slow.rs b/crates/librqbit/src/storage/slow.rs index b06b2f4..e822d7b 100644 --- a/crates/librqbit/src/storage/slow.rs +++ b/crates/librqbit/src/storage/slow.rs @@ -4,19 +4,19 @@ use rand::Rng; use super::{StorageFactory, TorrentStorage}; -pub struct SlowStorageFactory { - underlying_factory: Box, +pub struct SlowStorageFactory { + underlying_factory: U, } -impl SlowStorageFactory { - pub fn new(underlying: Box) -> Self { +impl SlowStorageFactory { + pub fn new(underlying: U) -> Self { Self { underlying_factory: underlying, } } } -impl StorageFactory for SlowStorageFactory { +impl StorageFactory for SlowStorageFactory { fn init_storage( &self, info: &crate::ManagedTorrentInfo, @@ -27,8 +27,8 @@ impl StorageFactory for SlowStorageFactory { } } -struct SlowStorage { - underlying: Box, +struct SlowStorage { + underlying: U, } fn random_sleep() { @@ -38,7 +38,7 @@ fn random_sleep() { std::thread::sleep(sl) } -impl TorrentStorage for SlowStorage { +impl TorrentStorage for SlowStorage { 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> { - Ok(Box::new(Self { + Ok(Box::new(SlowStorage { underlying: self.underlying.take()?, })) } diff --git a/crates/librqbit/src/storage/timing.rs b/crates/librqbit/src/storage/timing.rs index db50745..b3cf7cd 100644 --- a/crates/librqbit/src/storage/timing.rs +++ b/crates/librqbit/src/storage/timing.rs @@ -1,12 +1,12 @@ use super::{StorageFactory, TorrentStorage}; -pub struct TimingStorageFactory { +pub struct TimingStorageFactory { name: String, - underlying_factory: Box, + underlying_factory: U, } -impl TimingStorageFactory { - pub fn new(name: String, underlying: Box) -> Self { +impl TimingStorageFactory { + pub fn new(name: String, underlying: U) -> Self { Self { name, underlying_factory: underlying, @@ -14,7 +14,7 @@ impl TimingStorageFactory { } } -impl StorageFactory for TimingStorageFactory { +impl StorageFactory for TimingStorageFactory { fn init_storage( &self, info: &crate::ManagedTorrentInfo, @@ -26,9 +26,9 @@ impl StorageFactory for TimingStorageFactory { } } -struct TimingStorage { +struct TimingStorage { name: String, - underlying: Box, + underlying: U, } macro_rules! timeit { @@ -43,7 +43,7 @@ macro_rules! timeit { }; } -impl TorrentStorage for TimingStorage { +impl TorrentStorage for TimingStorage { 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> { - Ok(Box::new(Self { + Ok(Box::new(TimingStorage { underlying: self.underlying.take()?, name: self.name.clone(), }))