-
Notifications
You must be signed in to change notification settings - Fork 46
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
Add
trait is not enough
#63
Comments
Could you give me an example of where the current method doesn't work, or of what a better For example, a function to concatenate two generic arrays might look something like this: fn concat<T, M, N>(a: GenericArray<T, M>, b: GenericArray<T, N>) -> GenericArray<T, Sum<M, N>>
where M: ArrayLength<T> + Add<N>, N: ArrayLength<T>, Sum<M, N>: ArrayLength<T>
{
// ...
} |
I was trying to convert this to generic array: struct Entry<K, V> {
keys: ArrayVec<[K; MAX_ORDER - 1]>,
values: ArrayVec<[V; MAX_ORDER - 1]>,
children: ArrayVec<[Box<Entry<K, V>>; MAX_ORDER]>,
parent: *mut Entry<K, V>,
position: usize,
} Instead of the constant max order, you'd use one type parameter for "B" or so, and have one array be 2B + 1 and another 2B + 2. The entry is part of a public api (a map), that would have the same pub struct Bmap<K, V> {
length: usize,
root: Box<Entry<K, V>>,
} |
I think, unfortunately, lots of where bounds are necessary for working with typenum. I wish there were a way to say something like "where the minimum necessary constraints are satisfied". The compiler clearly has this information, as it can tell you exactly what where bounds you need to add. Anyway, I would be happy to implement an alternative |
It's a very cool library, but it's not suited for what I was trying to do: A B-tree with B as a type parameter. Even writing in primitives like Typenum nowadays enables arrayvec to use any size however, so that's neat; It's by simply both supporting fixed size arrays (a finite number of trait impls) and |
With specialization it's possible to define an "Add" as
But now we've run into the problem that we can't prove that the Of course this wont work for subtraction. Anyway, I figured this comment should exist in case someone finds it interesting. I've been using a similar trick using specialisation to patch generic-array so that |
Neat! I figured this would become possible with specialization, but didn't know we were there yet. Will definitely play with it.
Yeah, due to a compiler bug that has since been fixed. So we can put the trait bounds back. While that's technically a breaking change, the documentation is pretty clear that nothing else should ever go there, so I don't see a problem with it. |
Add
does not propagate useful information for generic code:We need to know that if we add two
Unsigned
, theOutput
is also Unsigned, so that we can continue performing interesting arithmetic on it, or use it as a typenum input to for example generic-array.The text was updated successfully, but these errors were encountered: