zfc

Library for sets according to ZFC.
git clone https://git.philomathiclife.com/repos/zfc
Log | Files | Refs | README

commit 55d897303bea205e2d78b47743cf62ede692f7f1
parent 93f87d9457f5eee1b2dd318804db59ecba7a64b6
Author: Zack Newman <zack@philomathiclife.com>
Date:   Tue,  5 Sep 2023 16:29:39 -0600

made proper err conversion types. added exact functions to boundedcardinality

Diffstat:
MCargo.toml | 2+-
Msrc/lib.rs | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 81 insertions(+), 5 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -9,7 +9,7 @@ license = "MIT OR Apache-2.0" name = "zfc" readme = "README.md" repository = "https://git.philomathiclife.com/repos/zfc/" -version = "0.2.0" +version = "0.3.0" [lib] name = "zfc" diff --git a/src/lib.rs b/src/lib.rs @@ -21,6 +21,7 @@ #![allow( clippy::arithmetic_side_effects, clippy::blanket_clippy_restriction_lints, + clippy::exhaustive_structs, clippy::implicit_return, clippy::missing_trait_methods, clippy::ref_patterns, @@ -158,6 +159,17 @@ impl From<BigUint> for Cardinality { Self::from_biguint(value) } } +impl TryFrom<BoundedCardinality> for Cardinality { + type Error = CardinalityErr; + #[inline] + fn try_from(value: BoundedCardinality) -> Result<Self, Self::Error> { + if value.lower == value.upper { + Ok(value.lower) + } else { + Err(CardinalityErr) + } + } +} impl From<Cardinality> for BigUint { #[inline] fn from(value: Cardinality) -> Self { @@ -271,6 +283,31 @@ impl PartialOrd<BigUint> for Cardinality { Some(self.cmp_biguint(other)) } } +/// Error returned when attempting to create a +/// [`BoundedCardinality`] from a pair of [`BigUint`]s +/// or [`Cardinality`]s such that the upper bound +/// is strictly less than the lower bound. +#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct BoundedErr; +impl Display for BoundedErr { + #[allow(clippy::min_ident_chars)] + #[inline] + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.write_str("upper bound is greater than lower bound") + } +} +/// Error returned when attempting to create a +/// [`Cardinality`] from a [`BoundedCardinality`] such that +/// the lower bound is less than the upper bound. +#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct CardinalityErr; +impl Display for CardinalityErr { + #[allow(clippy::min_ident_chars)] + #[inline] + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.write_str("lower bound of the BoundedCardinality is less than the upper bound") + } +} /// Contains a lower and upper bound /// [`Cardinality`] used to bound the cardinality /// of a [`Set`]. @@ -293,6 +330,16 @@ impl BoundedCardinality { Some(Self { lower, upper }) } } + /// Creates an instance of `BoundedCardinality` with the lower and upper bounds + /// both set to `exact`. + #[inline] + #[must_use] + pub fn new_exact(exact: Cardinality) -> Self { + Self { + lower: exact.clone(), + upper: exact, + } + } /// Creates an instance of `BoundedCardinality` /// with the lower and upper bounds as `Cardinality::Finite` /// of their respective values. @@ -310,6 +357,16 @@ impl BoundedCardinality { } } /// Creates an instance of `BoundedCardinality` + /// with the lower and upper bounds as `Cardinality::Finite(exact)`. + #[inline] + #[must_use] + pub fn from_biguint_exact(exact: BigUint) -> Self { + Self { + lower: Cardinality::Finite(exact.clone()), + upper: Cardinality::Finite(exact), + } + } + /// Creates an instance of `BoundedCardinality` /// without verifying `lower <= upper`. /// /// # Safety @@ -412,18 +469,33 @@ impl fmt::Debug for BoundedCardinality { <Self as Display>::fmt(self, f) } } +impl From<BigUint> for BoundedCardinality { + #[inline] + fn from(value: BigUint) -> Self { + Self::from_biguint_exact(value) + } +} +impl From<Cardinality> for BoundedCardinality { + #[inline] + fn from(value: Cardinality) -> Self { + Self { + lower: value.clone(), + upper: value, + } + } +} impl TryFrom<(Cardinality, Cardinality)> for BoundedCardinality { - type Error = (); + type Error = BoundedErr; #[inline] fn try_from(value: (Cardinality, Cardinality)) -> Result<Self, Self::Error> { - Self::new(value.0, value.1).ok_or(()) + Self::new(value.0, value.1).ok_or(BoundedErr) } } impl TryFrom<(BigUint, BigUint)> for BoundedCardinality { - type Error = (); + type Error = BoundedErr; #[inline] fn try_from(value: (BigUint, BigUint)) -> Result<Self, Self::Error> { - Self::from_biguint(value.0, value.1).ok_or(()) + Self::from_biguint(value.0, value.1).ok_or(BoundedErr) } } impl From<BoundedCardinality> for (Cardinality, Cardinality) { @@ -723,3 +795,7 @@ where self.len() <= val.len() && self.intersection(val).count() == val.len() } } +#[cfg(feature = "std")] +impl std::error::Error for BoundedErr {} +#[cfg(feature = "std")] +impl std::error::Error for CardinalityErr {}