1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use Rc;
use crateDigest;
/// A type that can provide a single unique digest that characterizes its
/// contents.
///
/// This trait is used to define a common interface for objects that can produce
/// a cryptographic digest (hash) of their content. Implementations of this
/// trait use a `Digest` to avoid unnecessary cloning:
/// - If the digest is already owned by the implementor, it can be returned by
/// borrowing
/// - If it doesn't exist yet, it can be created and returned by owning
///
/// # Use Cases
///
/// The `DigestProvider` trait is useful in scenarios where:
///
/// - You need to verify data integrity
/// - You need a unique identifier for an object based on its content
/// - You want to implement content-addressable storage
/// - You need to compare objects by their content rather than identity
///
/// # Examples
///
/// Implementing `DigestProvider` for a custom type:
///
/// ```
/// use std::borrow::Cow;
///
/// use bc_components::{Digest, DigestProvider};
///
/// struct Document {
/// content: Vec<u8>,
/// cached_digest: Option<Digest>,
/// }
///
/// impl Document {
/// fn new(content: Vec<u8>) -> Self {
/// Self { content, cached_digest: None }
/// }
/// }
///
/// impl DigestProvider for Document {
/// fn digest(&self) -> Digest {
/// match &self.cached_digest {
/// Some(digest) => *digest,
/// None => Digest::from_image(&self.content),
/// }
/// }
/// }
///
/// // Create a document and get its digest
/// let doc = Document::new(b"important data".to_vec());
/// let digest = doc.digest();
/// ```
///
/// Using the provided implementation for `&[u8]`:
///
/// ```
/// use std::borrow::Cow;
///
/// use bc_components::{Digest, DigestProvider};
///
/// // The DigestProvider is implemented for &[u8], not for &[u8; N]
/// let data: &[u8] = b"hello world";
/// let digest_cow: Digest = data.digest();
/// ```
/// Implements DigestProvider for byte slices, creating a digest from the
/// slice's contents.
/// Implements DigestProvider for Rc-wrapped types that implement
/// DigestProvider, delegating to the inner type's implementation.