Skip to main content

gix_object/
find.rs

1use gix_error::Result;
2use gix_error::ResultExt;
3
4/// An implementation of object access traits that stores nothing and finds nothing.
5/// Use [`Never::panic_on_access()`] to panic on object access instead.
6#[derive(Debug, Copy, Clone)]
7pub struct Never;
8
9impl Never {
10    /// Return an implementation that panics whenever an object access trait method is called.
11    /// Useful for asserting that an operation does not access the object database.
12    pub const fn panic_on_access() -> PanicAlways {
13        PanicAlways
14    }
15}
16
17/// An implementation of object access traits that panics on every call.
18/// Obtain it with [`Never::panic_on_access()`].
19#[derive(Debug, Copy, Clone)]
20pub struct PanicAlways;
21
22impl super::FindHeader for PanicAlways {
23    fn try_header(&self, _id: &gix_hash::oid) -> Result<Option<crate::Header>> {
24        panic!("object header lookups are forbidden");
25    }
26}
27
28impl super::Find for PanicAlways {
29    fn try_find<'a>(&self, _id: &gix_hash::oid, _buffer: &'a mut Vec<u8>) -> Result<Option<crate::Data<'a>>> {
30        panic!("object lookups are forbidden");
31    }
32}
33
34impl super::Exists for PanicAlways {
35    fn exists(&self, _id: &gix_hash::oid) -> bool {
36        panic!("object existence checks are forbidden");
37    }
38}
39
40impl super::Write for PanicAlways {
41    fn write(&self, _object: &dyn crate::WriteTo) -> Result<gix_hash::ObjectId> {
42        panic!("object writes are forbidden");
43    }
44
45    fn write_buf_with_known_id(
46        &self,
47        _object: crate::Kind,
48        _from: &[u8],
49        _id: gix_hash::ObjectId,
50    ) -> Result<gix_hash::ObjectId> {
51        panic!("object writes are forbidden");
52    }
53
54    fn write_stream(
55        &self,
56        _kind: crate::Kind,
57        _size: u64,
58        _from: &mut dyn std::io::Read,
59    ) -> Result<gix_hash::ObjectId> {
60        panic!("object writes are forbidden");
61    }
62
63    fn write_stream_with_known_id(
64        &self,
65        _kind: crate::Kind,
66        _size: u64,
67        _from: &mut dyn std::io::Read,
68        _id: gix_hash::ObjectId,
69    ) -> Result<gix_hash::ObjectId> {
70        panic!("object writes are forbidden");
71    }
72}
73
74impl super::FindHeader for Never {
75    fn try_header(&self, _id: &gix_hash::oid) -> Result<Option<crate::Header>> {
76        Ok(None)
77    }
78}
79
80impl super::Find for Never {
81    fn try_find<'a>(&self, _id: &gix_hash::oid, _buffer: &'a mut Vec<u8>) -> Result<Option<crate::Data<'a>>> {
82        Ok(None)
83    }
84}
85
86impl super::Exists for Never {
87    fn exists(&self, _id: &gix_hash::oid) -> bool {
88        false
89    }
90}
91
92impl super::Write for Never {
93    fn write_buf(&self, object: crate::Kind, from: &[u8]) -> Result<gix_hash::ObjectId> {
94        crate::compute_hash(gix_hash::Kind::default(), object, from)
95    }
96
97    fn write_buf_with_known_id(
98        &self,
99        _object: crate::Kind,
100        _from: &[u8],
101        id: gix_hash::ObjectId,
102    ) -> Result<gix_hash::ObjectId> {
103        Ok(id)
104    }
105
106    fn write_stream(&self, kind: crate::Kind, size: u64, from: &mut dyn std::io::Read) -> Result<gix_hash::ObjectId> {
107        crate::compute_stream_hash(
108            gix_hash::Kind::default(),
109            kind,
110            from,
111            size,
112            &mut gix_utils::progress::Discard,
113            &std::sync::atomic::AtomicBool::new(false),
114        )
115    }
116
117    fn write_stream_with_known_id(
118        &self,
119        _kind: crate::Kind,
120        mut size: u64,
121        from: &mut dyn std::io::Read,
122        id: gix_hash::ObjectId,
123    ) -> Result<gix_hash::ObjectId> {
124        let mut buf = [0u8; u16::MAX as usize];
125        while size != 0 {
126            let bytes = (size as usize).min(buf.len());
127            from.read_exact(&mut buf[..bytes]).or_error()?;
128            size -= bytes as u64;
129        }
130        Ok(id)
131    }
132}