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
use std::{convert::Infallible, io};

use crate::{Blob, BlobRef, Kind};

impl<'a> crate::WriteTo for BlobRef<'a> {
    /// Write the blobs data to `out` verbatim.
    fn write_to(&self, out: &mut dyn io::Write) -> io::Result<()> {
        out.write_all(self.data)
    }

    fn kind(&self) -> Kind {
        Kind::Blob
    }

    fn size(&self) -> u64 {
        self.data.len() as u64
    }
}

impl crate::WriteTo for Blob {
    /// Write the blobs data to `out` verbatim.
    fn write_to(&self, out: &mut dyn io::Write) -> io::Result<()> {
        self.to_ref().write_to(out)
    }

    fn kind(&self) -> Kind {
        Kind::Blob
    }

    fn size(&self) -> u64 {
        self.to_ref().size()
    }
}

impl Blob {
    /// Provide a `BlobRef` to this owned blob
    pub fn to_ref(&self) -> BlobRef<'_> {
        BlobRef { data: &self.data }
    }
}

impl<'a> BlobRef<'a> {
    /// Instantiate a `Blob` from the given `data`, which is used as-is.
    pub fn from_bytes(data: &[u8]) -> Result<BlobRef<'_>, Infallible> {
        Ok(BlobRef { data })
    }
}