Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type BigSize = usize;
- type Limb = u64;
- trait Pod {
- fn limbs(&self) -> BigSize;
- fn get_limb(&self, i: BigSize) -> Limb;
- }
- trait PodOps: Pod {
- fn pod_eq(&self, other: &impl PodOps) -> bool {
- if self.limbs() > other.limbs() {
- for i in (0..self.limbs()).rev() {
- if i < other.limbs() {
- if self.get_limb(i) != other.get_limb(i) {
- return false;
- }
- } else {
- if self.get_limb(i) != 0 {
- return false;
- }
- }
- }
- } else {
- for i in (0..other.limbs()).rev() {
- if i < self.limbs() {
- if self.get_limb(i) != other.get_limb(i) {
- return false;
- }
- } else {
- if 0 != other.get_limb(i) {
- return false;
- }
- }
- }
- }
- true
- }
- }
- impl<P: Pod> PodOps for P {}
- struct Wrapper<'a>(&'a [Limb]);
- impl Pod for Wrapper<'_> {
- fn limbs(&self) -> BigSize { self.0.len() }
- fn get_limb(&self, i: BigSize) -> Limb { self.0[i] }
- }
- struct WrapperMut<'a>(&'a mut [Limb]);
- impl Pod for WrapperMut<'_> {
- fn limbs(&self) -> BigSize { self.0.len() }
- fn get_limb(&self, i: BigSize) -> Limb { self.0[i] }
- }
- // New stuff here
- macro_rules! pod_eq {
- (lifetime $l: lifetime; $($P:ty;)+) => {$(
- impl<$l, P: Pod> PartialEq<P> for $P {
- fn eq(&self, other: &P) -> bool {
- self.pod_eq(other)
- }
- }
- impl<$l> Eq for $P {}
- )+}
- }
- pod_eq! {
- lifetime 'a;
- Wrapper<'a>;
- WrapperMut<'a>;
- }
- fn main() {
- println!("{}", Wrapper(&[0]) == WrapperMut(&mut [0]));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement