Skip to main content

gix_object/traits/
mod.rs

1use std::io;
2
3use crate::Kind;
4
5/// Describe the capability to write git objects into an object store.
6pub trait Write {
7    /// Write objects using the intrinsic kind of [`hash`](gix_hash::Kind) into the database,
8    /// returning id to reference it in subsequent reads.
9    fn write(&self, object: &dyn WriteTo) -> Result<gix_hash::ObjectId, crate::write::Error> {
10        let mut buf = Vec::with_capacity(2048);
11        object.write_to(&mut buf)?;
12        self.write_stream(object.kind(), buf.len() as u64, &mut buf.as_slice())
13    }
14    /// As [`write`](Write::write), but takes an [`object` kind](Kind) along with its encoded bytes.
15    fn write_buf(&self, object: crate::Kind, mut from: &[u8]) -> Result<gix_hash::ObjectId, crate::write::Error> {
16        self.write_stream(object, from.len() as u64, &mut from)
17    }
18    /// As [`write_buf`](Write::write_buf), but the object `id` has already been computed by the caller.
19    ///
20    /// Implementations may trust the given `id` and avoid computing it again. Callers must make sure `id` matches
21    /// the provided `object` and `from` bytes.
22    fn write_buf_with_known_id(
23        &self,
24        object: crate::Kind,
25        from: &[u8],
26        id: gix_hash::ObjectId,
27    ) -> Result<gix_hash::ObjectId, crate::write::Error>;
28    /// As [`write`](Write::write), but takes an input stream.
29    /// This is commonly used for writing blobs directly without reading them to memory first.
30    fn write_stream(
31        &self,
32        kind: crate::Kind,
33        size: u64,
34        from: &mut dyn io::Read,
35    ) -> Result<gix_hash::ObjectId, crate::write::Error>;
36    /// As [`write_stream`](Write::write_stream), but the object `id` has already been computed by the caller.
37    ///
38    /// Implementations may trust the given `id` and avoid computing it again. Callers must make sure `id` matches
39    /// the provided `kind`, `size` and stream contents.
40    fn write_stream_with_known_id(
41        &self,
42        kind: crate::Kind,
43        size: u64,
44        from: &mut dyn io::Read,
45        id: gix_hash::ObjectId,
46    ) -> Result<gix_hash::ObjectId, crate::write::Error>;
47}
48
49/// Writing of objects to a `Write` implementation
50pub trait WriteTo {
51    /// Write a representation of this instance to `out`.
52    fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()>;
53
54    /// Returns the type of this object.
55    fn kind(&self) -> Kind;
56
57    /// Returns the size of this object's representation (the amount
58    /// of data which would be written by [`write_to`](Self::write_to)).
59    ///
60    /// [`size`](Self::size)'s value has no bearing on the validity of
61    /// the object, as such it's possible for [`size`](Self::size) to
62    /// return a sensible value but [`write_to`](Self::write_to) to
63    /// fail because the object was not actually valid in some way.
64    fn size(&self) -> u64;
65
66    /// Returns a loose object header based on the object's data
67    fn loose_header(&self) -> smallvec::SmallVec<[u8; 28]> {
68        crate::encode::loose_header(self.kind(), self.size())
69    }
70}
71
72mod _impls;
73
74mod find;
75pub use find::*;