Skip to content

Commit

Permalink
Change ParseTree::write_one_entry to use a Write instead of a Vec
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Nov 3, 2023
1 parent f20b5b3 commit 2e011a6
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/cinnabar/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::io::{self, Write};

use either::Either;

use crate::git::{git_oid_type, CommitId, MalformedTree, TreeId, TreeIsh};
Expand Down Expand Up @@ -85,7 +87,7 @@ impl ParseTree for GitManifestTree {
))
}

fn write_one_entry(_entry: &WithPath<Self::Inner>, _buf: &mut Vec<u8>) {
fn write_one_entry<W: Write>(_entry: &WithPath<Self::Inner>, _w: W) -> io::Result<()> {
todo!()
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/git/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::io::{self, Write};

use digest::OutputSizeUser;
use either::Either;

Expand Down Expand Up @@ -86,7 +88,7 @@ impl ParseTree for RawTree {
.ok_or(MalformedTree)
}

fn write_one_entry(_entry: &WithPath<Self::Inner>, _buf: &mut Vec<u8>) {
fn write_one_entry<W: Write>(_entry: &WithPath<Self::Inner>, _w: W) -> io::Result<()> {
todo!()
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/hg/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::io::Write;
use std::io::{self, Write};

use bstr::{BStr, ByteSlice};

Expand Down Expand Up @@ -109,12 +109,13 @@ impl ParseTree for RawHgManifest {
.ok_or(MalformedManifest)
}

fn write_one_entry(entry: &WithPath<Self::Inner>, buf: &mut Vec<u8>) {
buf.extend_from_slice(entry.path());
buf.push(b'\0');
write!(buf, "{}", entry.inner().fid).unwrap();
buf.extend_from_slice(entry.inner().attr.as_bstr());
buf.push(b'\n');
fn write_one_entry<W: Write>(entry: &WithPath<Self::Inner>, mut w: W) -> io::Result<()> {
w.write_all(entry.path())?;
w.write_all(b"\0")?;
write!(w, "{}", entry.inner().fid)?;
w.write_all(entry.inner().attr.as_bstr())?;
w.write_all(b"\n")?;
Ok(())
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,7 @@ fn create_root_changeset(cid: CommitId) -> HgChangesetId {
attr: item.mode.try_into().unwrap(),
})
{
RawHgManifest::write_one_entry(&entry, &mut manifest);
RawHgManifest::write_one_entry(&entry, &mut manifest).unwrap();
paths.extend_from_slice(entry.path());
paths.push(b'\0');
}
Expand Down Expand Up @@ -1657,7 +1657,7 @@ fn create_simple_manifest(cid: CommitId, parent: CommitId) -> (HgManifestId, Opt
{
let (fid, mode) = match either_or_both {
EitherOrBoth::Left(info) => {
RawHgManifest::write_one_entry(&WithPath::new(path, info), &mut manifest);
RawHgManifest::write_one_entry(&WithPath::new(path, info), &mut manifest).unwrap();
continue;
}
EitherOrBoth::Both(_, DiffTreeItem::Deleted { .. }) => {
Expand Down Expand Up @@ -1701,7 +1701,7 @@ fn create_simple_manifest(cid: CommitId, parent: CommitId) -> (HgManifestId, Opt
attr: mode.try_into().unwrap(),
},
);
RawHgManifest::write_one_entry(&entry, &mut manifest);
RawHgManifest::write_one_entry(&entry, &mut manifest).unwrap();
paths.extend_from_slice(entry.path());
paths.push(b'\0');
}
Expand Down Expand Up @@ -1874,7 +1874,7 @@ fn create_merge_changeset(
attr: l.mode.try_into().unwrap(),
},
);
RawHgManifest::write_one_entry(&line, &mut manifest);
RawHgManifest::write_one_entry(&line, &mut manifest).unwrap();
if !unchanged
|| p1_attr
.map(|attr| attr != line.inner().attr)
Expand Down
7 changes: 4 additions & 3 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,8 @@ impl RawHgManifest {
inner.right().unwrap()
}),
&mut manifest,
),
)
.unwrap(),
// There was an entry in the last manifest, but the file was modified or removed
Both(_, diff) => {
if let Some(new_entry) = diff
Expand All @@ -624,7 +625,7 @@ impl RawHgManifest {
})
.transpose()
{
RawHgManifest::write_one_entry(&new_entry, &mut manifest);
RawHgManifest::write_one_entry(&new_entry, &mut manifest).unwrap();
}
}
};
Expand All @@ -635,7 +636,7 @@ impl RawHgManifest {
.into_iter()
.recurse()
{
RawHgManifest::write_one_entry(&entry, &mut manifest);
RawHgManifest::write_one_entry(&entry, &mut manifest).unwrap();
}
}
let content = Rc::<[u8]>::from(manifest.as_ref());
Expand Down
7 changes: 4 additions & 3 deletions src/tree_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! Helpers related to trees.
use std::cmp::Ordering;
use std::io::{self, Write};
use std::iter::{zip, Peekable};

use bstr::{BStr, BString, ByteSlice};
Expand Down Expand Up @@ -297,7 +298,7 @@ pub trait ParseTree: AsRef<[u8]> {
fn parse_one_entry(buf: &mut &[u8]) -> Result<WithPath<Self::Inner>, Self::Error>;

/// Write one entry into the given buffer.
fn write_one_entry(entry: &WithPath<Self::Inner>, buf: &mut Vec<u8>);
fn write_one_entry<W: Write>(entry: &WithPath<Self::Inner>, w: W) -> io::Result<()>;

/// Iterates the tree
fn iter(&self) -> TreeIter<&Self> {
Expand All @@ -313,8 +314,8 @@ impl<T: ParseTree + ?Sized> ParseTree for &T {
T::parse_one_entry(buf)
}

fn write_one_entry(entry: &WithPath<Self::Inner>, buf: &mut Vec<u8>) {
T::write_one_entry(entry, buf);
fn write_one_entry<W: Write>(entry: &WithPath<Self::Inner>, w: W) -> io::Result<()> {
T::write_one_entry(entry, w)
}
}

Expand Down

0 comments on commit 2e011a6

Please sign in to comment.