1use std::ops::{Deref, DerefMut};
14
15pub trait BorrowDeref<T> {
17 fn borrow(&self) -> impl Deref<Target = T> + '_;
19}
20
21pub trait BorrowDerefMut<T> {
23 fn borrow_mut(&self) -> impl DerefMut<Target = T> + '_;
25}
26
27pub trait With<T> {
29 fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R;
30}
31
32impl<T, BD: BorrowDeref<T>> With<T> for BD {
33 fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
34 f(self.borrow().deref())
35 }
36}
37
38pub trait WithInnerMut<T> {
40 fn with_inner_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> R;
42}
43impl<T, BDM: BorrowDerefMut<T>> WithInnerMut<T> for BDM {
44 fn with_inner_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
45 f(self.borrow_mut().deref_mut())
46 }
47}
48
49pub trait WithMut<T> {
51 fn with_mut<R>(&mut self, f: impl FnOnce(&mut T) -> R) -> R;
53}
54
55impl<T, WIM: WithInnerMut<T>> WithMut<T> for WIM {
57 fn with_mut<R>(&mut self, f: impl FnOnce(&mut T) -> R) -> R {
58 WithInnerMut::with_inner_mut(self, f)
59 }
60}
61
62pub trait InnerEq<T> {
64 fn inner_eq(&self, other: &Self) -> bool;
66}
67impl<T: PartialEq, BD: BorrowDeref<T>> InnerEq<T> for BD {
68 fn inner_eq(&self, other: &Self) -> bool {
69 self.borrow().deref() == other.borrow().deref()
70 }
71}