murf/matcher/
deref.rs

1use std::fmt::{Display, Formatter, Result as FmtResult};
2
3use crate::Matcher;
4
5/// Create a new [`Deref`] matcher, that calls the [`deref`](std::ops::Deref::deref())
6/// method of the argument and forwards it to the passed `inner` matcher.
7pub fn deref<M>(inner: M) -> Deref<M> {
8    Deref(inner)
9}
10
11/// Implements a [`Matcher`] that calls the [`deref`](std::ops::Deref::deref())
12/// method of the argument and forwards it to the passed matcher `M`.
13#[must_use]
14#[derive(Debug)]
15pub struct Deref<M>(pub M);
16
17impl<T, M> Matcher<T> for Deref<M>
18where
19    T: std::ops::Deref,
20    T::Target: Sized,
21    M: Matcher<T::Target>,
22{
23    fn matches(&self, value: &T) -> bool {
24        self.0.matches(&**value)
25    }
26}
27
28impl<M> Display for Deref<M>
29where
30    M: Display,
31{
32    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
33        write!(f, "deref(")?;
34        self.0.fmt(f)?;
35        write!(f, ")")?;
36
37        Ok(())
38    }
39}