From b4512e48098990e76ad4c9b2b7ccc38f6ee1af67 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Wed, 21 Aug 2024 16:06:16 +0100 Subject: [PATCH] Move some fields into ManagedTorrentInfo --- crates/librqbit/src/session.rs | 8 ++++---- crates/librqbit/src/session_persistence/json.rs | 1 + crates/librqbit/src/torrent_state/mod.rs | 10 ++++------ 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/crates/librqbit/src/session.rs b/crates/librqbit/src/session.rs index aa5261d..a9263d0 100644 --- a/crates/librqbit/src/session.rs +++ b/crates/librqbit/src/session.rs @@ -1117,6 +1117,7 @@ impl Session { let span = error_span!(parent: self.rs(), "torrent", id); let peer_opts = self.merge_peer_opts(opts.peer_opts); let minfo = Arc::new(ManagedTorrentInfo { + id, span, file_infos, info, @@ -1127,6 +1128,7 @@ impl Session { spawner: self.spawner, peer_id: self.peer_id, lengths, + storage_factory, options: ManagedTorrentOptions { force_tracker_interval: opts.force_tracker_interval, peer_connect_timeout: peer_opts.connect_timeout, @@ -1141,16 +1143,14 @@ impl Session { let initializing = Arc::new(TorrentStateInitializing::new( minfo.clone(), only_files.clone(), - storage_factory.create_and_init(&minfo)?, + minfo.storage_factory.create_and_init(&minfo)?, )); let handle = Arc::new(ManagedTorrent { - id, locked: RwLock::new(ManagedTorrentLocked { state: ManagedTorrentState::Initializing(initializing), only_files, }), state_change_notify: Notify::new(), - storage_factory, info: minfo, session: Arc::downgrade(self), }); @@ -1257,7 +1257,7 @@ impl Session { _ => None, }) .map(Ok) - .unwrap_or_else(|| removed.storage_factory.create(removed.info())); + .unwrap_or_else(|| removed.info.storage_factory.create(removed.info())); match (storage, delete_files) { (Err(e), true) => return Err(e).context("torrent deleted, but could not delete files"), diff --git a/crates/librqbit/src/session_persistence/json.rs b/crates/librqbit/src/session_persistence/json.rs index d415375..b8cc7dc 100644 --- a/crates/librqbit/src/session_persistence/json.rs +++ b/crates/librqbit/src/session_persistence/json.rs @@ -128,6 +128,7 @@ impl JsonSessionPersistenceStore { write_torrent_file: bool, ) -> anyhow::Result<()> { if !torrent + .info .storage_factory .is_type_id(TypeId::of::()) { diff --git a/crates/librqbit/src/torrent_state/mod.rs b/crates/librqbit/src/torrent_state/mod.rs index c8c5846..9da473b 100644 --- a/crates/librqbit/src/torrent_state/mod.rs +++ b/crates/librqbit/src/torrent_state/mod.rs @@ -100,6 +100,7 @@ pub(crate) struct ManagedTorrentOptions { } pub struct ManagedTorrentInfo { + pub id: TorrentId, pub info: TorrentMetaV1Info, pub torrent_bytes: Bytes, pub info_bytes: Bytes, @@ -112,14 +113,11 @@ pub struct ManagedTorrentInfo { pub span: tracing::Span, pub(crate) options: ManagedTorrentOptions, pub(crate) connector: Arc, + pub(crate) storage_factory: BoxStorageFactory, } pub struct ManagedTorrent { - pub id: TorrentId, - // TODO: merge ManagedTorrent and ManagedTorrentInfo pub info: Arc, - pub(crate) storage_factory: BoxStorageFactory, - pub(crate) session: Weak, pub(crate) state_change_notify: Notify, pub(crate) locked: RwLock, @@ -127,7 +125,7 @@ pub struct ManagedTorrent { impl ManagedTorrent { pub fn id(&self) -> TorrentId { - self.id + self.info.id } pub fn info(&self) -> &ManagedTorrentInfo { @@ -369,7 +367,7 @@ impl ManagedTorrent { let initializing = Arc::new(TorrentStateInitializing::new( self.info.clone(), g.only_files.clone(), - self.storage_factory.create_and_init(self.info())?, + self.info.storage_factory.create_and_init(self.info())?, )); g.state = ManagedTorrentState::Initializing(initializing.clone()); drop(g);