Skip to content

Commit 335100d

Browse files
authored
add auto implementation of Pod::equals (#327)
1 parent e0d2fce commit 335100d

File tree

7 files changed

+21
-75
lines changed

7 files changed

+21
-75
lines changed

src/backends/plonky2/emptypod.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::{
2-
any::Any,
32
collections::HashMap,
43
sync::{LazyLock, Mutex},
54
};
@@ -193,17 +192,6 @@ impl Pod for EmptyPod {
193192
})
194193
.expect("serialization to json")
195194
}
196-
197-
fn as_any(&self) -> &dyn Any {
198-
self
199-
}
200-
fn equals(&self, other: &dyn Pod) -> bool {
201-
if let Some(other) = other.as_any().downcast_ref::<EmptyPod>() {
202-
self == other
203-
} else {
204-
false
205-
}
206-
}
207195
}
208196

209197
impl RecursivePod for EmptyPod {

src/backends/plonky2/mainpod/mod.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -676,17 +676,6 @@ impl Pod for MainPod {
676676
})
677677
.expect("serialization to json")
678678
}
679-
680-
fn as_any(&self) -> &dyn Any {
681-
self
682-
}
683-
fn equals(&self, other: &dyn Pod) -> bool {
684-
if let Some(other) = other.as_any().downcast_ref::<MainPod>() {
685-
self == other
686-
} else {
687-
false
688-
}
689-
}
690679
}
691680

692681
impl RecursivePod for MainPod {

src/backends/plonky2/mock/emptypod.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::any::Any;
2-
31
use itertools::Itertools;
42

53
use crate::{
@@ -69,17 +67,6 @@ impl Pod for MockEmptyPod {
6967
fn serialize_data(&self) -> serde_json::Value {
7068
serde_json::Value::Null
7169
}
72-
73-
fn as_any(&self) -> &dyn Any {
74-
self
75-
}
76-
fn equals(&self, other: &dyn Pod) -> bool {
77-
if let Some(other) = other.as_any().downcast_ref::<MockEmptyPod>() {
78-
self == other
79-
} else {
80-
false
81-
}
82-
}
8370
}
8471

8572
impl RecursivePod for MockEmptyPod {

src/backends/plonky2/mock/mainpod.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// MainPod
33
//
44

5-
use std::{any::Any, fmt, iter};
5+
use std::{fmt, iter};
66

77
use itertools::Itertools;
88
use serde::{Deserialize, Serialize};
@@ -367,17 +367,6 @@ impl Pod for MockMainPod {
367367
})
368368
.expect("serialization to json")
369369
}
370-
371-
fn as_any(&self) -> &dyn Any {
372-
self
373-
}
374-
fn equals(&self, other: &dyn Pod) -> bool {
375-
if let Some(other) = other.as_any().downcast_ref::<MockMainPod>() {
376-
self == other
377-
} else {
378-
false
379-
}
380-
}
381370
}
382371

383372
impl RecursivePod for MockMainPod {

src/backends/plonky2/mock/signedpod.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{any::Any, collections::HashMap};
1+
use std::collections::HashMap;
22

33
use itertools::Itertools;
44
use serde::{Deserialize, Serialize};
@@ -166,17 +166,6 @@ impl Pod for MockSignedPod {
166166
})
167167
.expect("serialization to json")
168168
}
169-
170-
fn as_any(&self) -> &dyn Any {
171-
self
172-
}
173-
fn equals(&self, other: &dyn Pod) -> bool {
174-
if let Some(other) = other.as_any().downcast_ref::<MockSignedPod>() {
175-
self == other
176-
} else {
177-
false
178-
}
179-
}
180169
}
181170

182171
#[cfg(test)]

src/backends/plonky2/signedpod.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{any::Any, collections::HashMap, sync::LazyLock};
1+
use std::{collections::HashMap, sync::LazyLock};
22

33
use itertools::Itertools;
44
use num_bigint::{BigUint, RandBigInt};
@@ -203,17 +203,6 @@ impl Pod for SignedPod {
203203
})
204204
.expect("serialization to json")
205205
}
206-
207-
fn as_any(&self) -> &dyn Any {
208-
self
209-
}
210-
fn equals(&self, other: &dyn Pod) -> bool {
211-
if let Some(other) = other.as_any().downcast_ref::<SignedPod>() {
212-
self == other
213-
} else {
214-
false
215-
}
216-
}
217206
}
218207

219208
#[cfg(test)]

src/middleware/mod.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,21 @@ pub fn normalize_statement(statement: &Statement, self_id: PodId) -> Statement {
776776
Statement::from_args(predicate, args).expect("statement was valid before normalization")
777777
}
778778

779-
pub trait Pod: fmt::Debug + DynClone + Sync + Send + Any {
779+
pub trait EqualsAny {
780+
fn equals_any(&self, other: &dyn Any) -> bool;
781+
}
782+
783+
impl<T: Any + Eq> EqualsAny for T {
784+
fn equals_any(&self, other: &dyn Any) -> bool {
785+
if let Some(o) = other.downcast_ref::<T>() {
786+
self == o
787+
} else {
788+
false
789+
}
790+
}
791+
}
792+
793+
pub trait Pod: fmt::Debug + DynClone + Sync + Send + Any + EqualsAny {
780794
fn params(&self) -> &Params;
781795
fn verify(&self) -> Result<(), BackendError>;
782796
fn id(&self) -> PodId;
@@ -808,8 +822,9 @@ pub trait Pod: fmt::Debug + DynClone + Sync + Send + Any {
808822
.collect()
809823
}
810824

811-
fn as_any(&self) -> &dyn Any;
812-
fn equals(&self, other: &dyn Pod) -> bool;
825+
fn equals(&self, other: &dyn Pod) -> bool {
826+
self.equals_any(other as &dyn Any)
827+
}
813828
}
814829
impl PartialEq for Box<dyn Pod> {
815830
fn eq(&self, other: &Self) -> bool {

0 commit comments

Comments
 (0)