Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. type BigSize = usize;
  2. type Limb = u64;
  3.  
  4. trait Pod {
  5. fn limbs(&self) -> BigSize;
  6. fn get_limb(&self, i: BigSize) -> Limb;
  7. }
  8.  
  9. trait PodOps: Pod {
  10. fn pod_eq(&self, other: &impl PodOps) -> bool {
  11. if self.limbs() > other.limbs() {
  12. for i in (0..self.limbs()).rev() {
  13. if i < other.limbs() {
  14. if self.get_limb(i) != other.get_limb(i) {
  15. return false;
  16. }
  17. } else {
  18. if self.get_limb(i) != 0 {
  19. return false;
  20. }
  21. }
  22. }
  23. } else {
  24. for i in (0..other.limbs()).rev() {
  25. if i < self.limbs() {
  26. if self.get_limb(i) != other.get_limb(i) {
  27. return false;
  28. }
  29. } else {
  30. if 0 != other.get_limb(i) {
  31. return false;
  32. }
  33. }
  34. }
  35. }
  36. true
  37. }
  38. }
  39.  
  40. impl<P: Pod> PodOps for P {}
  41.  
  42. struct Wrapper<'a>(&'a [Limb]);
  43. impl Pod for Wrapper<'_> {
  44. fn limbs(&self) -> BigSize { self.0.len() }
  45. fn get_limb(&self, i: BigSize) -> Limb { self.0[i] }
  46. }
  47.  
  48. struct WrapperMut<'a>(&'a mut [Limb]);
  49. impl Pod for WrapperMut<'_> {
  50. fn limbs(&self) -> BigSize { self.0.len() }
  51. fn get_limb(&self, i: BigSize) -> Limb { self.0[i] }
  52. }
  53.  
  54. // New stuff here
  55.  
  56. macro_rules! pod_eq {
  57. (lifetime $l: lifetime; $($P:ty;)+) => {$(
  58. impl<$l, P: Pod> PartialEq<P> for $P {
  59. fn eq(&self, other: &P) -> bool {
  60. self.pod_eq(other)
  61. }
  62. }
  63. impl<$l> Eq for $P {}
  64. )+}
  65. }
  66.  
  67. pod_eq! {
  68. lifetime 'a;
  69. Wrapper<'a>;
  70. WrapperMut<'a>;
  71. }
  72.  
  73. fn main() {
  74. println!("{}", Wrapper(&[0]) == WrapperMut(&mut [0]));
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement