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