Closed seftontycho closed 6 months ago
That should definitely be doable. IsEqual
returns a Bit
, and BitOr
is implemented on bits. Let me know if a concrete example would be helpful.
Here's the full example:
use std::ops::BitOr;
use generic_array::ArrayLength;
use typenum::{Eq, IsEqual, Or, Same, True, U1, U2, U3, U7};
pub trait MyTrait<A, B> {
fn foo();
}
impl<T, A: ArrayLength, B: ArrayLength> MyTrait<A, B> for T
where
A: IsEqual<B> + IsEqual<U1>,
B: IsEqual<U1>,
Eq<A, U1>: BitOr<Eq<B, U1>>,
Eq<A, B>: BitOr<Or<Eq<A, U1>, Eq<B, U1>>>,
Or<Eq<A, B>, Or<Eq<A, U1>, Eq<B, U1>>>: Same<True>,
{
fn foo() {}
}
fn main() {
<() as MyTrait<U1, U2>>::foo();
<() as MyTrait<U7, U7>>::foo();
<() as MyTrait<U3, U1>>::foo();
// Fails to compile:
// <() as MyTrait<U3, U2>>::foo();
}
Note the use of Same
; this trait exists for exactly this purpose, to check type equality in things like where clauses.
Perfect that helps a lot!
That appears to have solved all my issues.
I wish to implement a trait when 2 of the generics are either equal or at least one of them is equal to 1. Here A and B have the ArrayLength Trait from
GenericArray
which is a superset ofUnsigned
.Something like: