Skip to content

Commit

Permalink
chore: add Borrow impl for Extra and NormalizedPackageName (#89)
Browse files Browse the repository at this point in the history
This allows these types to be used in HashMaps which can be queried by
string.

I didnt implement it for `PackageName` because the hash of `PackageName`
is not equal to the hash of `String`.
  • Loading branch information
baszalmstra authored Nov 24, 2023
1 parent c7d2aae commit 2b31e8f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/rattler_installs_packages/src/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use miette::Diagnostic;
use serde::{Serialize, Serializer};
use serde_with::DeserializeFromStr;
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::str::FromStr;
Expand Down Expand Up @@ -110,3 +111,9 @@ impl Serialize for Extra {
self.source.as_ref().serialize(serializer)
}
}

impl Borrow<str> for Extra {
fn borrow(&self) -> &str {
self.normalized.as_ref()
}
}
7 changes: 7 additions & 0 deletions crates/rattler_installs_packages/src/package_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use miette::Diagnostic;
use regex::Regex;
use serde::{Serialize, Serializer};
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -145,6 +146,12 @@ impl FromStr for NormalizedPackageName {
}
}

impl Borrow<str> for NormalizedPackageName {
fn borrow(&self) -> &str {
self.0.as_ref()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 2b31e8f

Please sign in to comment.