Mercurial > public > mercurial-scm > hg
changeset 52762:169ccd142ef8
rust: add methods that take checked revisions
This continues the work done in a3fa37bdb7ecbad4cf31bc6e87545fd42058a93b,
turning more methods into checked/unchecked pairs of methods.
author | Mitchell Kember <mkember@janestreet.com> |
---|---|
date | Thu, 16 Jan 2025 15:36:26 -0500 |
parents | 8497cfb0d76c |
children | 37e9e6e1f470 |
files | rust/hg-core/src/revlog/changelog.rs rust/hg-core/src/revlog/mod.rs |
diffstat | 2 files changed, 18 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/rust/hg-core/src/revlog/changelog.rs Tue Jan 14 17:44:02 2025 -0500 +++ b/rust/hg-core/src/revlog/changelog.rs Thu Jan 16 15:36:26 2025 -0500 @@ -53,7 +53,7 @@ } /// Same as [`Self::entry_for_unchecked_rev`] for a checked revision - fn entry(&self, rev: Revision) -> Result<ChangelogEntry, RevlogError> { + pub fn entry(&self, rev: Revision) -> Result<ChangelogEntry, RevlogError> { let revlog_entry = self.revlog.get_entry(rev)?; Ok(ChangelogEntry { revlog_entry }) } @@ -71,11 +71,15 @@ self.entry_for_unchecked_rev(rev)?.data() } + pub fn node_from_rev(&self, rev: Revision) -> &Node { + self.revlog.node_from_rev(rev) + } + pub fn node_from_unchecked_rev( &self, rev: UncheckedRevision, ) -> Option<&Node> { - self.revlog.node_from_rev(rev) + self.revlog.node_from_unchecked_rev(rev) } pub fn rev_from_node(
--- a/rust/hg-core/src/revlog/mod.rs Tue Jan 14 17:44:02 2025 -0500 +++ b/rust/hg-core/src/revlog/mod.rs Thu Jan 16 15:36:26 2025 -0500 @@ -343,12 +343,19 @@ /// Returns the node ID for the given revision number, if it exists in this /// revlog - pub fn node_from_rev(&self, rev: UncheckedRevision) -> Option<&Node> { - if rev == NULL_REVISION.into() { - return Some(&NULL_NODE); + pub fn node_from_rev(&self, rev: Revision) -> &Node { + match self.index().get_entry(rev) { + None => &NULL_NODE, + Some(entry) => entry.hash(), } - let rev = self.index().check_revision(rev)?; - Some(self.index().get_entry(rev)?.hash()) + } + + /// Like [`Self::node_from_rev`] but checks `rev` first. + pub fn node_from_unchecked_rev( + &self, + rev: UncheckedRevision, + ) -> Option<&Node> { + Some(self.node_from_rev(self.index().check_revision(rev)?)) } /// Return the revision number for the given node ID, if it exists in this