git_object/
traits.rs

1use std::io::Write;
2
3use crate::Kind;
4
5/// Writing of objects to a `Write` implementation
6pub trait WriteTo {
7    /// Write a representation of this instance to `out`.
8    fn write_to(&self, out: impl std::io::Write) -> std::io::Result<()>;
9
10    /// Returns the type of this object.
11    fn kind(&self) -> Kind;
12
13    /// Returns the size of this object's representation (the amount
14    /// of data which would be written by [`write_to`](Self::write_to)).
15    ///
16    /// [`size`](Self::size)'s value has no bearing on the validity of
17    /// the object, as such it's possible for [`size`](Self::size) to
18    /// return a sensible value but [`write_to`](Self::write_to) to
19    /// fail because the object was not actually valid in some way.
20    fn size(&self) -> usize;
21
22    /// Returns a loose object header based on the object's data
23    fn loose_header(&self) -> smallvec::SmallVec<[u8; 28]> {
24        crate::encode::loose_header(self.kind(), self.size())
25    }
26}
27
28impl<T> WriteTo for &T
29where
30    T: WriteTo,
31{
32    fn write_to(&self, out: impl Write) -> std::io::Result<()> {
33        <T as WriteTo>::write_to(self, out)
34    }
35
36    fn size(&self) -> usize {
37        <T as WriteTo>::size(self)
38    }
39
40    fn kind(&self) -> Kind {
41        <T as WriteTo>::kind(self)
42    }
43}