use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Property {
Convex,
Bankruptcy,
}
impl fmt::Display for Property {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Property::Convex => "convex",
Property::Bankruptcy => "bankruptcy",
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Guarantee {
Exact,
Proven(Property),
Assumed(Property),
Certified,
Approximate,
}
impl Guarantee {
pub fn is_reliable(self) -> bool {
matches!(
self,
Guarantee::Exact | Guarantee::Proven(_) | Guarantee::Certified
)
}
}
impl fmt::Display for Guarantee {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Guarantee::Exact => f.write_str("exact"),
Guarantee::Proven(property) => write!(f, "proven({property})"),
Guarantee::Assumed(property) => write!(f, "assumed({property})"),
Guarantee::Certified => f.write_str("certified"),
Guarantee::Approximate => f.write_str("approximate"),
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[must_use = "仮定付きの結果は、検証するか accept_unverified で明示的に受け入れる"]
pub struct Unverified<T> {
value: T,
property: Property,
}
impl<T> Unverified<T> {
pub(crate) fn new(value: T, property: Property) -> Unverified<T> {
Unverified { value, property }
}
pub fn property(&self) -> Property {
self.property
}
pub fn peek(&self) -> &T {
&self.value
}
pub fn accept_unverified(self) -> T {
self.value
}
}