From 6233cc9d121f879059bc8ed2a1f13a96d0d81dd8 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Thu, 2 May 2024 09:53:53 +0100 Subject: [PATCH] better type_id for nested storages --- crates/librqbit/src/session.rs | 4 +++- crates/librqbit/src/storage/mod.rs | 12 +++++++++++- crates/librqbit/src/storage/slow.rs | 4 ++++ crates/librqbit/src/storage/timing.rs | 4 ++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/crates/librqbit/src/session.rs b/crates/librqbit/src/session.rs index 9374420..f9898fe 100644 --- a/crates/librqbit/src/session.rs +++ b/crates/librqbit/src/session.rs @@ -99,7 +99,9 @@ impl SessionDatabase { .iter() // We don't support serializing / deserializing of other storage types. .filter(|(_, torrent)| { - torrent.storage_factory.type_id() == TypeId::of::() + torrent + .storage_factory + .is_type_id(TypeId::of::()) }) .map(|(id, torrent)| { ( diff --git a/crates/librqbit/src/storage/mod.rs b/crates/librqbit/src/storage/mod.rs index c85df48..f2420f5 100644 --- a/crates/librqbit/src/storage/mod.rs +++ b/crates/librqbit/src/storage/mod.rs @@ -4,7 +4,10 @@ pub mod mmap; pub mod slow; pub mod timing; -use std::{any::Any, path::Path}; +use std::{ + any::{Any, TypeId}, + path::Path, +}; use crate::torrent_state::ManagedTorrentInfo; @@ -12,6 +15,9 @@ 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>>; @@ -33,6 +39,10 @@ impl StorageFactoryExt for SF { 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 }) diff --git a/crates/librqbit/src/storage/slow.rs b/crates/librqbit/src/storage/slow.rs index ac2c315..28f4dba 100644 --- a/crates/librqbit/src/storage/slow.rs +++ b/crates/librqbit/src/storage/slow.rs @@ -24,6 +24,10 @@ impl StorageFactory for SlowStorageFactory { 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 { diff --git a/crates/librqbit/src/storage/timing.rs b/crates/librqbit/src/storage/timing.rs index 7893765..7451cd3 100644 --- a/crates/librqbit/src/storage/timing.rs +++ b/crates/librqbit/src/storage/timing.rs @@ -23,6 +23,10 @@ impl StorageFactory for TimingStorageFactory { 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 {