-
Notifications
You must be signed in to change notification settings - Fork 15
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
Allow other primitives as keys #67
Conversation
347cccc
to
f52d947
Compare
Relates to #63 |
It seems like the tests don't pass. |
f52d947
to
5ba3c96
Compare
Fixed |
This change makes it possible to use other primitives as keys. There are a lot of function-level constraints, which could be either aliased, or lifted to the impl clause, but I prefer to have the minimal set on a per-function basis. This is a breaking change, because of the introduction of the extra type parameter.
5ba3c96
to
c4ad9c5
Compare
@JesperAxelsson looks like there were integration test failures. Should be all ✅ now. |
@JesperAxelsson Any ideas how we deal with the two competing PRs? I think the main difference is that this PR provides a trait that can implemented by the user for any integer type, though it requires |
@jakoschiko Well, thanks for bringing this up, at present, HighestPrime isn't public, so can't be implemented by users of the crate. I will change that though, because I need the feature. I want to have keys that are newtype wrappers around u32, ie |
Another alternative: struct IntMap<K: IntKey, V> {
// ...
}
trait IntKey: Copy {
type Int: Int; // Int is the sealed trait from PR #63
fn from_int(int: Self::Int) -> Self;
fn to_int(self) -> Self::Int;
}
// That allows the user to use u32 as key without any wrapper
impl IntKey for u32 {
type Int = Self;
fn from_int(int: Self::Int) -> Self { int }
fn to_int(self) -> Self::Int { self }
}
// Your use case
#[derive(Copy)]
struct MyKey(u32);
impl IntKey for MyKey {
type Int = u32
fn from_int(int: Self::Int) -> Self { Self(int) }
fn to_int(self) -> Self::Int { self.0 }
} That would require that the |
@jakoschiko I was doing something similar in an internal project until recently. Your version is admittedly way easier to implement for custom types than all of the separate Would you be open to adding this change to your PR? If I can use it with custom types. then I'm happy to 👍 yours. |
I'll create a new PR soon. |
The best way is to discuss pros and cons, as you guys did :) Keeping it backwards compatible is great of course, but having key and value be in the correct order ( |
Closed in favor of #69 |
This change makes it possible to use other primitives as keys.
There are a lot of function-level constraints, which could be either
aliased, or lifted to the impl clause, but I prefer to have the
minimal set on a per-function basis.
This is a breaking change, because of the introduction of the extra type
parameter.