Custom storage example

This commit is contained in:
Igor Katson 2024-04-30 09:36:31 +01:00
parent 6c3dfbc52f
commit f2ae2f67f4
2 changed files with 30 additions and 6 deletions

View file

@ -8,26 +8,26 @@ struct DummyStorage {}
impl StorageFactory for DummyStorage { impl StorageFactory for DummyStorage {
fn init_storage( fn init_storage(
&self, &self,
info: &ManagedTorrentInfo, _info: &ManagedTorrentInfo,
) -> anyhow::Result<Box<dyn librqbit::storage::TorrentStorage>> { ) -> anyhow::Result<Box<dyn librqbit::storage::TorrentStorage>> {
Ok(Box::new(DummyStorage {})) Ok(Box::new(DummyStorage {}))
} }
} }
impl TorrentStorage for 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") 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") 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") 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") anyhow::bail!("ensure_file_length")
} }
@ -62,9 +62,13 @@ async fn main() -> anyhow::Result<()> {
), ),
Some(librqbit::AddTorrentOptions { Some(librqbit::AddTorrentOptions {
storage_factory: Some(Box::new(DummyStorage {})), storage_factory: Some(Box::new(DummyStorage {})),
paused: true,
..Default::default() ..Default::default()
}), }),
) )
.await?; .await?
.into_handle()
.unwrap();
handle.wait_until_initialized().await?;
Ok(()) Ok(())
} }

View file

@ -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)] #[inline(never)]
pub fn wait_until_completed(&self) -> BoxFuture<'_, anyhow::Result<()>> { pub fn wait_until_completed(&self) -> BoxFuture<'_, anyhow::Result<()>> {
async move { async move {