diff --git a/crates/librqbit/examples/storage.rs b/crates/librqbit/examples/storage.rs index 9343331..3270db0 100644 --- a/crates/librqbit/examples/storage.rs +++ b/crates/librqbit/examples/storage.rs @@ -8,26 +8,26 @@ struct DummyStorage {} impl StorageFactory for DummyStorage { fn init_storage( &self, - info: &ManagedTorrentInfo, + _info: &ManagedTorrentInfo, ) -> anyhow::Result> { Ok(Box::new(DummyStorage {})) } } impl TorrentStorage for DummyStorage { - 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<()> { anyhow::bail!("pread_exact") } - fn pwrite_all(&self, file_id: usize, offset: u64, buf: &[u8]) -> anyhow::Result<()> { + fn pwrite_all(&self, _file_id: usize, _offset: u64, _buf: &[u8]) -> anyhow::Result<()> { anyhow::bail!("pwrite_all") } - fn remove_file(&self, file_id: usize, filename: &std::path::Path) -> anyhow::Result<()> { + fn remove_file(&self, _file_id: usize, _filename: &std::path::Path) -> anyhow::Result<()> { anyhow::bail!("remove_file") } - fn ensure_file_length(&self, file_id: usize, length: u64) -> anyhow::Result<()> { + fn ensure_file_length(&self, _file_id: usize, _length: u64) -> anyhow::Result<()> { anyhow::bail!("ensure_file_length") } @@ -62,9 +62,13 @@ async fn main() -> anyhow::Result<()> { ), Some(librqbit::AddTorrentOptions { storage_factory: Some(Box::new(DummyStorage {})), + paused: true, ..Default::default() }), ) - .await?; + .await? + .into_handle() + .unwrap(); + handle.wait_until_initialized().await?; Ok(()) } diff --git a/crates/librqbit/src/torrent_state/mod.rs b/crates/librqbit/src/torrent_state/mod.rs index 54310a0..aedd24b 100644 --- a/crates/librqbit/src/torrent_state/mod.rs +++ b/crates/librqbit/src/torrent_state/mod.rs @@ -405,6 +405,26 @@ impl ManagedTorrent { }) } + #[inline(never)] + pub fn wait_until_initialized(&self) -> BoxFuture<'_, anyhow::Result<()>> { + async move { + // TODO: rewrite, this polling is horrible + loop { + let done = self.with_state(|s| match s { + ManagedTorrentState::Initializing(_) => Ok(false), + ManagedTorrentState::Error(e) => bail!("{:?}", e), + ManagedTorrentState::None => bail!("bug: torrent state is None"), + _ => Ok(true), + })?; + if done { + return Ok(()); + } + tokio::time::sleep(Duration::from_secs(1)).await; + } + } + .boxed() + } + #[inline(never)] pub fn wait_until_completed(&self) -> BoxFuture<'_, anyhow::Result<()>> { async move {