Repository: astral-sh/uv
File: crates/uv-build-frontend/src/lib.rs (Rust)

Code before:
            })
            .join(", ");

        // > Projects can specify that their backend code is hosted in-tree by including the
        // > backend-path key in pyproject.toml. This key contains a list of directories, which the
        // > frontend will add to the start of sys.path when loading the backend, and running the
        // > backend hooks.
        formatdoc! {r#"
            import sys

            if sys.path[0] == "":
                sys.path.pop(0)

            sys.path = [{backend_path}] + sys.path

            {import}
        "#, backend_path = backend_path_encoded}
    }

    fn is_setuptools(&self) -> bool {
        // either `setuptools.build_meta` or `setuptools.build_meta:__legacy__`
        self.backend.split(':').next() == Some("setuptools.build_meta")
    }
}

/// Uses an [`Arc`] internally, clone freely.
#[derive(Debug, Clone)]
pub struct SourceBuildContext {
    /// An in-memory resolution of the default backend's requirements for PEP 517 builds.
    default_resolution: Arc<Mutex<Option<Resolution>>>,
    /// A shared semaphore to limit the number of concurrent builds.
    concurrency: Arc<Semaphore>,
}

impl SourceBuildContext {
    /// Create a [`SourceBuildContext`] with the given concurrent build limit.
    pub fn new(concurrent_builds: usize) -> Self {
        Self {
            default_resolution: Arc::default(),
            concurrency: Arc::new(Semaphore::new(concurrent_builds)),
        }
    }
}

/// Holds the state through a series of PEP 517 frontend to backend calls or a single `setup.py`
/// invocation.
///
/// This keeps both the temp dir and the result of a potential `prepare_metadata_for_build_wheel`
/// call which changes how we call `build_wheel`.
pub struct SourceBuild {
    temp_dir: TempDir,

Review comment (suggestion):
Using an `Arc` in the expectation that we want to share the currency limits globally, and its cost wrt to `Rc` is negligible.

Code after:
            })
            .join(", ");

        // > Projects can specify that their backend code is hosted in-tree by including the
        // > backend-path key in pyproject.toml. This key contains a list of directories, which the
        // > frontend will add to the start of sys.path when loading the backend, and running the
        // > backend hooks.
        formatdoc! {r#"
            import sys

            if sys.path[0] == "":
                sys.path.pop(0)

            sys.path = [{backend_path}] + sys.path

            {import}
        "#, backend_path = backend_path_encoded}
    }

    fn is_setuptools(&self) -> bool {
        // either `setuptools.build_meta` or `setuptools.build_meta:__legacy__`
        self.backend.split(':').next() == Some("setuptools.build_meta")
    }
}

/// Uses an [`Arc`] internally, clone freely.
#[derive(Debug, Clone)]
pub struct SourceBuildContext {
    /// An in-memory resolution of the default backend's requirements for PEP 517 builds.
    default_resolution: Arc<Mutex<Option<Resolution>>>,
    /// A shared semaphore to limit the number of concurrent builds.
    concurrent_build_slots: Arc<Semaphore>,
}

impl SourceBuildContext {
    /// Create a [`SourceBuildContext`] with the given concurrent build limit.
    pub fn new(concurrent_builds: usize) -> Self {
        Self {
            default_resolution: Arc::default(),
            concurrent_build_slots: Arc::new(Semaphore::new(concurrent_builds)),
        }
    }
}

/// Holds the state through a series of PEP 517 frontend to backend calls or a single `setup.py`
/// invocation.
///
/// This keeps both the temp dir and the result of a potential `prepare_metadata_for_build_wheel`
/// call which changes how we call `build_wheel`.
pub struct SourceBuild {
    temp_dir: TempDir,