must/matchers/
partial_eq.rs1use errors::{ErrorKind, dump};
2use lazy::SimpleAssert;
3use std::fmt::Debug;
4
5
6pub trait PartialEqMust<T: Debug>: Sized + Debug + PartialEq<T> {
10 fn must_be(self, to: T) -> SimpleAssert<Self> {
23 if self == to {
24 self.into()
25 } else {
26 ErrorKind::MustEq { to: dump(&to) }.but_got(self)
27 }
28 }
29
30 fn must_eq(self, to: T) -> SimpleAssert<Self> {
31 self.must_be(to)
32 }
33
34 fn must_not_be(self, to: T) -> SimpleAssert<Self> {
35 if self != to {
36 self.into()
37 } else {
38 ErrorKind::MustNotEq { to: dump(&to) }.but_got(self)
39 }
40 }
41
42 fn must_not_eq(self, to: T) -> SimpleAssert<Self> {
43 self.must_not_be(to)
44 }
45}
46
47impl<T: Debug, V: Sized> PartialEqMust<T> for V where V: Debug + PartialEq<T> {}
48pub trait MustBeBool: PartialEqMust<bool> {
52 fn must_be_true(self) -> SimpleAssert<Self> {
64 self.must_eq(true)
65 }
66
67 fn must_be_false(self) -> SimpleAssert<Self> {
68 self.must_be(false)
69 }
70}
71
72impl<V: ?Sized> MustBeBool for V where V: PartialEqMust<bool> {}
73
74pub trait MustBeDefault: Debug + PartialEq + Default {
75 fn must_be_default(self) -> SimpleAssert<Self> {
76 self.must_be(Self::default())
77 }
78
79 fn must_not_be_default(self) -> SimpleAssert<Self> {
80 self.must_not_be(Self::default())
81 }
82}
83
84impl<V: ?Sized> MustBeDefault for V where V: Debug + PartialEq + Default {}
85
86
87fn _assert() {
88 fn partial_eq<T: PartialEqMust<To>, To: Debug>() {}
89 fn be_default<T: MustBeDefault>() {}
90 fn be_bool<T: MustBeBool>() {}
91
92 partial_eq::<&str, &str>();
93 partial_eq::<String, &str>();
94 partial_eq::<Vec<u8>, Vec<u8>>();
95 partial_eq::<[u8; 32], [u8; 32]>();
96 partial_eq::<&[u8], &[u8]>();
97
98 be_default::<Option<i64>>();
99 be_default::<i64>();
100
101 be_bool::<bool>();
102}