Shorten Pin<Box to BoxFuture

This commit is contained in:
Igor Katson 2024-02-27 08:14:39 +00:00
parent 1b79b66cc3
commit a001bb8c97
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
6 changed files with 19 additions and 27 deletions

View file

@ -1,7 +1,6 @@
use std::{
cmp::Reverse,
net::SocketAddr,
pin::Pin,
str::FromStr,
sync::{
atomic::{AtomicU16, Ordering},
@ -24,7 +23,9 @@ use anyhow::{bail, Context};
use backoff::{backoff::Backoff, ExponentialBackoffBuilder};
use bencode::ByteString;
use dashmap::DashMap;
use futures::{stream::FuturesUnordered, Future, FutureExt, Stream, StreamExt, TryFutureExt};
use futures::{
future::BoxFuture, stream::FuturesUnordered, FutureExt, Stream, StreamExt, TryFutureExt,
};
use leaky_bucket::RateLimiter;
use librqbit_core::{
@ -1147,9 +1148,7 @@ impl DhtState {
}
#[inline(never)]
pub fn with_config(
mut config: DhtConfig,
) -> Pin<Box<dyn Future<Output = anyhow::Result<Arc<Self>>> + Send>> {
pub fn with_config(mut config: DhtConfig) -> BoxFuture<'static, anyhow::Result<Arc<Self>>> {
async move {
let socket = match config.listen_addr {
Some(addr) => UdpSocket::bind(addr)

View file

@ -1,6 +1,7 @@
// TODO: this now stores only the routing table, but we also need AT LEAST the same socket address...
use futures::{Future, FutureExt};
use futures::future::BoxFuture;
use futures::FutureExt;
use librqbit_core::directories::get_configuration_directory;
use librqbit_core::spawn_utils::spawn_with_cancel;
use serde::{Deserialize, Serialize};
@ -8,7 +9,6 @@ use std::fs::OpenOptions;
use std::io::{BufReader, BufWriter};
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::time::Duration;
use tokio_util::sync::CancellationToken;
@ -81,7 +81,7 @@ impl PersistentDht {
pub fn create(
config: Option<PersistentDhtConfig>,
cancellation_token: Option<CancellationToken>,
) -> Pin<Box<dyn Future<Output = anyhow::Result<Dht>> + Send>> {
) -> BoxFuture<'static, anyhow::Result<Dht>> {
async move {
let mut config = config.unwrap_or_default();
let config_filename = match config.config_filename.take() {

View file

@ -3,12 +3,12 @@ use axum::body::Bytes;
use axum::extract::{Path, Query, State};
use axum::response::IntoResponse;
use axum::routing::{get, post};
use futures::{Future, FutureExt, TryStreamExt};
use futures::future::BoxFuture;
use futures::{FutureExt, TryStreamExt};
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
use std::pin::Pin;
use std::str::FromStr;
use std::time::Duration;
use tracing::{debug, info};
@ -46,10 +46,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,
) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send>> {
pub fn make_http_api_and_run(self, addr: SocketAddr) -> BoxFuture<'static, anyhow::Result<()>> {
let state = self.inner;
async fn api_root() -> impl IntoResponse {

View file

@ -1,7 +1,5 @@
use std::pin::Pin;
use anyhow::Context;
use futures::{Future, FutureExt};
use futures::{future::BoxFuture, FutureExt};
use serde::Deserialize;
use crate::{
@ -75,7 +73,7 @@ impl HttpApiClient {
}
#[inline(never)]
pub fn validate_rqbit_server(&self) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + '_>> {
pub fn validate_rqbit_server(&self) -> BoxFuture<'_, anyhow::Result<()>> {
async move {
let response = self.client.get(self.base_url.clone()).send().await?;
let root: ApiRoot = json_response(response).await?;
@ -91,7 +89,7 @@ impl HttpApiClient {
&'a self,
torrent: AddTorrent<'a>,
opts: Option<AddTorrentOptions>,
) -> Pin<Box<dyn Future<Output = anyhow::Result<ApiAddTorrentResponse>> + 'a>> {
) -> BoxFuture<'a, anyhow::Result<ApiAddTorrentResponse>> {
async move {
let opts = opts.unwrap_or_default();
let params = TorrentAddQueryParams {

View file

@ -4,7 +4,6 @@ use std::{
io::{BufReader, BufWriter, Read},
net::SocketAddr,
path::PathBuf,
pin::Pin,
str::FromStr,
sync::Arc,
time::Duration,
@ -25,7 +24,7 @@ use bencode::{bencode_serialize_to_writer, BencodeDeserializer};
use buffers::{ByteBuf, ByteBufT, ByteString};
use clone_to_owned::CloneToOwned;
use dht::{Dht, DhtBuilder, DhtConfig, Id20, PersistentDht, PersistentDhtConfig};
use futures::{stream::FuturesUnordered, Future, FutureExt, TryFutureExt};
use futures::{future::BoxFuture, stream::FuturesUnordered, FutureExt, TryFutureExt};
use librqbit_core::{
directories::get_configuration_directory,
magnet::Magnet,
@ -381,7 +380,7 @@ pub(crate) struct CheckedIncomingConnection {
impl Session {
/// Create a new session. The passed in folder will be used as a default unless overriden per torrent.
#[inline(never)]
pub fn new(output_folder: PathBuf) -> Pin<Box<dyn Future<Output = anyhow::Result<Arc<Self>>>>> {
pub fn new(output_folder: PathBuf) -> BoxFuture<'static, anyhow::Result<Arc<Self>>> {
Self::new_with_opts(output_folder, SessionOptions::default())
}
@ -399,7 +398,7 @@ impl Session {
pub fn new_with_opts(
output_folder: PathBuf,
mut opts: SessionOptions,
) -> Pin<Box<dyn Future<Output = anyhow::Result<Arc<Self>>>>> {
) -> BoxFuture<'static, anyhow::Result<Arc<Self>>> {
async move {
let peer_id = opts.peer_id.unwrap_or_else(generate_peer_id);
let token = CancellationToken::new();
@ -751,7 +750,7 @@ impl Session {
&'a self,
add: AddTorrent<'a>,
opts: Option<AddTorrentOptions>,
) -> Pin<Box<dyn Future<Output = anyhow::Result<AddTorrentResponse>> + Send + 'a>> {
) -> BoxFuture<'a, anyhow::Result<AddTorrentResponse>> {
async move {
// Magnet links are different in that we first need to discover the metadata.
let span = error_span!("add_torrent");

View file

@ -8,7 +8,6 @@ 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;
@ -16,7 +15,7 @@ use std::time::Duration;
use anyhow::bail;
use anyhow::Context;
use buffers::ByteString;
use futures::Future;
use futures::future::BoxFuture;
use futures::FutureExt;
use librqbit_core::hash_id::Id20;
use librqbit_core::lengths::Lengths;
@ -399,7 +398,7 @@ impl ManagedTorrent {
}
#[inline(never)]
pub fn wait_until_completed(&self) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + '_>> {
pub fn wait_until_completed(&self) -> BoxFuture<'_, anyhow::Result<()>> {
async move {
// TODO: rewrite, this polling is horrible
let live = loop {