From d730b250a01a374aec73317cadad0a393e5b610c Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 13 Oct 2023 03:57:28 +0900 Subject: [PATCH] view: add absent RemoteRef constructors, proxy methods, and conversion impls Copied from RefTarget. --- lib/src/op_store.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lib/src/op_store.rs b/lib/src/op_store.rs index 52d6f4d95..ab4504833 100644 --- a/lib/src/op_store.rs +++ b/lib/src/op_store.rs @@ -143,6 +143,33 @@ content_hash! { } } +impl RemoteRef { + /// Creates remote ref pointing to no commit. + pub fn absent() -> Self { + RemoteRef { + target: RefTarget::absent(), + } + } + + /// Returns remote ref pointing to no commit. + /// + /// This will typically be used in place of `None` returned by map lookup. + pub fn absent_ref() -> &'static Self { + static TARGET: Lazy = Lazy::new(RemoteRef::absent); + &TARGET + } + + /// Returns true if the target points to no commit. + pub fn is_absent(&self) -> bool { + self.target.is_absent() + } + + /// Returns true if the target points to any commit. + pub fn is_present(&self) -> bool { + self.target.is_present() + } +} + /// Helper to strip redundant `Option` from `RefTarget` lookup result. pub trait RefTargetOptionExt { type Value; @@ -166,6 +193,22 @@ impl<'a> RefTargetOptionExt for Option<&'a RefTarget> { } } +impl RefTargetOptionExt for Option { + type Value = RemoteRef; + + fn flatten(self) -> Self::Value { + self.unwrap_or_else(RemoteRef::absent) + } +} + +impl<'a> RefTargetOptionExt for Option<&'a RemoteRef> { + type Value = &'a RemoteRef; + + fn flatten(self) -> Self::Value { + self.unwrap_or_else(|| RemoteRef::absent_ref()) + } +} + /// Local and remote branches of the same branch name. #[derive(PartialEq, Eq, Clone, Debug)] pub struct BranchTarget<'a> {