Skip to content

Commit

Permalink
fix: trait impl override (#1848)
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra authored Aug 20, 2024
1 parent 6315217 commit 090b18e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
4 changes: 0 additions & 4 deletions src/lock_file/package_identifier.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use rattler_conda_types::{PackageUrl, RepoDataRecord};
use std::{collections::HashSet, str::FromStr};
use thiserror::Error;
use url::Url;

use pixi_manifest::pypi::PyPiPackageName;
use uv_normalize::{ExtraName, InvalidNameError, PackageName};
Expand All @@ -12,7 +11,6 @@ use uv_normalize::{ExtraName, InvalidNameError, PackageName};
pub struct PypiPackageIdentifier {
pub name: PyPiPackageName,
pub version: pep440_rs::Version,
pub url: Url,
pub extras: HashSet<ExtraName>,
}

Expand Down Expand Up @@ -68,7 +66,6 @@ impl PypiPackageIdentifier {
result.push(PypiPackageIdentifier {
name: PyPiPackageName::from_normalized(name),
version,
url: record.url.clone(),
// TODO: We can't really tell which python extras are enabled in a conda package.
extras: Default::default(),
})
Expand Down Expand Up @@ -112,7 +109,6 @@ impl PypiPackageIdentifier {

Ok(Self {
name: PyPiPackageName::from_normalized(name),
url: Url::parse(&package_url.to_string()).expect("cannot parse purl -> url"),
version,
extras,
})
Expand Down
49 changes: 36 additions & 13 deletions src/lock_file/records_by_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@ use std::borrow::Borrow;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::hash::Hash;
use std::marker::PhantomData;

pub type RepoDataRecordsByName = DependencyRecordsByName<PackageName, RepoDataRecord>;
pub type PypiRecordsByName = DependencyRecordsByName<uv_normalize::PackageName, PypiRecord>;
pub type RepoDataRecordsByName =
DependencyRecordsByName<PackageName, VersionWithSource, RepoDataRecord>;
pub type PypiRecordsByName =
DependencyRecordsByName<uv_normalize::PackageName, pep440_rs::Version, PypiRecord>;

/// A trait required from the dependencies stored in DependencyRecordsByName
pub(crate) trait HasNameVersion<N> {
pub(crate) trait HasNameVersion<N, V: PartialOrd> {
fn name(&self) -> &N;
fn version(&self) -> &impl PartialOrd;
fn version(&self) -> &V;
}

impl HasNameVersion<uv_normalize::PackageName> for PypiRecord {
impl HasNameVersion<uv_normalize::PackageName, pep440_rs::Version> for PypiRecord {
fn name(&self) -> &uv_normalize::PackageName {
&self.0.name
}
fn version(&self) -> &pep440_rs::Version {
&self.0.version
}
}
impl HasNameVersion<PackageName> for RepoDataRecord {
impl HasNameVersion<PackageName, VersionWithSource> for RepoDataRecord {
fn name(&self) -> &PackageName {
&self.package_record.name
}
Expand All @@ -34,32 +37,44 @@ impl HasNameVersion<PackageName> for RepoDataRecord {

/// A struct that holds both a ``Vec` of `DependencyRecord` and a mapping from name to index.
#[derive(Clone, Debug)]
pub struct DependencyRecordsByName<N: Hash + Eq + Clone, D: HasNameVersion<N>> {
pub struct DependencyRecordsByName<N: Hash + Eq + Clone, V: PartialOrd, D: HasNameVersion<N, V>> {
pub records: Vec<D>,
by_name: HashMap<N, usize>,
_data: PhantomData<V>,
}

impl<N: Hash + Eq + Clone, D: HasNameVersion<N>> Default for DependencyRecordsByName<N, D> {
impl<N: Hash + Eq + Clone, V: PartialOrd, D: HasNameVersion<N, V>> Default
for DependencyRecordsByName<N, V, D>
{
fn default() -> Self {
Self {
records: Vec::new(),
by_name: HashMap::new(),
_data: PhantomData,
}
}
}

impl<N: Hash + Eq + Clone, D: HasNameVersion<N>> From<Vec<D>> for DependencyRecordsByName<N, D> {
impl<N: Hash + Eq + Clone, V: PartialOrd, D: HasNameVersion<N, V>> From<Vec<D>>
for DependencyRecordsByName<N, V, D>
{
fn from(records: Vec<D>) -> Self {
let by_name = records
.iter()
.enumerate()
.map(|(idx, record)| (record.name().clone(), idx))
.collect();
Self { records, by_name }
Self {
records,
by_name,
_data: PhantomData,
}
}
}

impl<N: Hash + Eq + Clone, D: HasNameVersion<N>> DependencyRecordsByName<N, D> {
impl<N: Hash + Eq + Clone, V: PartialOrd, D: HasNameVersion<N, V>>
DependencyRecordsByName<N, V, D>
{
/// Returns the record with the given name or `None` if no such record exists.
pub(crate) fn by_name<Q: ?Sized>(&self, key: &Q) -> Option<&D>
where
Expand Down Expand Up @@ -117,7 +132,11 @@ impl<N: Hash + Eq + Clone, D: HasNameVersion<N>> DependencyRecordsByName<N, D> {
}
}
}
Ok(Self { records, by_name })
Ok(Self {
records,
by_name,
_data: PhantomData,
})
}

/// Constructs a new instance from an iterator of repodata records. The records are
Expand All @@ -144,7 +163,11 @@ impl<N: Hash + Eq + Clone, D: HasNameVersion<N>> DependencyRecordsByName<N, D> {
}
}

Self { records, by_name }
Self {
records,
by_name,
_data: PhantomData,
}
}
}

Expand Down

0 comments on commit 090b18e

Please sign in to comment.