Skip to content

Commit

Permalink
Added doc examples to new BoolWitG methods
Browse files Browse the repository at this point in the history
Fixed changelog typo
  • Loading branch information
rodrimati1992 committed Dec 3, 2024
1 parent 2e4911b commit 0971915
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Added these methods to `BoolWitG`:
- `unwrap_true`
- `unwrap_false`

Relaxed `Copy + Clone + Debug` impls of `BooleanWitG` to work for any `<B> BooleanWitG<B>`, instead of requirning `<const B: bool> BoolWitG<Bool<B>>`.
Relaxed `Copy + Clone + Debug` impls of `BooleanWitG` to work for any `<B> BooleanWitG<B>`, instead of requiring `<const B: bool> BoolWitG<Bool<B>>`.


### 1.10.1
Expand Down
78 changes: 72 additions & 6 deletions src/const_marker/boolwit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,45 +203,111 @@ pub enum BoolWitG<B> {

impl<B> BoolWitG<B> {
/// Whether `B == Bool<true>`
///
/// # Example
///
/// ```rust
/// use typewit::{const_marker::BoolWitG, TypeEq};
///
/// assert_eq!(BoolWitG::True(TypeEq::NEW).is_true(), true);
/// assert_eq!(BoolWitG::False(TypeEq::NEW).is_true(), false);
/// ```
///
pub const fn is_true(self) -> bool {
matches!(self, Self::True{..})
}

/// Whether `B == Bool<false>`
///
/// # Example
///
/// ```rust
/// use typewit::{const_marker::BoolWitG, TypeEq};
///
/// assert_eq!(BoolWitG::True(TypeEq::NEW).is_false(), false);
/// assert_eq!(BoolWitG::False(TypeEq::NEW).is_false(), true);
/// ```
///
pub const fn is_false(self) -> bool {
matches!(self, Self::False{..})
}

/// Gets a proof of `B == Bool<true>`, returns None if `B == Bool<false>`
///
/// # Example
///
/// ```rust
/// use typewit::{const_marker::{Bool, BoolWitG}, TypeEq};
///
/// assert_eq!(BoolWitG::True(TypeEq::NEW).to_true(), Some(TypeEq::new::<Bool<true>>()));
/// assert_eq!(BoolWitG::False(TypeEq::NEW).to_true(), None);
/// ```
///
pub const fn to_true(self) -> Option<TypeEq<B, Bool<true>>> {
match self {
Self::True(x) => Some(x),
_ => None
Self::False{..} => None
}
}

/// Gets a proof of `B == Bool<false>`, returns None if `B == Bool<true>`
///
/// # Example
///
/// ```rust
/// use typewit::{const_marker::{Bool, BoolWitG}, TypeEq};
///
/// assert_eq!(BoolWitG::True(TypeEq::NEW).to_false(), None);
/// assert_eq!(BoolWitG::False(TypeEq::NEW).to_false(), Some(TypeEq::new::<Bool<false>>()));
/// ```
///
pub const fn to_false(self) -> Option<TypeEq<B, Bool<false>>> {
match self {
Self::False(x) => Some(x),
_ => None
Self::True{..} => None
}
}


/// Gets a proof of `B == Bool<true>`, panics if `B == Bool<false>`
/// Gets a proof of `B == Bool<true>`.
///
/// # Panic
///
/// Panics if `B == Bool<false>`
///
/// # Example
///
/// ```rust
/// use typewit::{const_marker::{Bool, BoolWitG}, TypeEq};
///
/// assert_eq!(BoolWitG::False(TypeEq::NEW).unwrap_true(), TypeEq::new::<Bool<true>>());
/// ```
///
pub const fn unwrap_true(self) -> TypeEq<B, Bool<true>> {
match self {
Self::True(x) => x,
_ => panic!("attempted to unwrap into True on False variant")
Self::False{..} => panic!("attempted to unwrap into True on False variant")
}
}

/// Gets a proof of `B == Bool<false>`, panics if `B == Bool<true>`
/// Gets a proof of `B == Bool<false>`.
///
/// # Panic
///
/// Panics if `B == Bool<true>`
///
/// # Example
///
/// ```rust
/// use typewit::{const_marker::{Bool, BoolWitG}, TypeEq};
///
/// assert_eq!(BoolWitG::False(TypeEq::NEW).unwrap_false(), TypeEq::new::<Bool<false>>());
/// ```
///
pub const fn unwrap_false(self) -> TypeEq<B, Bool<false>> {
match self {
Self::False(x) => x,
_ => panic!("attempted to unwrap into False on True variant")
Self::True{..} => panic!("attempted to unwrap into False on True variant")
}
}

Expand Down

0 comments on commit 0971915

Please sign in to comment.