storages generics
This commit is contained in:
parent
fdc9243a41
commit
e4adfa569a
3 changed files with 46 additions and 18 deletions
|
|
@ -12,6 +12,12 @@ pub trait StorageFactory: Send + Sync + Any {
|
||||||
fn init_storage(&self, info: &ManagedTorrentInfo) -> anyhow::Result<Box<dyn TorrentStorage>>;
|
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 {
|
pub trait TorrentStorage: Send + Sync {
|
||||||
fn pread_exact(&self, file_id: usize, offset: u64, buf: &mut [u8]) -> anyhow::Result<()>;
|
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>>;
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,19 +4,19 @@ use rand::Rng;
|
||||||
|
|
||||||
use super::{StorageFactory, TorrentStorage};
|
use super::{StorageFactory, TorrentStorage};
|
||||||
|
|
||||||
pub struct SlowStorageFactory {
|
pub struct SlowStorageFactory<U> {
|
||||||
underlying_factory: Box<dyn StorageFactory>,
|
underlying_factory: U,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SlowStorageFactory {
|
impl<U: StorageFactory> SlowStorageFactory<U> {
|
||||||
pub fn new(underlying: Box<dyn StorageFactory>) -> Self {
|
pub fn new(underlying: U) -> Self {
|
||||||
Self {
|
Self {
|
||||||
underlying_factory: underlying,
|
underlying_factory: underlying,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StorageFactory for SlowStorageFactory {
|
impl<U: StorageFactory> StorageFactory for SlowStorageFactory<U> {
|
||||||
fn init_storage(
|
fn init_storage(
|
||||||
&self,
|
&self,
|
||||||
info: &crate::ManagedTorrentInfo,
|
info: &crate::ManagedTorrentInfo,
|
||||||
|
|
@ -27,8 +27,8 @@ impl StorageFactory for SlowStorageFactory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SlowStorage {
|
struct SlowStorage<U> {
|
||||||
underlying: Box<dyn TorrentStorage>,
|
underlying: U,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn random_sleep() {
|
fn random_sleep() {
|
||||||
|
|
@ -38,7 +38,7 @@ fn random_sleep() {
|
||||||
std::thread::sleep(sl)
|
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<()> {
|
fn pread_exact(&self, file_id: usize, offset: u64, buf: &mut [u8]) -> anyhow::Result<()> {
|
||||||
random_sleep();
|
random_sleep();
|
||||||
self.underlying.pread_exact(file_id, offset, buf)
|
self.underlying.pread_exact(file_id, offset, buf)
|
||||||
|
|
@ -58,7 +58,7 @@ impl TorrentStorage for SlowStorage {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>> {
|
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>> {
|
||||||
Ok(Box::new(Self {
|
Ok(Box::new(SlowStorage {
|
||||||
underlying: self.underlying.take()?,
|
underlying: self.underlying.take()?,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
use super::{StorageFactory, TorrentStorage};
|
use super::{StorageFactory, TorrentStorage};
|
||||||
|
|
||||||
pub struct TimingStorageFactory {
|
pub struct TimingStorageFactory<U> {
|
||||||
name: String,
|
name: String,
|
||||||
underlying_factory: Box<dyn StorageFactory>,
|
underlying_factory: U,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TimingStorageFactory {
|
impl<U> TimingStorageFactory<U> {
|
||||||
pub fn new(name: String, underlying: Box<dyn StorageFactory>) -> Self {
|
pub fn new(name: String, underlying: U) -> Self {
|
||||||
Self {
|
Self {
|
||||||
name,
|
name,
|
||||||
underlying_factory: underlying,
|
underlying_factory: underlying,
|
||||||
|
|
@ -14,7 +14,7 @@ impl TimingStorageFactory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StorageFactory for TimingStorageFactory {
|
impl<U: StorageFactory> StorageFactory for TimingStorageFactory<U> {
|
||||||
fn init_storage(
|
fn init_storage(
|
||||||
&self,
|
&self,
|
||||||
info: &crate::ManagedTorrentInfo,
|
info: &crate::ManagedTorrentInfo,
|
||||||
|
|
@ -26,9 +26,9 @@ impl StorageFactory for TimingStorageFactory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TimingStorage {
|
struct TimingStorage<U> {
|
||||||
name: String,
|
name: String,
|
||||||
underlying: Box<dyn TorrentStorage>,
|
underlying: U,
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! timeit {
|
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<()> {
|
fn pread_exact(&self, file_id: usize, offset: u64, buf: &mut [u8]) -> anyhow::Result<()> {
|
||||||
let storage = &self.name;
|
let storage = &self.name;
|
||||||
let len = buf.len();
|
let len = buf.len();
|
||||||
|
|
@ -79,7 +79,7 @@ impl TorrentStorage for TimingStorage {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>> {
|
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>> {
|
||||||
Ok(Box::new(Self {
|
Ok(Box::new(TimingStorage {
|
||||||
underlying: self.underlying.take()?,
|
underlying: self.underlying.take()?,
|
||||||
name: self.name.clone(),
|
name: self.name.clone(),
|
||||||
}))
|
}))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue