From 2e011a6ba50636abe72e095bece26ad7bdc7bad3 Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Fri, 3 Nov 2023 20:46:43 +0900 Subject: [PATCH] Change ParseTree::write_one_entry to use a Write instead of a Vec --- src/cinnabar/manifest.rs | 4 +++- src/git/tree.rs | 4 +++- src/hg/manifest.rs | 15 ++++++++------- src/main.rs | 8 ++++---- src/store.rs | 7 ++++--- src/tree_util.rs | 7 ++++--- 6 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/cinnabar/manifest.rs b/src/cinnabar/manifest.rs index 4225dc5cf..00c963f60 100644 --- a/src/cinnabar/manifest.rs +++ b/src/cinnabar/manifest.rs @@ -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}; @@ -85,7 +87,7 @@ impl ParseTree for GitManifestTree { )) } - fn write_one_entry(_entry: &WithPath, _buf: &mut Vec) { + fn write_one_entry(_entry: &WithPath, _w: W) -> io::Result<()> { todo!() } } diff --git a/src/git/tree.rs b/src/git/tree.rs index 228d54bf0..e743d2313 100644 --- a/src/git/tree.rs +++ b/src/git/tree.rs @@ -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; @@ -86,7 +88,7 @@ impl ParseTree for RawTree { .ok_or(MalformedTree) } - fn write_one_entry(_entry: &WithPath, _buf: &mut Vec) { + fn write_one_entry(_entry: &WithPath, _w: W) -> io::Result<()> { todo!() } } diff --git a/src/hg/manifest.rs b/src/hg/manifest.rs index b4a2e003e..d85f4f0ae 100644 --- a/src/hg/manifest.rs +++ b/src/hg/manifest.rs @@ -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}; @@ -109,12 +109,13 @@ impl ParseTree for RawHgManifest { .ok_or(MalformedManifest) } - fn write_one_entry(entry: &WithPath, buf: &mut Vec) { - 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(entry: &WithPath, 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(()) } } diff --git a/src/main.rs b/src/main.rs index 4254af7ab..0e489e5d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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'); } @@ -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 { .. }) => { @@ -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'); } @@ -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) diff --git a/src/store.rs b/src/store.rs index 9ade63e62..42378fc37 100644 --- a/src/store.rs +++ b/src/store.rs @@ -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 @@ -624,7 +625,7 @@ impl RawHgManifest { }) .transpose() { - RawHgManifest::write_one_entry(&new_entry, &mut manifest); + RawHgManifest::write_one_entry(&new_entry, &mut manifest).unwrap(); } } }; @@ -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()); diff --git a/src/tree_util.rs b/src/tree_util.rs index c7881d3a7..fefc72012 100644 --- a/src/tree_util.rs +++ b/src/tree_util.rs @@ -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}; @@ -297,7 +298,7 @@ pub trait ParseTree: AsRef<[u8]> { fn parse_one_entry(buf: &mut &[u8]) -> Result, Self::Error>; /// Write one entry into the given buffer. - fn write_one_entry(entry: &WithPath, buf: &mut Vec); + fn write_one_entry(entry: &WithPath, w: W) -> io::Result<()>; /// Iterates the tree fn iter(&self) -> TreeIter<&Self> { @@ -313,8 +314,8 @@ impl ParseTree for &T { T::parse_one_entry(buf) } - fn write_one_entry(entry: &WithPath, buf: &mut Vec) { - T::write_one_entry(entry, buf); + fn write_one_entry(entry: &WithPath, w: W) -> io::Result<()> { + T::write_one_entry(entry, w) } }