Skip to content

Commit

Permalink
Rust 1.75 (#736)
Browse files Browse the repository at this point in the history
The `async fn` and return-position `impl Trait` in traits improve
`BuildContext` ergonomics. The traits use `impl Future` over `async fn`
to make the send bound explicit
(https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html).

The remaining changes are due to clippy.
  • Loading branch information
konstin authored Dec 28, 2023
1 parent 7bf2790 commit 2d4cb1e
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 46 deletions.
8 changes: 8 additions & 0 deletions crates/distribution-types/src/index_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,11 @@ impl<'a> IntoIterator for &'a IndexUrls {
self.index.iter().chain(self.extra_index.iter())
}
}

impl<'a> IndexUrls {
pub fn iter(
&'a self,
) -> Chain<std::option::Iter<'a, IndexUrl>, std::slice::Iter<'a, IndexUrl>> {
self.into_iter()
}
}
7 changes: 2 additions & 5 deletions crates/puffin-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::future::Future;
use std::io;
use std::io::BufRead;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::process::Output;
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -544,16 +543,14 @@ impl SourceBuild {
}

impl SourceBuildTrait for SourceBuild {
fn metadata<'a>(
&'a mut self,
) -> Pin<Box<dyn Future<Output = anyhow::Result<Option<PathBuf>>> + Send + 'a>> {
fn metadata(&mut self) -> impl Future<Output = anyhow::Result<Option<PathBuf>>> + Send {
Box::pin(async { Ok(self.get_metadata_without_build().await?) })
}

fn wheel<'a>(
&'a self,
wheel_dir: &'a Path,
) -> Pin<Box<dyn Future<Output = anyhow::Result<String>> + Send + 'a>> {
) -> impl Future<Output = anyhow::Result<String>> + Send + 'a {
Box::pin(async { Ok(self.build(wheel_dir).await?) })
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/puffin-dev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ enum Cli {
ResolveMany(ResolveManyArgs),
InstallMany(InstallManyArgs),
/// Resolve requirements passed on the CLI
ResolveCli(ResolveCliArgs),
Resolve(ResolveCliArgs),
WheelMetadata(WheelMetadataArgs),
}

Expand All @@ -75,7 +75,7 @@ async fn run() -> Result<()> {
Cli::InstallMany(args) => {
install_many::install_many(args).await?;
}
Cli::ResolveCli(args) => {
Cli::Resolve(args) => {
resolve_cli::resolve_cli(args).await?;
}
Cli::WheelMetadata(args) => wheel_metadata::wheel_metadata(args).await?,
Expand Down
7 changes: 3 additions & 4 deletions crates/puffin-dispatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use std::future::Future;
use std::path::{Path, PathBuf};
use std::pin::Pin;

use anyhow::{bail, Context, Result};
use itertools::Itertools;
Expand Down Expand Up @@ -86,7 +85,7 @@ impl<'a> BuildContext for BuildDispatch<'a> {
fn resolve<'data>(
&'data self,
requirements: &'data [Requirement],
) -> Pin<Box<dyn Future<Output = Result<Resolution>> + Send + 'data>> {
) -> impl Future<Output = Result<Resolution>> + Send + 'data {
Box::pin(async {
let markers = self.interpreter.markers();
let tags = self.interpreter.tags()?;
Expand Down Expand Up @@ -119,7 +118,7 @@ impl<'a> BuildContext for BuildDispatch<'a> {
&'data self,
resolution: &'data Resolution,
venv: &'data Virtualenv,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'data>> {
) -> impl Future<Output = Result<()>> + Send + 'data {
Box::pin(async move {
debug!(
"Installing in {} in {}",
Expand Down Expand Up @@ -223,7 +222,7 @@ impl<'a> BuildContext for BuildDispatch<'a> {
subdirectory: Option<&'data Path>,
package_id: &'data str,
build_kind: BuildKind,
) -> Pin<Box<dyn Future<Output = Result<SourceBuild>> + Send + 'data>> {
) -> impl Future<Output = Result<SourceBuild>> + Send + 'data {
Box::pin(async move {
if self.no_build {
bail!("Building source distributions is disabled");
Expand Down
9 changes: 4 additions & 5 deletions crates/puffin-resolver/src/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Given a set of requirements, find a set of compatible packages.
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use anyhow::Result;
Expand Down Expand Up @@ -52,7 +51,7 @@ pub trait ResolverProvider: Send + Sync {
fn get_version_map<'io>(
&'io self,
package_name: &'io PackageName,
) -> Pin<Box<dyn Future<Output = VersionMapResponse> + Send + 'io>>;
) -> impl Future<Output = VersionMapResponse> + Send + 'io;

/// Get the metadata for a distribution.
///
Expand All @@ -62,7 +61,7 @@ pub trait ResolverProvider: Send + Sync {
fn get_or_build_wheel_metadata<'io>(
&'io self,
dist: &'io Dist,
) -> Pin<Box<dyn Future<Output = WheelMetadataResponse> + Send + 'io>>;
) -> impl Future<Output = WheelMetadataResponse> + Send + 'io;

/// Set the [`Reporter`] to use for this installer.
#[must_use]
Expand Down Expand Up @@ -109,7 +108,7 @@ impl<'a, Context: BuildContext + Send + Sync> ResolverProvider
fn get_version_map<'io>(
&'io self,
package_name: &'io PackageName,
) -> Pin<Box<dyn Future<Output = VersionMapResponse> + Send + 'io>> {
) -> impl Future<Output = VersionMapResponse> + Send + 'io {
Box::pin(
self.client
.simple(package_name)
Expand All @@ -134,7 +133,7 @@ impl<'a, Context: BuildContext + Send + Sync> ResolverProvider
fn get_or_build_wheel_metadata<'io>(
&'io self,
dist: &'io Dist,
) -> Pin<Box<dyn Future<Output = WheelMetadataResponse> + Send + 'io>> {
) -> impl Future<Output = WheelMetadataResponse> + Send + 'io {
Box::pin(self.fetcher.get_or_build_wheel_metadata(dist))
}

Expand Down
26 changes: 8 additions & 18 deletions crates/puffin-resolver/tests/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
//! Integration tests for the resolver. These tests rely on a live network connection, and hit
//! `PyPI` directly.
use std::future::Future;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::str::FromStr;

use anyhow::Result;
Expand Down Expand Up @@ -51,45 +49,37 @@ impl BuildContext for DummyContext {
panic!("The test should not need to build source distributions")
}

fn resolve<'a>(
&'a self,
_requirements: &'a [Requirement],
) -> Pin<Box<dyn Future<Output = Result<Resolution>> + Send + 'a>> {
async fn resolve<'a>(&'a self, _requirements: &'a [Requirement]) -> Result<Resolution> {
panic!("The test should not need to build source distributions")
}

fn install<'a>(
async fn install<'a>(
&'a self,
_resolution: &'a Resolution,
_venv: &'a Virtualenv,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
) -> Result<()> {
panic!("The test should not need to build source distributions")
}

fn setup_build<'a>(
async fn setup_build<'a>(
&'a self,
_source: &'a Path,
_subdirectory: Option<&'a Path>,
_package_id: &'a str,
_build_kind: BuildKind,
) -> Pin<Box<dyn Future<Output = Result<Self::SourceDistBuilder>> + Send + 'a>> {
Box::pin(async { Ok(DummyBuilder) })
) -> Result<Self::SourceDistBuilder> {
Ok(DummyBuilder)
}
}

struct DummyBuilder;

impl SourceBuildTrait for DummyBuilder {
fn metadata<'a>(
&'a mut self,
) -> Pin<Box<dyn Future<Output = Result<Option<PathBuf>>> + Send + 'a>> {
async fn metadata(&mut self) -> Result<Option<PathBuf>> {
panic!("The test should not need to build source distributions")
}

fn wheel<'a>(
&'a self,
_wheel_dir: &'a Path,
) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'a>> {
async fn wheel<'a>(&'a self, _wheel_dir: &'a Path) -> Result<String> {
panic!("The test should not need to build source distributions")
}
}
Expand Down
17 changes: 6 additions & 11 deletions crates/puffin-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::fmt::{Display, Formatter};
use std::future::Future;
use std::path::{Path, PathBuf};
use std::pin::Pin;

use anyhow::Result;

Expand Down Expand Up @@ -77,15 +76,15 @@ pub trait BuildContext {
fn resolve<'a>(
&'a self,
requirements: &'a [Requirement],
) -> Pin<Box<dyn Future<Output = Result<Resolution>> + Send + 'a>>;
) -> impl Future<Output = Result<Resolution>> + Send + 'a;

/// Install the given set of package versions into the virtual environment. The environment must
/// use the same base python as [`BuildContext::base_python`]
fn install<'a>(
&'a self,
resolution: &'a Resolution,
venv: &'a Virtualenv,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>;
) -> impl Future<Output = Result<()>> + Send + 'a;

/// Setup a source distribution build by installing the required dependencies. A wrapper for
/// `puffin_build::SourceBuild::setup`.
Expand All @@ -99,7 +98,7 @@ pub trait BuildContext {
subdirectory: Option<&'a Path>,
package_id: &'a str,
build_kind: BuildKind,
) -> Pin<Box<dyn Future<Output = Result<Self::SourceDistBuilder>> + Send + 'a>>;
) -> impl Future<Output = Result<Self::SourceDistBuilder>> + Send + 'a;
}

/// A wrapper for `puffin_build::SourceBuild` to avoid cyclical crate dependencies.
Expand All @@ -113,19 +112,15 @@ pub trait SourceBuildTrait {
///
/// Returns the metadata directory if we're having a PEP 517 build and the
/// `prepare_metadata_for_build_wheel` hook exists
fn metadata<'a>(
&'a mut self,
) -> Pin<Box<dyn Future<Output = Result<Option<PathBuf>>> + Send + 'a>>;
fn metadata(&mut self) -> impl Future<Output = Result<Option<PathBuf>>> + Send;

/// A wrapper for `puffin_build::SourceBuild::build`.
///
/// For PEP 517 builds, this calls `build_wheel`.
///
/// Returns the filename of the built wheel inside the given `wheel_dir`.
fn wheel<'a>(
&'a self,
wheel_dir: &'a Path,
) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'a>>;
fn wheel<'a>(&'a self, wheel_dir: &'a Path)
-> impl Future<Output = Result<String>> + Send + 'a;
}

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "1.74"
channel = "1.75"

0 comments on commit 2d4cb1e

Please sign in to comment.