use super::cause::*;
use std::{cmp::*, error::*};
pub struct CauseComparison(pub Box<dyn Fn(&Cause, &Cause) -> Option<Ordering> + Send + Sync>);
impl CauseComparison {
pub fn new<ErrorT>() -> Self
where
ErrorT: 'static + Error + PartialOrd,
{
Self(Box::new(Self::compare_::<ErrorT>))
}
pub fn compare(&self, left: &Cause, right: &Cause) -> Option<Ordering> {
self.0(left, right)
}
fn compare_<ErrorT>(left: &Cause, right: &Cause) -> Option<Ordering>
where
ErrorT: 'static + Error + PartialOrd,
{
if let Some(left) = left.error.downcast_ref::<ErrorT>()
&& let Some(right) = right.error.downcast_ref::<ErrorT>()
{
left.partial_cmp(right)
} else {
None
}
}
}