pub mod examples; pub mod filesystem; pub mod middleware; use std::{ any::{Any, TypeId}, path::Path, }; use crate::torrent_state::ManagedTorrentInfo; pub trait StorageFactory: Send + Sync + Any { type Storage: TorrentStorage; fn init_storage(&self, info: &ManagedTorrentInfo) -> anyhow::Result; fn is_type_id(&self, type_id: TypeId) -> bool { Self::type_id(self) == type_id } } pub type BoxStorageFactory = Box>>; pub trait StorageFactoryExt { fn boxed(self) -> BoxStorageFactory; } impl StorageFactoryExt for SF { fn boxed(self) -> BoxStorageFactory { struct Wrapper { sf: SF, } impl StorageFactory for Wrapper { type Storage = Box; fn init_storage(&self, info: &ManagedTorrentInfo) -> anyhow::Result { let s = self.sf.init_storage(info)?; Ok(Box::new(s)) } fn is_type_id(&self, type_id: TypeId) -> bool { self.sf.type_id() == type_id } } Box::new(Wrapper { sf: self }) } } impl StorageFactory for Box { type Storage = U::Storage; 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<()>; fn pwrite_all(&self, file_id: usize, offset: u64, buf: &[u8]) -> anyhow::Result<()>; fn remove_file(&self, file_id: usize, filename: &Path) -> anyhow::Result<()>; fn ensure_file_length(&self, file_id: usize, length: u64) -> anyhow::Result<()>; 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() } }