Skip to main content

objects/store/
source.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Read-only object source traits for graph walkers.
3
4use crate::{
5    object::{Blob, ChangeId, ContentHash, State, Tree},
6    store::ObjectStore,
7};
8
9use super::Result;
10
11/// Read-only subset of [`ObjectStore`] needed by object graph walkers.
12pub trait ObjectSource {
13    fn get_tree(&self, hash: &ContentHash) -> Result<Option<Tree>>;
14    fn get_state(&self, id: &ChangeId) -> Result<Option<State>>;
15    fn get_blob(&self, hash: &ContentHash) -> Result<Option<Blob>>;
16
17    /// Zero-copy variant of `get_blob`.
18    fn get_blob_bytes(&self, hash: &ContentHash) -> Result<Option<bytes::Bytes>> {
19        Ok(self
20            .get_blob(hash)?
21            .map(|blob| bytes::Bytes::from(blob.into_content())))
22    }
23}
24
25impl<S: ObjectStore + ?Sized> ObjectSource for S {
26    #[inline]
27    fn get_tree(&self, hash: &ContentHash) -> Result<Option<Tree>> {
28        ObjectStore::get_tree(self, hash)
29    }
30
31    #[inline]
32    fn get_state(&self, id: &ChangeId) -> Result<Option<State>> {
33        ObjectStore::get_state(self, id)
34    }
35
36    #[inline]
37    fn get_blob(&self, hash: &ContentHash) -> Result<Option<Blob>> {
38        ObjectStore::get_blob(self, hash)
39    }
40
41    #[inline]
42    fn get_blob_bytes(&self, hash: &ContentHash) -> Result<Option<bytes::Bytes>> {
43        ObjectStore::get_blob_bytes(self, hash)
44    }
45}
46
47#[cfg(feature = "async-source")]
48#[allow(async_fn_in_trait)]
49pub trait AsyncObjectSource {
50    async fn get_tree(&self, hash: &ContentHash) -> Result<Option<Tree>>;
51    async fn get_state(&self, id: &ChangeId) -> Result<Option<State>>;
52    async fn get_blob(&self, hash: &ContentHash) -> Result<Option<Blob>>;
53}