Skip to content

Commit eb8ed39

Browse files
committed
fix!: use dyn trait where possible.
This reduces compile time due to avoiding duplication.
1 parent 09b3633 commit eb8ed39

File tree

8 files changed

+48
-45
lines changed

8 files changed

+48
-45
lines changed

gix-object/src/blob.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@ use crate::{Blob, BlobRef, Kind};
44

55
impl<'a> crate::WriteTo for BlobRef<'a> {
66
/// Write the blobs data to `out` verbatim.
7-
fn write_to(&self, mut out: impl io::Write) -> io::Result<()> {
7+
fn write_to(&self, out: &mut dyn io::Write) -> io::Result<()> {
88
out.write_all(self.data)
99
}
1010

11-
fn size(&self) -> usize {
12-
self.data.len()
13-
}
14-
1511
fn kind(&self) -> Kind {
1612
Kind::Blob
1713
}
14+
15+
fn size(&self) -> usize {
16+
self.data.len()
17+
}
1818
}
1919

2020
impl crate::WriteTo for Blob {
2121
/// Write the blobs data to `out` verbatim.
22-
fn write_to(&self, out: impl io::Write) -> io::Result<()> {
22+
fn write_to(&self, out: &mut dyn io::Write) -> io::Result<()> {
2323
self.to_ref().write_to(out)
2424
}
2525

26-
fn size(&self) -> usize {
27-
self.to_ref().size()
28-
}
29-
3026
fn kind(&self) -> Kind {
3127
Kind::Blob
3228
}
29+
30+
fn size(&self) -> usize {
31+
self.to_ref().size()
32+
}
3333
}
3434

3535
impl Blob {

gix-object/src/commit/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{encode, encode::NL, Commit, CommitRef, Kind};
66

77
impl crate::WriteTo for Commit {
88
/// Serializes this instance to `out` in the git serialization format.
9-
fn write_to(&self, mut out: impl io::Write) -> io::Result<()> {
9+
fn write_to(&self, mut out: &mut dyn io::Write) -> io::Result<()> {
1010
encode::trusted_header_id(b"tree", &self.tree, &mut out)?;
1111
for parent in &self.parents {
1212
encode::trusted_header_id(b"parent", parent, &mut out)?;
@@ -52,7 +52,7 @@ impl crate::WriteTo for Commit {
5252

5353
impl<'a> crate::WriteTo for CommitRef<'a> {
5454
/// Serializes this instance to `out` in the git serialization format.
55-
fn write_to(&self, mut out: impl io::Write) -> io::Result<()> {
55+
fn write_to(&self, mut out: &mut dyn io::Write) -> io::Result<()> {
5656
encode::trusted_header_id(b"tree", &self.tree(), &mut out)?;
5757
for parent in self.parents() {
5858
encode::trusted_header_id(b"parent", &parent, &mut out)?;

gix-object/src/data.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ pub mod verify {
6767
/// Compute the checksum of `self` and compare it with the `desired` hash.
6868
/// If the hashes do not match, an [`Error`] is returned, containing the actual
6969
/// hash of `self`.
70-
pub fn verify_checksum(&self, desired: impl AsRef<gix_hash::oid>) -> Result<(), Error> {
71-
let desired = desired.as_ref();
70+
pub fn verify_checksum(&self, desired: &gix_hash::oid) -> Result<(), Error> {
7271
let actual_id = crate::compute_hash(desired.kind(), self.kind, self.data);
7372
if desired != actual_id {
7473
return Err(Error::ChecksumMismatch {

gix-object/src/encode.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ impl From<Error> for io::Error {
3434
}
3535
}
3636

37-
pub(crate) fn header_field_multi_line(name: &[u8], value: &[u8], mut out: impl io::Write) -> io::Result<()> {
37+
pub(crate) fn header_field_multi_line(name: &[u8], value: &[u8], out: &mut dyn io::Write) -> io::Result<()> {
3838
let mut lines = value.as_bstr().split_str(b"\n");
39-
trusted_header_field(name, lines.next().ok_or(Error::EmptyValue)?, &mut out)?;
39+
trusted_header_field(name, lines.next().ok_or(Error::EmptyValue)?, out)?;
4040
for line in lines {
4141
out.write_all(SPACE)?;
4242
out.write_all(line)?;
@@ -45,7 +45,7 @@ pub(crate) fn header_field_multi_line(name: &[u8], value: &[u8], mut out: impl i
4545
Ok(())
4646
}
4747

48-
pub(crate) fn trusted_header_field(name: &[u8], value: &[u8], mut out: impl io::Write) -> io::Result<()> {
48+
pub(crate) fn trusted_header_field(name: &[u8], value: &[u8], out: &mut dyn io::Write) -> io::Result<()> {
4949
out.write_all(name)?;
5050
out.write_all(SPACE)?;
5151
out.write_all(value)?;
@@ -55,22 +55,26 @@ pub(crate) fn trusted_header_field(name: &[u8], value: &[u8], mut out: impl io::
5555
pub(crate) fn trusted_header_signature(
5656
name: &[u8],
5757
value: &gix_actor::SignatureRef<'_>,
58-
mut out: impl io::Write,
58+
out: &mut dyn io::Write,
5959
) -> io::Result<()> {
6060
out.write_all(name)?;
6161
out.write_all(SPACE)?;
62-
value.write_to(&mut out)?;
62+
value.write_to(out)?;
6363
out.write_all(NL)
6464
}
6565

66-
pub(crate) fn trusted_header_id(name: &[u8], value: &gix_hash::ObjectId, mut out: impl io::Write) -> io::Result<()> {
66+
pub(crate) fn trusted_header_id(
67+
name: &[u8],
68+
value: &gix_hash::ObjectId,
69+
mut out: &mut dyn io::Write,
70+
) -> io::Result<()> {
6771
out.write_all(name)?;
6872
out.write_all(SPACE)?;
6973
value.write_hex_to(&mut out)?;
7074
out.write_all(NL)
7175
}
7276

73-
pub(crate) fn header_field(name: &[u8], value: &[u8], out: impl io::Write) -> io::Result<()> {
77+
pub(crate) fn header_field(name: &[u8], value: &[u8], out: &mut dyn io::Write) -> io::Result<()> {
7478
if value.is_empty() {
7579
return Err(Error::EmptyValue.into());
7680
}

gix-object/src/object/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod write {
1010
/// Serialization
1111
impl<'a> WriteTo for ObjectRef<'a> {
1212
/// Write the contained object to `out` in the git serialization format.
13-
fn write_to(&self, out: impl io::Write) -> io::Result<()> {
13+
fn write_to(&self, out: &mut dyn io::Write) -> io::Result<()> {
1414
use crate::ObjectRef::*;
1515
match self {
1616
Tree(v) => v.write_to(out),
@@ -20,6 +20,10 @@ mod write {
2020
}
2121
}
2222

23+
fn kind(&self) -> Kind {
24+
self.kind()
25+
}
26+
2327
fn size(&self) -> usize {
2428
use crate::ObjectRef::*;
2529
match self {
@@ -29,16 +33,12 @@ mod write {
2933
Tag(v) => v.size(),
3034
}
3135
}
32-
33-
fn kind(&self) -> Kind {
34-
self.kind()
35-
}
3636
}
3737

3838
/// Serialization
3939
impl WriteTo for Object {
4040
/// Write the contained object to `out` in the git serialization format.
41-
fn write_to(&self, out: impl io::Write) -> io::Result<()> {
41+
fn write_to(&self, out: &mut dyn io::Write) -> io::Result<()> {
4242
use crate::Object::*;
4343
match self {
4444
Tree(v) => v.write_to(out),
@@ -48,6 +48,10 @@ mod write {
4848
}
4949
}
5050

51+
fn kind(&self) -> Kind {
52+
self.kind()
53+
}
54+
5155
fn size(&self) -> usize {
5256
use crate::Object::*;
5357
match self {
@@ -57,10 +61,6 @@ mod write {
5761
Tag(v) => v.size(),
5862
}
5963
}
60-
61-
fn kind(&self) -> Kind {
62-
self.kind()
63-
}
6464
}
6565
}
6666

gix-object/src/tag/write.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ impl From<Error> for io::Error {
2121
}
2222

2323
impl crate::WriteTo for Tag {
24-
fn write_to(&self, mut out: impl io::Write) -> io::Result<()> {
25-
encode::trusted_header_id(b"object", &self.target, &mut out)?;
26-
encode::trusted_header_field(b"type", self.target_kind.as_bytes(), &mut out)?;
27-
encode::header_field(b"tag", validated_name(self.name.as_ref())?, &mut out)?;
24+
fn write_to(&self, out: &mut dyn io::Write) -> io::Result<()> {
25+
encode::trusted_header_id(b"object", &self.target, out)?;
26+
encode::trusted_header_field(b"type", self.target_kind.as_bytes(), out)?;
27+
encode::header_field(b"tag", validated_name(self.name.as_ref())?, out)?;
2828
if let Some(tagger) = &self.tagger {
29-
encode::trusted_header_signature(b"tagger", &tagger.to_ref(), &mut out)?;
29+
encode::trusted_header_signature(b"tagger", &tagger.to_ref(), out)?;
3030
}
3131

3232
out.write_all(NL)?;
@@ -58,7 +58,7 @@ impl crate::WriteTo for Tag {
5858
}
5959

6060
impl<'a> crate::WriteTo for TagRef<'a> {
61-
fn write_to(&self, mut out: impl io::Write) -> io::Result<()> {
61+
fn write_to(&self, mut out: &mut dyn io::Write) -> io::Result<()> {
6262
encode::trusted_header_field(b"object", self.target, &mut out)?;
6363
encode::trusted_header_field(b"type", self.target_kind.as_bytes(), &mut out)?;
6464
encode::header_field(b"tag", validated_name(self.name)?, &mut out)?;

gix-object/src/traits.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::Kind;
55
/// Writing of objects to a `Write` implementation
66
pub trait WriteTo {
77
/// Write a representation of this instance to `out`.
8-
fn write_to(&self, out: impl std::io::Write) -> std::io::Result<()>;
8+
fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()>;
99

1010
/// Returns the type of this object.
1111
fn kind(&self) -> Kind;
@@ -29,15 +29,15 @@ impl<T> WriteTo for &T
2929
where
3030
T: WriteTo,
3131
{
32-
fn write_to(&self, out: impl Write) -> std::io::Result<()> {
32+
fn write_to(&self, out: &mut dyn Write) -> std::io::Result<()> {
3333
<T as WriteTo>::write_to(self, out)
3434
}
3535

36-
fn size(&self) -> usize {
37-
<T as WriteTo>::size(self)
38-
}
39-
4036
fn kind(&self) -> Kind {
4137
<T as WriteTo>::kind(self)
4238
}
39+
40+
fn size(&self) -> usize {
41+
<T as WriteTo>::size(self)
42+
}
4343
}

gix-object/src/tree/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl From<Error> for io::Error {
2525
/// Serialization
2626
impl crate::WriteTo for Tree {
2727
/// Serialize this tree to `out` in the git internal format.
28-
fn write_to(&self, mut out: impl io::Write) -> io::Result<()> {
28+
fn write_to(&self, out: &mut dyn io::Write) -> io::Result<()> {
2929
debug_assert_eq!(
3030
&{
3131
let mut entries_sorted = self.entries.clone();
@@ -68,7 +68,7 @@ impl crate::WriteTo for Tree {
6868
/// Serialization
6969
impl<'a> crate::WriteTo for TreeRef<'a> {
7070
/// Serialize this tree to `out` in the git internal format.
71-
fn write_to(&self, mut out: impl io::Write) -> io::Result<()> {
71+
fn write_to(&self, out: &mut dyn io::Write) -> io::Result<()> {
7272
debug_assert_eq!(
7373
&{
7474
let mut entries_sorted = self.entries.clone();

0 commit comments

Comments
 (0)