Skip to content

Commit

Permalink
fix!: use dyn trait where possible.
Browse files Browse the repository at this point in the history
This reduces compile time due to avoiding duplication.
  • Loading branch information
Byron committed Sep 6, 2023
1 parent 24dd870 commit 072ee32
Show file tree
Hide file tree
Showing 289 changed files with 1,694 additions and 1,641 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gitoxide-core/src/commitgraph/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub(crate) mod function {
W1: io::Write,
W2: io::Write,
{
let g = Graph::at(path).with_context(|| "Could not open commit graph")?;
let g = Graph::at(path.as_ref()).with_context(|| "Could not open commit graph")?;

#[allow(clippy::unnecessary_wraps, unknown_lints)]
fn noop_processor(_commit: &gix::commitgraph::file::Commit<'_>) -> std::result::Result<(), std::fmt::Error> {
Expand Down
3 changes: 2 additions & 1 deletion gitoxide-core/src/corpus/run.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use gix::progress::DynNestedProgress;
use std::{path::Path, sync::atomic::AtomicBool};

use crate::{
Expand Down Expand Up @@ -141,7 +142,7 @@ impl Execute for VerifyOdb {
crate::repository::verify::integrity(
repo,
std::io::sink(),
progress,
progress.add_child("integrity".into()),
should_interrupt,
crate::repository::verify::Context {
output_statistics: None,
Expand Down
8 changes: 4 additions & 4 deletions gitoxide-core/src/index/checkout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ pub fn checkout_exclusive(
}
}
},
&mut files,
&mut bytes,
&files,
&bytes,
should_interrupt,
opts,
),
Expand All @@ -116,8 +116,8 @@ pub fn checkout_exclusive(
buf.clear();
Ok(gix::objs::BlobRef { data: buf })
},
&mut files,
&mut bytes,
&files,
&bytes,
should_interrupt,
opts,
),
Expand Down
4 changes: 2 additions & 2 deletions gitoxide-core/src/organize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ pub fn find_git_repository_workdirs(
fn find_origin_remote(repo: &Path) -> anyhow::Result<Option<gix_url::Url>> {
let non_bare = repo.join(".git").join("config");
let local = gix::config::Source::Local;
let config = gix::config::File::from_path_no_includes(non_bare.as_path(), local)
.or_else(|_| gix::config::File::from_path_no_includes(repo.join("config").as_path(), local))?;
let config = gix::config::File::from_path_no_includes(non_bare.as_path().into(), local)
.or_else(|_| gix::config::File::from_path_no_includes(repo.join("config"), local))?;
Ok(config
.string_by_key("remote.origin.url")
.map(|url| gix_url::Url::from_bytes(url.as_ref()))
Expand Down
21 changes: 10 additions & 11 deletions gitoxide-core/src/pack/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,13 @@ where
P: NestedProgress,
P::SubProgress: 'static,
{
type ObjectIdIter = dyn Iterator<Item = Result<ObjectId, Box<dyn std::error::Error + Send + Sync>>> + Send;

let repo = gix::discover(repository_path)?.into_sync();
progress.init(Some(2), progress::steps());
let tips = tips.into_iter();
let make_cancellation_err = || anyhow!("Cancelled by user");
let (mut handle, input): (
_,
Box<dyn Iterator<Item = Result<ObjectId, input_iteration::Error>> + Send>,
) = match input {
let (mut handle, mut input): (_, Box<ObjectIdIter>) = match input {
None => {
let mut progress = progress.add_child("traversing");
progress.init(None, progress::count("commits"));
Expand All @@ -141,7 +140,7 @@ where
let handle = handle.clone();
move |oid, buf| handle.find_commit_iter(oid, buf).map(|t| t.0)
})
.map(|res| res.map_err(Into::into).map(|c| c.id))
.map(|res| res.map_err(|err| Box::new(err) as Box<_>).map(|c| c.id))
.inspect(move |_| progress.inc()),
);
(handle, iter)
Expand All @@ -157,7 +156,7 @@ where
.lines()
.map(|hex_id| {
hex_id
.map_err(Into::into)
.map_err(|err| Box::new(err) as Box<_>)
.and_then(|hex_id| ObjectId::from_hex(hex_id.as_bytes()).map_err(Into::into))
})
.inspect(move |_| progress.inc()),
Expand Down Expand Up @@ -207,7 +206,7 @@ where
pack::data::output::count::objects(
handle.clone(),
input,
progress,
&progress,
&interrupt::IS_INTERRUPTED,
pack::data::output::count::objects::Options {
thread_limit,
Expand All @@ -217,9 +216,9 @@ where
)?
} else {
pack::data::output::count::objects_unthreaded(
handle.clone(),
input,
progress,
&handle,
&mut input,
&progress,
&interrupt::IS_INTERRUPTED,
input_object_expansion,
)?
Expand All @@ -236,7 +235,7 @@ where
InOrderIter::from(pack::data::output::entry::iter_from_counts(
counts,
handle,
progress,
Box::new(progress),
pack::data::output::entry::iter_from_counts::Options {
thread_limit,
mode: pack::data::output::entry::iter_from_counts::Mode::PackCopyAndBaseObjects,
Expand Down
31 changes: 17 additions & 14 deletions gitoxide-core/src/pack/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,22 @@ enum OutputWriter {
}

impl gix::odb::Write for OutputWriter {
type Error = Error;

fn write_buf(&self, kind: object::Kind, from: &[u8]) -> Result<ObjectId, Self::Error> {
fn write_buf(&self, kind: object::Kind, from: &[u8]) -> Result<ObjectId, gix::odb::write::Error> {
match self {
OutputWriter::Loose(db) => db.write_buf(kind, from).map_err(Into::into),
OutputWriter::Sink(db) => db.write_buf(kind, from).map_err(Into::into),
OutputWriter::Loose(db) => db.write_buf(kind, from),
OutputWriter::Sink(db) => db.write_buf(kind, from),
}
}

fn write_stream(&self, kind: object::Kind, size: u64, from: impl Read) -> Result<ObjectId, Self::Error> {
fn write_stream(
&self,
kind: object::Kind,
size: u64,
from: &mut dyn Read,
) -> Result<ObjectId, gix::odb::write::Error> {
match self {
OutputWriter::Loose(db) => db.write_stream(kind, size, from).map_err(Into::into),
OutputWriter::Sink(db) => db.write_stream(kind, size, from).map_err(Into::into),
OutputWriter::Loose(db) => db.write_stream(kind, size, from),
OutputWriter::Sink(db) => db.write_stream(kind, size, from),
}
}
}
Expand Down Expand Up @@ -137,7 +140,7 @@ pub fn pack_or_pack_index(
pack_path: impl AsRef<Path>,
object_path: Option<impl AsRef<Path>>,
check: SafetyCheck,
progress: impl NestedProgress,
mut progress: impl NestedProgress + 'static,
Context {
thread_limit,
delete_pack,
Expand Down Expand Up @@ -178,11 +181,11 @@ pub fn pack_or_pack_index(
|_| pack::index::traverse::Algorithm::Lookup,
);

let pack::index::traverse::Outcome { progress, .. } = bundle
let pack::index::traverse::Outcome { .. } = bundle
.index
.traverse(
&bundle.pack,
progress,
&mut progress,
&should_interrupt,
{
let object_path = object_path.map(|p| p.as_ref().to_owned());
Expand All @@ -193,7 +196,7 @@ pub fn pack_or_pack_index(
let mut read_buf = Vec::new();
move |object_kind, buf, index_entry, progress| {
let written_id = out.write_buf(object_kind, buf).map_err(|err| Error::Write {
source: Box::new(err) as Box<dyn std::error::Error + Send + Sync>,
source: err,
kind: object_kind,
id: index_entry.oid,
})?;
Expand All @@ -213,13 +216,13 @@ pub fn pack_or_pack_index(
}
if let Some(verifier) = loose_odb.as_ref() {
let obj = verifier
.try_find(written_id, &mut read_buf)
.try_find(&written_id, &mut read_buf)
.map_err(|err| Error::WrittenFileCorrupt {
source: err,
id: written_id,
})?
.ok_or(Error::WrittenFileMissing { id: written_id })?;
obj.verify_checksum(written_id)?;
obj.verify_checksum(&written_id)?;
}
Ok(())
}
Expand Down
16 changes: 6 additions & 10 deletions gitoxide-core/src/pack/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,12 @@ pub enum PathOrRead {
Read(Box<dyn std::io::Read + Send + 'static>),
}

pub fn from_pack<P>(
pub fn from_pack(
pack: PathOrRead,
directory: Option<PathBuf>,
progress: P,
mut progress: impl NestedProgress + 'static,
ctx: Context<'static, impl io::Write>,
) -> anyhow::Result<()>
where
P: NestedProgress,
P::SubProgress: 'static,
{
) -> anyhow::Result<()> {
use anyhow::Context;
let options = pack::bundle::write::Options {
thread_limit: ctx.thread_limit,
Expand All @@ -94,10 +90,10 @@ where
let pack_len = pack.metadata()?.len();
let pack_file = fs::File::open(pack)?;
pack::Bundle::write_to_directory_eagerly(
pack_file,
Box::new(pack_file),
Some(pack_len),
directory,
progress,
&mut progress,
ctx.should_interrupt,
None,
options,
Expand All @@ -107,7 +103,7 @@ where
input,
None,
directory,
progress,
&mut progress,
ctx.should_interrupt,
None,
options,
Expand Down
8 changes: 4 additions & 4 deletions gitoxide-core/src/pack/multi_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 1..=3;

pub fn verify(
multi_index_path: PathBuf,
progress: impl NestedProgress,
mut progress: impl NestedProgress + 'static,
should_interrupt: &AtomicBool,
) -> anyhow::Result<()> {
gix::odb::pack::multi_index::File::at(multi_index_path)?.verify_integrity_fast(progress, should_interrupt)?;
gix::odb::pack::multi_index::File::at(multi_index_path)?.verify_integrity_fast(&mut progress, should_interrupt)?;
Ok(())
}

pub fn create(
index_paths: Vec<PathBuf>,
output_path: PathBuf,
progress: impl NestedProgress,
mut progress: impl NestedProgress + 'static,
should_interrupt: &AtomicBool,
object_hash: gix::hash::Kind,
) -> anyhow::Result<()> {
Expand All @@ -31,7 +31,7 @@ pub fn create(
gix::odb::pack::multi_index::File::write_from_index_paths(
index_paths,
&mut out,
progress,
&mut progress,
should_interrupt,
gix::odb::pack::multi_index::write::Options { object_hash },
)?;
Expand Down
22 changes: 14 additions & 8 deletions gitoxide-core/src/pack/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ mod blocking_io {
fn receive_pack(
&mut self,
input: impl BufRead,
progress: impl NestedProgress,
progress: impl NestedProgress + 'static,
refs: &[Ref],
_previous_response: &Response,
) -> io::Result<()> {
Expand All @@ -156,7 +156,7 @@ mod blocking_io {
) -> anyhow::Result<()>
where
W: std::io::Write,
P: NestedProgress,
P: NestedProgress + 'static,
P::SubProgress: 'static,
{
let transport = net::connect(
Expand Down Expand Up @@ -212,7 +212,7 @@ mod async_io {
async fn receive_pack(
&mut self,
input: impl AsyncBufRead + Unpin + 'async_trait,
progress: impl gix::NestedProgress,
progress: impl gix::NestedProgress + 'static,
refs: &[Ref],
_previous_response: &Response,
) -> io::Result<()> {
Expand Down Expand Up @@ -367,8 +367,8 @@ fn receive_pack_blocking<W: io::Write>(
mut directory: Option<PathBuf>,
mut refs_directory: Option<PathBuf>,
ctx: &mut Context<W>,
input: impl io::BufRead,
progress: impl NestedProgress,
mut input: impl io::BufRead,
mut progress: impl NestedProgress + 'static,
refs: &[Ref],
) -> io::Result<()> {
let options = pack::bundle::write::Options {
Expand All @@ -377,9 +377,15 @@ fn receive_pack_blocking<W: io::Write>(
iteration_mode: pack::data::input::Mode::Verify,
object_hash: ctx.object_hash,
};
let outcome =
pack::Bundle::write_to_directory(input, directory.take(), progress, &ctx.should_interrupt, None, options)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
let outcome = pack::Bundle::write_to_directory(
&mut input,
directory.take().as_deref(),
&mut progress,
&ctx.should_interrupt,
None,
options,
)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;

if let Some(directory) = refs_directory.take() {
write_raw_refs(refs, directory)?;
Expand Down
Loading

0 comments on commit 072ee32

Please sign in to comment.