Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom key type #69

Merged
merged 5 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve doc
  • Loading branch information
jakoschiko committed Nov 7, 2024
commit 188ced21aa45ba82721dd54ef0d08a78db45fe20
14 changes: 1 addition & 13 deletions src/int.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt::Debug;

/// A primitive integer that can be used as underlying key for [`IntMap`].
///
/// Note that this is a sealed trait that cannot be implemented externally. Consider implementing
Expand All @@ -22,16 +20,10 @@ impl Int for i64 {}
impl Int for i128 {}
impl Int for isize {}

pub trait SealedInt: Copy + PartialEq + Debug + SerdeInt {
pub trait SealedInt: Copy + PartialEq {
fn calc_index(self, mod_mask: usize) -> usize;
}

#[cfg(not(feature = "serde"))]
pub trait SerdeInt {}

#[cfg(feature = "serde")]
pub trait SerdeInt: serde::Serialize + for<'de> serde::Deserialize<'de> {}

macro_rules! impl_sealed_int_for_int_with_highest_prime {
($uint:ident, $prime:expr) => {
impl SealedInt for $uint {
Expand All @@ -42,8 +34,6 @@ macro_rules! impl_sealed_int_for_int_with_highest_prime {
(hash as usize) & mod_mask
}
}

impl SerdeInt for $uint {}
};
}

Expand All @@ -55,8 +45,6 @@ macro_rules! impl_sealed_int_for_int_with_cast {
(self as $uint).calc_index(mod_mask)
}
}

impl SerdeInt for $int {}
};
}

Expand Down
19 changes: 19 additions & 0 deletions src/int_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ use crate::Int;
/// This can be useful for types that wraps integers for type safety (e.g. [`Ipv4Addr`])
/// or for enforcing invariants (e.g. [`NonZeroU64`]).
///
/// # Example
///
/// ```
/// use intmap::{IntKey, IntMap};
///
/// #[derive(Clone, Copy)]
/// struct MyKey(u64);
///
/// impl IntKey for MyKey {
/// type Int = u64;
///
/// fn into_int(self) -> Self::Int {
/// self.0
/// }
/// }
///
/// let map: IntMap<MyKey, f32> = IntMap::new();
/// ```
///
/// [`IntMap`]: crate::IntMap
/// [`Ipv4Addr`]: std::net::Ipv4Addr
/// [`NonZeroU64`]: std::num::NonZeroU64
Expand Down