Skip to content

Commit

Permalink
impl: fix 'cargo t --features serde'
Browse files Browse the repository at this point in the history
We need to make our wrapper implement the serde traits.
  • Loading branch information
BurntSushi committed Jan 22, 2024
1 parent bb53f67 commit 2301991
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions src/type_aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ pub type SelectedDependencies<P, V> = Map<P, V>;
/// while the latter means they could not be fetched by the [DependencyProvider](crate::solver::DependencyProvider).
pub type DependencyConstraints<P, VS> = Map<P, VS>;

pub type Map<K, V> = MapI<K, V, BuildHasherDefault<rustc_hash::FxHasher>>;

#[derive(Debug, Clone)]
pub struct Map<K, V, S = BuildHasherDefault<rustc_hash::FxHasher>> {
pub struct MapI<K, V, S> {
map: std::collections::HashMap<K, V, S>,
}

impl<K, V, S> Map<K, V, S> {
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Map<K, V, S> {
Map {
impl<K, V, S> MapI<K, V, S> {
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> MapI<K, V, S> {
MapI {
map: std::collections::HashMap::with_capacity_and_hasher(capacity, hasher),
}
}
Expand Down Expand Up @@ -57,7 +59,7 @@ impl<K, V, S> Map<K, V, S> {
}
}

impl<K: Hash + Eq, V, S: BuildHasher> Map<K, V, S> {
impl<K: Hash + Eq, V, S: BuildHasher> MapI<K, V, S> {
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
self.map.insert(k, v)
}
Expand Down Expand Up @@ -87,45 +89,67 @@ impl<K: Hash + Eq, V, S: BuildHasher> Map<K, V, S> {
}
}

impl<K, V, S: Default> Default for Map<K, V, S> {
fn default() -> Map<K, V, S> {
Map {
#[cfg(feature = "serde")]
impl<K: serde::Serialize, V: serde::Serialize, S: Default> serde::Serialize for MapI<K, V, S> {
fn serialize<SE: serde::Serializer>(&self, s: SE) -> Result<SE::Ok, SE::Error> {
self.map.serialize(s)
}
}

#[cfg(feature = "serde")]
impl<
'de,
K: Hash + Eq + serde::Deserialize<'de>,
V: serde::Deserialize<'de>,
S: Default + BuildHasher,
> serde::Deserialize<'de> for MapI<K, V, S>
{
fn deserialize<D: serde::Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
Ok(MapI {
map: std::collections::HashMap::deserialize(de)?,
})
}
}

impl<K, V, S: Default> Default for MapI<K, V, S> {
fn default() -> MapI<K, V, S> {
MapI {
map: std::collections::HashMap::default(),
}
}
}

impl<K: Eq + Hash, V: PartialEq, S: BuildHasher> PartialEq for Map<K, V, S> {
fn eq(&self, other: &Map<K, V, S>) -> bool {
impl<K: Eq + Hash, V: PartialEq, S: BuildHasher> PartialEq for MapI<K, V, S> {
fn eq(&self, other: &MapI<K, V, S>) -> bool {
self.map.eq(&other.map)
}
}

impl<K: Hash + Eq, V, S: BuildHasher> Extend<(K, V)> for Map<K, V, S> {
impl<K: Hash + Eq, V, S: BuildHasher> Extend<(K, V)> for MapI<K, V, S> {
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, it: T) {
self.map.extend(it)
}
}

impl<'a, K: 'a + Hash + Eq + Clone, V: 'a + Clone, S: BuildHasher> Extend<(&'a K, &'a V)>
for Map<K, V, S>
for MapI<K, V, S>
{
fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, it: T) {
self.map
.extend(it.into_iter().map(|(k, v)| (k.clone(), v.clone())))
}
}

impl<K: Hash + Eq, V, S: BuildHasher + Default> FromIterator<(K, V)> for Map<K, V, S> {
fn from_iter<I: IntoIterator<Item = (K, V)>>(it: I) -> Map<K, V, S> {
Map {
impl<K: Hash + Eq, V, S: BuildHasher + Default> FromIterator<(K, V)> for MapI<K, V, S> {
fn from_iter<I: IntoIterator<Item = (K, V)>>(it: I) -> MapI<K, V, S> {
MapI {
map: FromIterator::from_iter(it),
}
}
}

impl<K: Eq + Hash + Borrow<Q>, Q: Eq + Hash + ?Sized, V, S: BuildHasher> std::ops::Index<&Q>
for Map<K, V, S>
for MapI<K, V, S>
{
type Output = V;

Expand All @@ -134,7 +158,7 @@ impl<K: Eq + Hash + Borrow<Q>, Q: Eq + Hash + ?Sized, V, S: BuildHasher> std::op
}
}

impl<K: Clone + Eq + Hash, V, S: BuildHasher> IntoIterator for Map<K, V, S> {
impl<K: Clone + Eq + Hash, V, S: BuildHasher> IntoIterator for MapI<K, V, S> {
type Item = (K, V);
type IntoIter = MapIntoIter<K, V, S>;

Expand All @@ -147,7 +171,7 @@ impl<K: Clone + Eq + Hash, V, S: BuildHasher> IntoIterator for Map<K, V, S> {
}
}

impl<'a, K: Eq + Hash, V, S: BuildHasher> IntoIterator for &'a Map<K, V, S> {
impl<'a, K: Eq + Hash, V, S: BuildHasher> IntoIterator for &'a MapI<K, V, S> {
type Item = (&'a K, &'a V);
type IntoIter = MapIter<'a, K, V, S>;

Expand All @@ -157,7 +181,7 @@ impl<'a, K: Eq + Hash, V, S: BuildHasher> IntoIterator for &'a Map<K, V, S> {
}

pub struct MapIter<'a, K, V, S = BuildHasherDefault<rustc_hash::FxHasher>> {
map: &'a Map<K, V, S>,
map: &'a MapI<K, V, S>,
order: std::vec::IntoIter<&'a K>,
}

Expand All @@ -171,7 +195,7 @@ impl<'a, K: Eq + Hash, V, S: BuildHasher> Iterator for MapIter<'a, K, V, S> {
}

pub struct MapIntoIter<K, V, S = BuildHasherDefault<rustc_hash::FxHasher>> {
map: Map<K, V, S>,
map: MapI<K, V, S>,
order: std::vec::IntoIter<K>,
}

Expand Down

0 comments on commit 2301991

Please sign in to comment.