Skip to main content

gix_object/traits/
mod.rs

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