better type_id for nested storages

This commit is contained in:
Igor Katson 2024-05-02 09:53:53 +01:00
parent 776c865781
commit 6233cc9d12
4 changed files with 22 additions and 2 deletions

View file

@ -99,7 +99,9 @@ impl SessionDatabase {
.iter() .iter()
// We don't support serializing / deserializing of other storage types. // We don't support serializing / deserializing of other storage types.
.filter(|(_, torrent)| { .filter(|(_, torrent)| {
torrent.storage_factory.type_id() == TypeId::of::<FilesystemStorageFactory>() torrent
.storage_factory
.is_type_id(TypeId::of::<FilesystemStorageFactory>())
}) })
.map(|(id, torrent)| { .map(|(id, torrent)| {
( (

View file

@ -4,7 +4,10 @@ pub mod mmap;
pub mod slow; pub mod slow;
pub mod timing; pub mod timing;
use std::{any::Any, path::Path}; use std::{
any::{Any, TypeId},
path::Path,
};
use crate::torrent_state::ManagedTorrentInfo; use crate::torrent_state::ManagedTorrentInfo;
@ -12,6 +15,9 @@ pub trait StorageFactory: Send + Sync + Any {
type Storage: TorrentStorage; type Storage: TorrentStorage;
fn init_storage(&self, info: &ManagedTorrentInfo) -> anyhow::Result<Self::Storage>; fn init_storage(&self, info: &ManagedTorrentInfo) -> anyhow::Result<Self::Storage>;
fn is_type_id(&self, type_id: TypeId) -> bool {
Self::type_id(self) == type_id
}
} }
pub type BoxStorageFactory = Box<dyn StorageFactory<Storage = Box<dyn TorrentStorage>>>; pub type BoxStorageFactory = Box<dyn StorageFactory<Storage = Box<dyn TorrentStorage>>>;
@ -33,6 +39,10 @@ impl<SF: StorageFactory> StorageFactoryExt for SF {
let s = self.sf.init_storage(info)?; let s = self.sf.init_storage(info)?;
Ok(Box::new(s)) Ok(Box::new(s))
} }
fn is_type_id(&self, type_id: TypeId) -> bool {
self.sf.type_id() == type_id
}
} }
Box::new(Wrapper { sf: self }) Box::new(Wrapper { sf: self })

View file

@ -24,6 +24,10 @@ impl<U: StorageFactory> StorageFactory for SlowStorageFactory<U> {
underlying: self.underlying_factory.init_storage(info)?, underlying: self.underlying_factory.init_storage(info)?,
}) })
} }
fn is_type_id(&self, type_id: std::any::TypeId) -> bool {
self.underlying_factory.is_type_id(type_id)
}
} }
pub struct SlowStorage<U> { pub struct SlowStorage<U> {

View file

@ -23,6 +23,10 @@ impl<U: StorageFactory> StorageFactory for TimingStorageFactory<U> {
underlying: self.underlying_factory.init_storage(info)?, underlying: self.underlying_factory.init_storage(info)?,
}) })
} }
fn is_type_id(&self, type_id: std::any::TypeId) -> bool {
self.underlying_factory.is_type_id(type_id)
}
} }
pub struct TimingStorage<U> { pub struct TimingStorage<U> {