1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use std::{rc::Rc, borrow::Cow};

use crate::digest::Digest;

/// A type that can provide a single unique digest that characterizes its contents.
///
/// Returns a Cow<'_, 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.
pub trait DigestProvider {
    fn digest(&self) -> Cow<'_, Digest>;
}

impl DigestProvider for &[u8] {
    fn digest(&self) -> Cow<'_, Digest> {
        Cow::Owned(Digest::from_image(&self))
    }
}

impl<T> DigestProvider for Rc<T> where T: DigestProvider {
    fn digest(&self) -> Cow<'_, Digest> {
        self.as_ref().digest()
    }
}