Skip to content

Commit 8ba2cfc

Browse files
committed
fix!: use dyn trait where possible.
This reduces compile time due to avoiding duplication.
1 parent e72aa50 commit 8ba2cfc

File tree

18 files changed

+152
-182
lines changed

18 files changed

+152
-182
lines changed

gix-odb/src/cache.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ mod impls {
145145
where
146146
S: crate::Write,
147147
{
148-
type Error = S::Error;
149-
150-
fn write_stream(&self, kind: Kind, size: u64, from: impl Read) -> Result<ObjectId, Self::Error> {
148+
fn write_stream(&self, kind: Kind, size: u64, from: impl Read) -> Result<ObjectId, crate::write::Error> {
151149
self.inner.write_stream(kind, size, from)
152150
}
153151
}
@@ -156,24 +154,24 @@ mod impls {
156154
where
157155
S: gix_pack::Find,
158156
{
159-
type Error = S::Error;
160-
161157
fn contains(&self, id: impl AsRef<oid>) -> bool {
162-
self.inner.contains(id)
158+
self.inner.contains(id.as_ref())
163159
}
164160

165-
fn try_find<'a>(&self, id: impl AsRef<oid>, buffer: &'a mut Vec<u8>) -> Result<Option<Data<'a>>, Self::Error> {
166-
gix_pack::Find::try_find(self, id, buffer).map(|t| t.map(|t| t.0))
161+
fn try_find<'a>(
162+
&self,
163+
id: impl AsRef<oid>,
164+
buffer: &'a mut Vec<u8>,
165+
) -> Result<Option<Data<'a>>, crate::find::Error> {
166+
gix_pack::Find::try_find(self, id.as_ref(), buffer).map(|t| t.map(|t| t.0))
167167
}
168168
}
169169

170170
impl<S> crate::Header for Cache<S>
171171
where
172172
S: crate::Header,
173173
{
174-
type Error = S::Error;
175-
176-
fn try_header(&self, id: impl AsRef<oid>) -> Result<Option<Header>, Self::Error> {
174+
fn try_header(&self, id: impl AsRef<oid>) -> Result<Option<Header>, crate::find::Error> {
177175
self.inner.try_header(id)
178176
}
179177
}
@@ -182,17 +180,15 @@ mod impls {
182180
where
183181
S: gix_pack::Find,
184182
{
185-
type Error = S::Error;
186-
187-
fn contains(&self, id: impl AsRef<oid>) -> bool {
183+
fn contains(&self, id: &oid) -> bool {
188184
self.inner.contains(id)
189185
}
190186

191187
fn try_find<'a>(
192188
&self,
193-
id: impl AsRef<oid>,
189+
id: &oid,
194190
buffer: &'a mut Vec<u8>,
195-
) -> Result<Option<(Data<'a>, Option<Location>)>, Self::Error> {
191+
) -> Result<Option<(Data<'a>, Option<Location>)>, crate::find::Error> {
196192
match self.pack_cache.as_ref().map(RefCell::borrow_mut) {
197193
Some(mut pack_cache) => self.try_find_cached(id, buffer, pack_cache.deref_mut()),
198194
None => self.try_find_cached(id, buffer, &mut gix_pack::cache::Never),
@@ -201,10 +197,10 @@ mod impls {
201197

202198
fn try_find_cached<'a>(
203199
&self,
204-
id: impl AsRef<oid>,
200+
id: &oid,
205201
buffer: &'a mut Vec<u8>,
206-
pack_cache: &mut impl gix_pack::cache::DecodeEntry,
207-
) -> Result<Option<(Data<'a>, Option<gix_pack::data::entry::Location>)>, Self::Error> {
202+
pack_cache: &mut dyn gix_pack::cache::DecodeEntry,
203+
) -> Result<Option<(Data<'a>, Option<gix_pack::data::entry::Location>)>, crate::find::Error> {
208204
if let Some(mut obj_cache) = self.object_cache.as_ref().map(RefCell::borrow_mut) {
209205
if let Some(kind) = obj_cache.get(&id.as_ref().to_owned(), buffer) {
210206
return Ok(Some((Data::new(kind, buffer), None)));
@@ -219,7 +215,7 @@ mod impls {
219215
Ok(possibly_obj)
220216
}
221217

222-
fn location_by_oid(&self, id: impl AsRef<oid>, buf: &mut Vec<u8>) -> Option<gix_pack::data::entry::Location> {
218+
fn location_by_oid(&self, id: &oid, buf: &mut Vec<u8>) -> Option<gix_pack::data::entry::Location> {
223219
self.inner.location_by_oid(id, buf)
224220
}
225221

gix-odb/src/find.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
/// The error type returned by the [`Find`](crate::Find) and [`Header`](crate::Header) traits.
2+
pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
13
///
24
pub mod existing {
35
use gix_hash::ObjectId;
46

57
/// The error returned by the [`find(…)`][crate::FindExt::find()] trait methods.
68
#[derive(Debug, thiserror::Error)]
79
#[allow(missing_docs)]
8-
pub enum Error<T: std::error::Error + 'static> {
10+
pub enum Error {
911
#[error(transparent)]
10-
Find(T),
12+
Find(crate::find::Error),
1113
#[error("An object with id {} could not be found", .oid)]
1214
NotFound { oid: ObjectId },
1315
}
@@ -20,9 +22,9 @@ pub mod existing_object {
2022
/// The error returned by the various [`find_*()`][crate::FindExt::find_commit()] trait methods.
2123
#[derive(Debug, thiserror::Error)]
2224
#[allow(missing_docs)]
23-
pub enum Error<T: std::error::Error + 'static> {
25+
pub enum Error {
2426
#[error(transparent)]
25-
Find(T),
27+
Find(crate::find::Error),
2628
#[error(transparent)]
2729
Decode(gix_object::decode::Error),
2830
#[error("An object with id {oid} could not be found")]
@@ -39,9 +41,9 @@ pub mod existing_iter {
3941
/// The error returned by the various [`find_*_iter()`][crate::FindExt::find_commit_iter()] trait methods.
4042
#[derive(Debug, thiserror::Error)]
4143
#[allow(missing_docs)]
42-
pub enum Error<T: std::error::Error + 'static> {
44+
pub enum Error {
4345
#[error(transparent)]
44-
Find(T),
46+
Find(crate::find::Error),
4547
#[error("An object with id {oid} could not be found")]
4648
NotFound { oid: ObjectId },
4749
#[error("Expected object of kind {expected}")]

gix-odb/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ mod traits;
7676

7777
pub use traits::{Find, FindExt, Header, HeaderExt, Write};
7878

79+
///
80+
pub mod write {
81+
/// The error type returned by the [`Write`](crate::Write) trait.
82+
pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
83+
}
84+
7985
/// A thread-local handle to access any object.
8086
pub type Handle = Cache<store::Handle<OwnShared<Store>>>;
8187
/// A thread-local handle to access any object, but thread-safe and independent of the actual type of `OwnShared` or feature toggles in `gix-features`.

gix-odb/src/sink.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ impl Sink {
2121
}
2222

2323
impl crate::traits::Write for Sink {
24-
type Error = io::Error;
25-
2624
fn write_stream(
2725
&self,
2826
kind: gix_object::Kind,
2927
size: u64,
3028
mut from: impl io::Read,
31-
) -> Result<gix_hash::ObjectId, Self::Error> {
29+
) -> Result<gix_hash::ObjectId, crate::write::Error> {
3230
let mut size = size.try_into().expect("object size to fit into usize");
3331
let mut buf = [0u8; 8096];
3432
let header = gix_object::encode::loose_header(kind, size);
@@ -42,18 +40,18 @@ impl crate::traits::Write for Sink {
4240

4341
let mut hasher = gix_features::hash::hasher(self.object_hash);
4442
hasher.update(&header);
45-
possibly_compress(&header)?;
43+
possibly_compress(&header).map_err(Box::new)?;
4644

4745
while size != 0 {
4846
let bytes = size.min(buf.len());
49-
from.read_exact(&mut buf[..bytes])?;
47+
from.read_exact(&mut buf[..bytes]).map_err(Box::new)?;
5048
hasher.update(&buf[..bytes]);
51-
possibly_compress(&buf[..bytes])?;
49+
possibly_compress(&buf[..bytes]).map_err(Box::new)?;
5250
size -= bytes;
5351
}
5452
if let Some(compressor) = self.compressor.as_ref() {
5553
let mut c = compressor.borrow_mut();
56-
c.flush()?;
54+
c.flush().map_err(Box::new)?;
5755
c.reset();
5856
}
5957

gix-odb/src/store_impls/dynamic/find.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ where
8888
mut id: &'b gix_hash::oid,
8989
buffer: &'a mut Vec<u8>,
9090
inflate: &mut zlib::Inflate,
91-
pack_cache: &mut impl DecodeEntry,
91+
pack_cache: &mut dyn DecodeEntry,
9292
snapshot: &mut load_index::Snapshot,
9393
recursion: Option<error::DeltaBaseRecursion<'_>>,
9494
) -> Result<Option<(gix_object::Data<'a>, Option<gix_pack::data::entry::Location>)>, Error> {
@@ -150,7 +150,7 @@ where
150150
entry,
151151
buffer,
152152
inflate,
153-
|id, _out| {
153+
&|id, _out| {
154154
index_file.pack_offset_by_id(id).map(|pack_offset| {
155155
gix_pack::data::decode::entry::ResolvedBase::InPack(pack.entry(pack_offset))
156156
})
@@ -236,7 +236,7 @@ where
236236
entry,
237237
buffer,
238238
inflate,
239-
|id, out| {
239+
&|id, out| {
240240
index_file
241241
.pack_offset_by_id(id)
242242
.map(|pack_offset| {
@@ -311,10 +311,8 @@ impl<S> gix_pack::Find for super::Handle<S>
311311
where
312312
S: Deref<Target = super::Store> + Clone,
313313
{
314-
type Error = Error;
315-
316314
// TODO: probably make this method fallible, but that would mean its own error type.
317-
fn contains(&self, id: impl AsRef<gix_hash::oid>) -> bool {
315+
fn contains(&self, id: &gix_hash::oid) -> bool {
318316
let id = id.as_ref();
319317
let mut snapshot = self.snapshot.borrow_mut();
320318
loop {
@@ -346,21 +344,18 @@ where
346344

347345
fn try_find_cached<'a>(
348346
&self,
349-
id: impl AsRef<gix_hash::oid>,
347+
id: &gix_hash::oid,
350348
buffer: &'a mut Vec<u8>,
351-
pack_cache: &mut impl DecodeEntry,
352-
) -> Result<Option<(gix_object::Data<'a>, Option<gix_pack::data::entry::Location>)>, Self::Error> {
349+
pack_cache: &mut dyn DecodeEntry,
350+
) -> Result<Option<(gix_object::Data<'a>, Option<gix_pack::data::entry::Location>)>, gix_pack::find::Error> {
353351
let id = id.as_ref();
354352
let mut snapshot = self.snapshot.borrow_mut();
355353
let mut inflate = self.inflate.borrow_mut();
356354
self.try_find_cached_inner(id, buffer, &mut inflate, pack_cache, &mut snapshot, None)
355+
.map_err(|err| Box::new(err) as _)
357356
}
358357

359-
fn location_by_oid(
360-
&self,
361-
id: impl AsRef<gix_hash::oid>,
362-
buf: &mut Vec<u8>,
363-
) -> Option<gix_pack::data::entry::Location> {
358+
fn location_by_oid(&self, id: &gix_hash::oid, buf: &mut Vec<u8>) -> Option<gix_pack::data::entry::Location> {
364359
assert!(
365360
matches!(self.token.as_ref(), Some(handle::Mode::KeepDeletedPacksAvailable)),
366361
"BUG: handle must be configured to `prevent_pack_unload()` before using this method"
@@ -511,17 +506,15 @@ where
511506
S: Deref<Target = super::Store> + Clone,
512507
Self: gix_pack::Find,
513508
{
514-
type Error = <Self as gix_pack::Find>::Error;
515-
516509
fn contains(&self, id: impl AsRef<gix_hash::oid>) -> bool {
517-
gix_pack::Find::contains(self, id)
510+
gix_pack::Find::contains(self, id.as_ref())
518511
}
519512

520513
fn try_find<'a>(
521514
&self,
522515
id: impl AsRef<gix_hash::oid>,
523516
buffer: &'a mut Vec<u8>,
524-
) -> Result<Option<gix_object::Data<'a>>, Self::Error> {
525-
gix_pack::Find::try_find(self, id, buffer).map(|t| t.map(|t| t.0))
517+
) -> Result<Option<gix_object::Data<'a>>, crate::find::Error> {
518+
gix_pack::Find::try_find(self, id.as_ref(), buffer).map(|t| t.map(|t| t.0))
526519
}
527520
}

gix-odb/src/store_impls/dynamic/header.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ where
7373
},
7474
};
7575
let entry = pack.entry(pack_offset);
76-
let res = match pack.decode_header(entry, inflate, |id| {
76+
let res = match pack.decode_header(entry, inflate, &|id| {
7777
index_file.pack_offset_by_id(id).map(|pack_offset| {
7878
gix_pack::data::decode::header::ResolvedBase::InPack(pack.entry(pack_offset))
7979
})
@@ -130,7 +130,7 @@ where
130130
.as_ref()
131131
.expect("pack to still be available like just now");
132132
let entry = pack.entry(pack_offset);
133-
pack.decode_header(entry, inflate, |id| {
133+
pack.decode_header(entry, inflate, &|id| {
134134
index_file
135135
.pack_offset_by_id(id)
136136
.map(|pack_offset| {
@@ -182,12 +182,11 @@ impl<S> crate::Header for super::Handle<S>
182182
where
183183
S: Deref<Target = super::Store> + Clone,
184184
{
185-
type Error = Error;
186-
187-
fn try_header(&self, id: impl AsRef<oid>) -> Result<Option<Header>, Self::Error> {
185+
fn try_header(&self, id: impl AsRef<oid>) -> Result<Option<Header>, crate::find::Error> {
188186
let id = id.as_ref();
189187
let mut snapshot = self.snapshot.borrow_mut();
190188
let mut inflate = self.inflate.borrow_mut();
191189
self.try_header_inner(id, &mut inflate, &mut snapshot, None)
190+
.map_err(|err| Box::new(err) as _)
192191
}
193192
}

gix-odb/src/store_impls/dynamic/prefix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ pub mod disambiguate {
4040
/// matching this prefix.
4141
pub fn new(id: impl Into<gix_hash::ObjectId>, hex_len: usize) -> Result<Self, gix_hash::prefix::Error> {
4242
let id = id.into();
43-
gix_hash::Prefix::new(id, hex_len)?;
43+
gix_hash::Prefix::new(&id, hex_len)?;
4444
Ok(Candidate { id, hex_len })
4545
}
4646

4747
/// Transform ourselves into a `Prefix` with our current hex lengths.
4848
pub fn to_prefix(&self) -> gix_hash::Prefix {
49-
gix_hash::Prefix::new(self.id, self.hex_len).expect("our hex-len to always be in bounds")
49+
gix_hash::Prefix::new(&self.id, self.hex_len).expect("our hex-len to always be in bounds")
5050
}
5151

5252
pub(crate) fn inc_hex_len(&mut self) {

0 commit comments

Comments
 (0)