view: add absent RemoteRef constructors, proxy methods, and conversion impls

Copied from RefTarget.
This commit is contained in:
Yuya Nishihara 2023-10-13 03:57:28 +09:00
parent ab1a6e2f71
commit d730b250a0

View file

@ -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<RemoteRef> = 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<T>` from `RefTarget` lookup result.
pub trait RefTargetOptionExt {
type Value;
@ -166,6 +193,22 @@ impl<'a> RefTargetOptionExt for Option<&'a RefTarget> {
}
}
impl RefTargetOptionExt for Option<RemoteRef> {
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> {