Inline(never) more things for rqbit(bin) to compile faster

This commit is contained in:
Igor Katson 2024-02-26 23:08:47 +00:00
parent f42007f436
commit 34f3ec6c29
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
5 changed files with 72 additions and 47 deletions

View file

@ -45,6 +45,7 @@ impl HttpApi {
/// Run the HTTP server forever on the given address.
/// If read_only is passed, no state-modifying methods will be exposed.
#[inline(never)]
pub fn make_http_api_and_run(
self,
addr: SocketAddr,

View file

@ -1,4 +1,7 @@
use std::pin::Pin;
use anyhow::Context;
use futures::{Future, FutureExt};
use serde::Deserialize;
use crate::{
@ -59,6 +62,7 @@ async fn json_response<T: serde::de::DeserializeOwned + std::any::Any>(
}
impl HttpApiClient {
#[inline(never)]
pub fn new(url: &str) -> anyhow::Result<Self> {
Ok(Self {
base_url: reqwest::Url::parse(url)?,
@ -70,40 +74,47 @@ impl HttpApiClient {
&self.base_url
}
pub async fn validate_rqbit_server(&self) -> anyhow::Result<()> {
let response = self.client.get(self.base_url.clone()).send().await?;
let root: ApiRoot = json_response(response).await?;
if root.server == "rqbit" {
return Ok(());
#[inline(never)]
pub fn validate_rqbit_server(&self) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + '_>> {
async move {
let response = self.client.get(self.base_url.clone()).send().await?;
let root: ApiRoot = json_response(response).await?;
if root.server == "rqbit" {
return Ok(());
}
anyhow::bail!("not an rqbit server at {}", &self.base_url)
}
anyhow::bail!("not an rqbit server at {}", &self.base_url)
.boxed()
}
pub async fn add_torrent(
&self,
torrent: AddTorrent<'_>,
pub fn add_torrent<'a>(
&'a self,
torrent: AddTorrent<'a>,
opts: Option<AddTorrentOptions>,
) -> anyhow::Result<ApiAddTorrentResponse> {
let opts = opts.unwrap_or_default();
let params = TorrentAddQueryParams {
overwrite: Some(opts.overwrite),
only_files_regex: opts.only_files_regex,
only_files: None,
output_folder: opts.output_folder,
sub_folder: opts.sub_folder,
list_only: Some(opts.list_only),
..Default::default()
};
let qs = serde_urlencoded::to_string(&params).unwrap();
let url = format!("{}torrents?{}", &self.base_url, qs);
let response = check_response(
self.client
.post(&url)
.body(torrent.into_bytes())
.send()
.await?,
)
.await?;
json_response(response).await
) -> Pin<Box<dyn Future<Output = anyhow::Result<ApiAddTorrentResponse>> + 'a>> {
async move {
let opts = opts.unwrap_or_default();
let params = TorrentAddQueryParams {
overwrite: Some(opts.overwrite),
only_files_regex: opts.only_files_regex,
only_files: None,
output_folder: opts.output_folder,
sub_folder: opts.sub_folder,
list_only: Some(opts.list_only),
..Default::default()
};
let qs = serde_urlencoded::to_string(&params).unwrap();
let url = format!("{}torrents?{}", &self.base_url, qs);
let response = check_response(
self.client
.post(&url)
.body(torrent.into_bytes())
.send()
.await?,
)
.await?;
json_response(response).await
}
.boxed()
}
}

View file

@ -298,6 +298,7 @@ pub enum AddTorrent<'a> {
impl<'a> AddTorrent<'a> {
// Don't call this from HTTP API.
#[inline(never)]
pub fn from_cli_argument(path: &'a str) -> anyhow::Result<Self> {
if SUPPORTED_SCHEMES.iter().any(|s| path.starts_with(s)) {
return Ok(Self::Url(Cow::Borrowed(path)));
@ -314,6 +315,7 @@ impl<'a> AddTorrent<'a> {
}
// Don't call this from HTTP API.
#[inline(never)]
pub fn from_local_filename(filename: &str) -> anyhow::Result<Self> {
let file = read_local_file_including_stdin(filename)
.with_context(|| format!("error reading local file {filename:?}"))?;
@ -744,6 +746,7 @@ impl Session {
}
/// Add a torrent to the session.
#[inline(never)]
pub fn add_torrent<'a>(
&'a self,
add: AddTorrent<'a>,

View file

@ -8,6 +8,7 @@ use std::collections::HashSet;
use std::net::SocketAddr;
use std::path::Path;
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;
@ -15,6 +16,8 @@ use std::time::Duration;
use anyhow::bail;
use anyhow::Context;
use buffers::ByteString;
use futures::Future;
use futures::FutureExt;
use librqbit_core::hash_id::Id20;
use librqbit_core::lengths::Lengths;
use librqbit_core::peer_id::generate_peer_id;
@ -395,23 +398,29 @@ impl ManagedTorrent {
})
}
pub async fn wait_until_completed(&self) -> anyhow::Result<()> {
// TODO: rewrite, this polling is horrible
let live = loop {
let live = self.with_state(|s| match s {
ManagedTorrentState::Initializing(_) | ManagedTorrentState::Paused(_) => Ok(None),
ManagedTorrentState::Live(l) => Ok(Some(l.clone())),
ManagedTorrentState::Error(e) => bail!("{:?}", e),
ManagedTorrentState::None => bail!("bug: torrent state is None"),
})?;
if let Some(live) = live {
break live;
}
tokio::time::sleep(Duration::from_secs(1)).await;
};
#[inline(never)]
pub fn wait_until_completed(&self) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + '_>> {
async move {
// TODO: rewrite, this polling is horrible
let live = loop {
let live = self.with_state(|s| match s {
ManagedTorrentState::Initializing(_) | ManagedTorrentState::Paused(_) => {
Ok(None)
}
ManagedTorrentState::Live(l) => Ok(Some(l.clone())),
ManagedTorrentState::Error(e) => bail!("{:?}", e),
ManagedTorrentState::None => bail!("bug: torrent state is None"),
})?;
if let Some(live) = live {
break live;
}
tokio::time::sleep(Duration::from_secs(1)).await;
};
live.wait_until_completed().await;
Ok(())
live.wait_until_completed().await;
Ok(())
}
.boxed()
}
}

View file

@ -159,6 +159,7 @@ impl<BufType: AsRef<[u8]>> TorrentMetaV1Info<BufType> {
Some(expected_hash == hash)
}
#[inline(never)]
pub fn iter_filenames_and_lengths(
&self,
) -> anyhow::Result<impl Iterator<Item = (FileIteratorName<'_, BufType>, u64)>> {