79 lines
1.9 KiB
Rust
79 lines
1.9 KiB
Rust
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
pub enum Namespace {
|
|
Types,
|
|
Values,
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
|
pub struct PerNs<T> {
|
|
pub types: Option<T>,
|
|
pub values: Option<T>,
|
|
}
|
|
|
|
impl<T> Default for PerNs<T> {
|
|
fn default() -> Self {
|
|
PerNs { types: None, values: None }
|
|
}
|
|
}
|
|
|
|
impl<T> PerNs<T> {
|
|
pub fn none() -> PerNs<T> {
|
|
PerNs { types: None, values: None }
|
|
}
|
|
|
|
pub fn values(t: T) -> PerNs<T> {
|
|
PerNs { types: None, values: Some(t) }
|
|
}
|
|
|
|
pub fn types(t: T) -> PerNs<T> {
|
|
PerNs { types: Some(t), values: None }
|
|
}
|
|
|
|
pub fn both(types: T, values: T) -> PerNs<T> {
|
|
PerNs { types: Some(types), values: Some(values) }
|
|
}
|
|
|
|
pub fn is_none(&self) -> bool {
|
|
self.types.is_none() && self.values.is_none()
|
|
}
|
|
|
|
pub fn is_both(&self) -> bool {
|
|
self.types.is_some() && self.values.is_some()
|
|
}
|
|
|
|
pub fn take(self, namespace: Namespace) -> Option<T> {
|
|
match namespace {
|
|
Namespace::Types => self.types,
|
|
Namespace::Values => self.values,
|
|
}
|
|
}
|
|
|
|
pub fn take_types(self) -> Option<T> {
|
|
self.take(Namespace::Types)
|
|
}
|
|
|
|
pub fn take_values(self) -> Option<T> {
|
|
self.take(Namespace::Values)
|
|
}
|
|
|
|
pub fn get(&self, namespace: Namespace) -> Option<&T> {
|
|
self.as_ref().take(namespace)
|
|
}
|
|
|
|
pub fn as_ref(&self) -> PerNs<&T> {
|
|
PerNs { types: self.types.as_ref(), values: self.values.as_ref() }
|
|
}
|
|
|
|
pub fn or(self, other: PerNs<T>) -> PerNs<T> {
|
|
PerNs { types: self.types.or(other.types), values: self.values.or(other.values) }
|
|
}
|
|
|
|
pub fn and_then<U>(self, f: impl Fn(T) -> Option<U>) -> PerNs<U> {
|
|
PerNs { types: self.types.and_then(&f), values: self.values.and_then(&f) }
|
|
}
|
|
|
|
pub fn map<U>(self, f: impl Fn(T) -> U) -> PerNs<U> {
|
|
PerNs { types: self.types.map(&f), values: self.values.map(&f) }
|
|
}
|
|
}
|