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`](Write::write), but takes an input stream.
19 /// This is commonly used for writing blobs directly without reading them to memory first.
20 fn write_stream(
21 &self,
22 kind: crate::Kind,
23 size: u64,
24 from: &mut dyn io::Read,
25 ) -> Result<gix_hash::ObjectId, crate::write::Error>;
26}
27
28/// Writing of objects to a `Write` implementation
29pub trait WriteTo {
30 /// Write a representation of this instance to `out`.
31 fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()>;
32
33 /// Returns the type of this object.
34 fn kind(&self) -> Kind;
35
36 /// Returns the size of this object's representation (the amount
37 /// of data which would be written by [`write_to`](Self::write_to)).
38 ///
39 /// [`size`](Self::size)'s value has no bearing on the validity of
40 /// the object, as such it's possible for [`size`](Self::size) to
41 /// return a sensible value but [`write_to`](Self::write_to) to
42 /// fail because the object was not actually valid in some way.
43 fn size(&self) -> u64;
44
45 /// Returns a loose object header based on the object's data
46 fn loose_header(&self) -> smallvec::SmallVec<[u8; 28]> {
47 crate::encode::loose_header(self.kind(), self.size())
48 }
49}
50
51mod _impls;
52
53mod find;
54pub use find::*;