1use std::{rc::Rc, sync::Arc};
4
5pub trait Digest {
10 const NAME: &'static str;
12
13 fn digest(&mut self, input: &[u8]) -> String;
15
16 fn update(&mut self, input: &[u8]);
18
19 fn verify(&mut self, encoded: &str) -> bool;
21}
22
23pub trait DigestBuilder: Digest {
25 fn build() -> Self;
27}
28
29pub trait DigestFactory {
34 type Digest: DigestBuilder + Digest + Send + 'static;
36}
37
38impl<'a, T> DigestFactory for &'a T
39where
40 T: DigestFactory,
41{
42 type Digest = T::Digest;
43}
44
45impl<'a, T> DigestFactory for &'a mut T
46where
47 T: DigestFactory,
48{
49 type Digest = T::Digest;
50}
51
52impl<T> DigestFactory for Box<T>
53where
54 T: DigestFactory,
55{
56 type Digest = T::Digest;
57}
58
59impl<T> DigestFactory for Rc<T>
60where
61 T: DigestFactory,
62{
63 type Digest = T::Digest;
64}
65
66impl<T> DigestFactory for Arc<T>
67where
68 T: DigestFactory,
69{
70 type Digest = T::Digest;
71}
72
73impl<'a, T> Digest for &'a mut T
74where
75 T: Digest,
76{
77 const NAME: &'static str = T::NAME;
78
79 fn digest(&mut self, input: &[u8]) -> String {
80 T::digest(self, input)
81 }
82
83 fn update(&mut self, input: &[u8]) {
84 T::update(self, input)
85 }
86
87 fn verify(&mut self, encoded: &str) -> bool {
88 T::verify(self, encoded)
89 }
90}
91
92impl<T> Digest for Box<T>
93where
94 T: Digest,
95{
96 const NAME: &'static str = T::NAME;
97
98 fn digest(&mut self, input: &[u8]) -> String {
99 T::digest(self, input)
100 }
101
102 fn update(&mut self, input: &[u8]) {
103 T::update(self, input)
104 }
105
106 fn verify(&mut self, encoded: &str) -> bool {
107 T::verify(self, encoded)
108 }
109}