Is there an easy way to annotate fields in a struct so that they are ignored when deriving the PartialEq trait? For example:
#[derive(PartialEq,Eq)]
pub struct UndirectedGraph {
nodes: HashMap<NodeIdx, UndirectedNode>,
// mapping of degree to nodes of that degree
degree_index: Vec<HashSet<NodeIdx>>,
}
I want two undirected graphs to be considered equal when they have the same nodes field, but the degree_index field may differ (the vector may contain extra empty hash-sets at the end).
Obviously I could just implement the trait manually, but automatic derivation would be simpler.
No, there is no way to do that currently and I doubt it will be supported.
You could consider making the fields that you want to compare into a sub-struct which is derived, which would make the implementation for the larger struct trivial.
Check out the derivative create (docs). It provides alternate derive macros with more power than the standard library versions, including ways to ignore fields for Hash and PartialEq traits.
I would also recommend the derivative crate if you need a more sophisticated form of #[derive].
If you only need something like this once or twice, it may be easier to just implement the required traits manually rather than derive them. PartialEq and Eq are very easy to implement yourself:
impl PartialEq for UndirectedGraph {
fn eq(&self, other: &Self) -> bool {
self.nodes == other.nodes
}
}
impl Eq for UndirectedGraph {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With