Skip to content

Commit

Permalink
Bumped version to 1.11.0, Added these methods to BoolWitG:
Browse files Browse the repository at this point in the history
- is_true
- is_false
- to_true
- to_false
- unwrap_true
- unwrap_false

Added tests for these methods

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

Updated what non-proc_macro UI tests output

Updated changelog
  • Loading branch information
rodrimati1992 committed Dec 3, 2024
1 parent 82fd8a2 commit 2e4911b
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 59 deletions.
11 changes: 1 addition & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
[package]
name = "typewit"
<<<<<<< HEAD
version = "1.10.0"
=======
version = "1.10.1"
>>>>>>> main
version = "1.11.0"
authors = ["rodrimati1992 <[email protected]>"]
rust-version = "1.57.0"
edition = "2021"
Expand Down Expand Up @@ -51,12 +47,7 @@ alloc = []
mut_refs = ["rust_stable"]
nightly_mut_refs = ["mut_refs"]
docsrs = []
<<<<<<< HEAD

__ui_tests = []
=======
__ui_tests = ["trybuild"]
>>>>>>> main

[package.metadata.docs.rs]
features = ["alloc", "rust_stable", "adt_const_marker", "docsrs"]
Expand Down
9 changes: 9 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ This is the changelog, summarising changes in each version(some minor changes ma

Added `"rust_1_83"` feature, which turns `typewit` functions that use `&mut` into const fns.

Added these methods to `BoolWitG`:
- `is_true`
- `is_false`
- `to_true`
- `to_false`
- `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>>`.


### 1.10.1
Expand Down
52 changes: 49 additions & 3 deletions src/const_marker/boolwit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,61 @@ pub enum BoolWitG<B> {
False(TypeEq<B, Bool<false>>),
}

impl<const B: bool> Copy for BoolWitG<Bool<B>> {}
impl<B> BoolWitG<B> {
/// Whether `B == Bool<true>`
pub const fn is_true(self) -> bool {
matches!(self, Self::True{..})
}

/// Whether `B == Bool<false>`
pub const fn is_false(self) -> bool {
matches!(self, Self::False{..})
}

/// Gets a proof of `B == Bool<true>`, returns None if `B == Bool<false>`
pub const fn to_true(self) -> Option<TypeEq<B, Bool<true>>> {
match self {
Self::True(x) => Some(x),
_ => None
}
}

/// Gets a proof of `B == Bool<false>`, returns None if `B == Bool<true>`
pub const fn to_false(self) -> Option<TypeEq<B, Bool<false>>> {
match self {
Self::False(x) => Some(x),
_ => None
}
}


/// Gets a proof of `B == Bool<true>`, panics if `B == Bool<false>`
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")
}
}

/// Gets a proof of `B == Bool<false>`, panics if `B == Bool<true>`
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")
}
}

}

impl<B> Copy for BoolWitG<B> {}

impl<const B: bool> Clone for BoolWitG<Bool<B>> {
impl<B> Clone for BoolWitG<B> {
fn clone(&self) -> Self {
*self
}
}

impl<const B: bool> Debug for BoolWitG<Bool<B>> {
impl<B> Debug for BoolWitG<B> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match self {
Self::True{..} => "True",
Expand Down
2 changes: 2 additions & 0 deletions tests/misc_tests/const_marker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use typewit::{
#[cfg(feature = "adt_const_marker")]
mod slice_const_marker_tests;

mod boolwit_tests;


#[test]
fn test_integer_const_marker() {
Expand Down
80 changes: 80 additions & 0 deletions tests/misc_tests/const_marker_tests/boolwit_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use typewit::const_marker::{Bool, BoolWitG};
use typewit::TypeEq;

#[test]
fn is_true_test() {
const fn inner<B>(wit: BoolWitG<B>) -> bool {
wit.is_true()
}

assert!(inner(BoolWitG::True(TypeEq::NEW)));
assert!(!inner(BoolWitG::False(TypeEq::NEW)));
}

#[test]
fn is_false_test() {
const fn inner<B>(wit: BoolWitG<B>) -> bool {
wit.is_false()
}

assert!(!inner(BoolWitG::True(TypeEq::NEW)));
assert!(inner(BoolWitG::False(TypeEq::NEW)));
}

#[test]
fn to_true_test() {
const fn inner<B>(wit: BoolWitG<B>) -> Option<TypeEq<B, Bool<true>>> {
wit.to_true()
}

assert_eq!(inner(BoolWitG::True(TypeEq::NEW)), Some(TypeEq::new::<Bool<true>>()));
assert_eq!(inner(BoolWitG::False(TypeEq::NEW)), None);
}

#[test]
fn to_false_test() {
const fn inner<B>(wit: BoolWitG<B>) -> Option<TypeEq<B, Bool<false>>> {
wit.to_false()
}

assert_eq!(inner(BoolWitG::True(TypeEq::NEW)), None);
assert_eq!(inner(BoolWitG::False(TypeEq::NEW)), Some(TypeEq::new::<Bool<false>>()));
}

#[test]
fn unwrap_true_test() {
const fn inner<B>(wit: BoolWitG<B>) -> TypeEq<B, Bool<true>> {
wit.unwrap_true()
}

assert_eq!(inner(BoolWitG::True(TypeEq::NEW)), TypeEq::new::<Bool<true>>());
}

#[test]
#[should_panic]
fn unwrap_true_on_false_test() {
const fn inner<B>(wit: BoolWitG<B>) -> TypeEq<B, Bool<true>> {
wit.unwrap_true()
}

let _ = inner(BoolWitG::False(TypeEq::NEW));
}

#[test]
fn unwrap_false_test() {
const fn inner<B>(wit: BoolWitG<B>) -> TypeEq<B, Bool<false>> {
wit.unwrap_false()
}

assert_eq!(inner(BoolWitG::False(TypeEq::NEW)), TypeEq::new::<Bool<false>>());
}

#[test]
#[should_panic]
fn unwrap_false_on_true_test() {
const fn inner<B>(wit: BoolWitG<B>) -> TypeEq<B, Bool<false>> {
wit.unwrap_false()
}

let _ = inner(BoolWitG::True(TypeEq::NEW));
}
13 changes: 13 additions & 0 deletions tests/misc_tests/misc_ui_tests/polymatch_semantic-err.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,16 @@ error[E0592]: duplicate definitions with name `foo`
| |
| duplicate definitions for `foo`
| other definition for `foo`

error[E0004]: non-exhaustive patterns: `i32::MIN..=199_i32`, `201_i32..=299_i32` and `301_i32..=i32::MAX` not covered
--> tests/misc_tests/misc_ui_tests/polymatch_semantic-err.rs:4:26
|
4 | typewit::polymatch! {100;
| ^^^ patterns `i32::MIN..=199_i32`, `201_i32..=299_i32` and `301_i32..=i32::MAX` not covered
|
= note: the matched value is of type `i32`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
--> src/macros/polymatch.rs
|
| (($($pattern, i32::MIN..=199_i32 | 201_i32..=299_i32 | 301_i32..=i32::MAX => todo!())+) => $expr)
| ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 changes: 1 addition & 1 deletion tests/misc_tests/misc_ui_tests/polymatch_syntax-err.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ error: expected arguments to be `matched expression; match arms`
|
= note: this error originates in the macro `typewit::polymatch` (in Nightly builds, run with -Z macro-backtrace for more info)

error: no rules expected the token `500`
error: no rules expected `500`
--> tests/misc_tests/misc_ui_tests/polymatch_syntax-err.rs:7:9
|
6 | 200 | 300 => 400
Expand Down
1 change: 1 addition & 0 deletions tests/misc_tests/simple_type_witness_macro_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(unused_lifetimes)]
#![allow(non_local_definitions)]

#[cfg(feature = "rust_1_61")]
use crate::misc_tests::test_utils::assert_type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ error[E0308]: mismatched types
|
= note: expected struct `TypeEq<_, (dyn Debug + 'static)>`
found struct `TypeEq<_, u8>`
= help: `u8` implements `Debug` so you could box the found value and coerce it to the trait object `Box<dyn Debug>`, you will have to change the expected type as well

error[E0308]: mismatched types
--> tests/misc_tests/type_eq_ui_tests/constructor_type_errors-err.rs:18:5
Expand All @@ -50,6 +51,7 @@ error[E0308]: mismatched types
|
= note: expected struct `TypeEq<(dyn Debug + 'static), _>`
found struct `TypeEq<u8, _>`
= help: `u8` implements `Debug` so you could box the found value and coerce it to the trait object `Box<dyn Debug>`, you will have to change the expected type as well

error[E0308]: mismatched types
--> tests/misc_tests/type_eq_ui_tests/constructor_type_errors-err.rs:22:5
Expand All @@ -61,3 +63,4 @@ error[E0308]: mismatched types
|
= note: expected struct `TypeEq<(dyn Debug + 'static), (dyn Debug + 'static)>`
found struct `TypeEq<u8, u8>`
= help: `u8` implements `Debug` so you could box the found value and coerce it to the trait object `Box<dyn Debug>`, you will have to change the expected type as well
5 changes: 0 additions & 5 deletions tests/misc_tests/type_eq_ui_tests/projection_type-err.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ error[E0308]: mismatched types
found struct `TypeEq<R, L>`
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
help: try removing the method call
|
4 - te.flip()
4 + te
|

error[E0308]: mismatched types
--> tests/misc_tests/type_eq_ui_tests/projection_type-err.rs:8:16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ error[E0308]: mismatched types
found struct `TypeNe<R, L>`
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
help: try removing the method call
|
15 - te.flip()
15 + te
|

error[E0308]: mismatched types
--> tests/misc_tests/type_eq_ui_tests/projection_type_ne-err.rs:19:5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ error[E0277]: the trait bound `Mapper: RevTypeFn<[L; 1]>` is not satisfied
| required by a bound introduced by this call
|
= help: the following other types implement trait `RevTypeFn<Ret>`:
<FnRev<F> as RevTypeFn<R>>
<GRef<'a> as RevTypeFn<&'a T>>
<GRefMut<'a> as RevTypeFn<&'a mut T>>
<GBox as RevTypeFn<Box<T>>>
<FnIdentity as RevTypeFn<T>>
<Invoke<F> as RevTypeFn<R>>
<PhantomData<F> as RevTypeFn<R>>
`FnIdentity` implements `RevTypeFn<T>`
`FnRev<F>` implements `RevTypeFn<R>`
`GBox` implements `RevTypeFn<Box<T>>`
`GRef<'a>` implements `RevTypeFn<&'a T>`
`GRefMut<'a>` implements `RevTypeFn<&'a mut T>`
`Invoke<F>` implements `RevTypeFn<R>`
`PhantomData<F>` implements `RevTypeFn<R>`
= note: required for `Mapper` to implement `InjTypeFn<L>`
note: required by a bound in `typewit::type_ne_::<impl TypeNe<L, R>>::map`
--> src/./type_ne/extra_type_ne_methods.rs
Expand All @@ -33,13 +33,13 @@ error[E0277]: the trait bound `Mapper: RevTypeFn<[R; 1]>` is not satisfied
| required by a bound introduced by this call
|
= help: the following other types implement trait `RevTypeFn<Ret>`:
<FnRev<F> as RevTypeFn<R>>
<GRef<'a> as RevTypeFn<&'a T>>
<GRefMut<'a> as RevTypeFn<&'a mut T>>
<GBox as RevTypeFn<Box<T>>>
<FnIdentity as RevTypeFn<T>>
<Invoke<F> as RevTypeFn<R>>
<PhantomData<F> as RevTypeFn<R>>
`FnIdentity` implements `RevTypeFn<T>`
`FnRev<F>` implements `RevTypeFn<R>`
`GBox` implements `RevTypeFn<Box<T>>`
`GRef<'a>` implements `RevTypeFn<&'a T>`
`GRefMut<'a>` implements `RevTypeFn<&'a mut T>`
`Invoke<F>` implements `RevTypeFn<R>`
`PhantomData<F>` implements `RevTypeFn<R>`
= note: required for `Mapper` to implement `InjTypeFn<R>`
note: required by a bound in `typewit::type_ne_::<impl TypeNe<L, R>>::map`
--> src/./type_ne/extra_type_ne_methods.rs
Expand All @@ -59,13 +59,13 @@ error[E0277]: the trait bound `Mapper: RevTypeFn<[L; 1]>` is not satisfied
| required by a bound introduced by this call
|
= help: the following other types implement trait `RevTypeFn<Ret>`:
<FnRev<F> as RevTypeFn<R>>
<GRef<'a> as RevTypeFn<&'a T>>
<GRefMut<'a> as RevTypeFn<&'a mut T>>
<GBox as RevTypeFn<Box<T>>>
<FnIdentity as RevTypeFn<T>>
<Invoke<F> as RevTypeFn<R>>
<PhantomData<F> as RevTypeFn<R>>
`FnIdentity` implements `RevTypeFn<T>`
`FnRev<F>` implements `RevTypeFn<R>`
`GBox` implements `RevTypeFn<Box<T>>`
`GRef<'a>` implements `RevTypeFn<&'a T>`
`GRefMut<'a>` implements `RevTypeFn<&'a mut T>`
`Invoke<F>` implements `RevTypeFn<R>`
`PhantomData<F>` implements `RevTypeFn<R>`
= note: required for `Mapper` to implement `InjTypeFn<L>`
note: required by a bound in `typewit::type_ne_::<impl TypeNe<L, R>>::project`
--> src/./type_ne/extra_type_ne_methods.rs
Expand All @@ -85,13 +85,13 @@ error[E0277]: the trait bound `Mapper: RevTypeFn<[R; 1]>` is not satisfied
| required by a bound introduced by this call
|
= help: the following other types implement trait `RevTypeFn<Ret>`:
<FnRev<F> as RevTypeFn<R>>
<GRef<'a> as RevTypeFn<&'a T>>
<GRefMut<'a> as RevTypeFn<&'a mut T>>
<GBox as RevTypeFn<Box<T>>>
<FnIdentity as RevTypeFn<T>>
<Invoke<F> as RevTypeFn<R>>
<PhantomData<F> as RevTypeFn<R>>
`FnIdentity` implements `RevTypeFn<T>`
`FnRev<F>` implements `RevTypeFn<R>`
`GBox` implements `RevTypeFn<Box<T>>`
`GRef<'a>` implements `RevTypeFn<&'a T>`
`GRefMut<'a>` implements `RevTypeFn<&'a mut T>`
`Invoke<F>` implements `RevTypeFn<R>`
`PhantomData<F>` implements `RevTypeFn<R>`
= note: required for `Mapper` to implement `InjTypeFn<R>`
note: required by a bound in `typewit::type_ne_::<impl TypeNe<L, R>>::project`
--> src/./type_ne/extra_type_ne_methods.rs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,27 @@ error[E0119]: conflicting implementations of trait `TypeFn<u8>` for type `Foo`
| conflicting implementation for `Foo`
|
= note: this error originates in the macro `$crate::__impl_with_span` which comes from the expansion of the macro `typewit::inj_type_fn` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0283]: type annotations needed
--> tests/misc_tests/type_fn_ui_tests/inj_type_fn_conflicting_arguments-err.rs:1:1
|
1 | / typewit::inj_type_fn!{
2 | | struct Foo;
3 | |
4 | | impl u8 => u16;
5 | | impl<T> T => Vec<T>;
6 | | }
| |_^ cannot infer type
|
note: multiple `impl`s satisfying `Foo: RevTypeFn<_>` found
--> tests/misc_tests/type_fn_ui_tests/inj_type_fn_conflicting_arguments-err.rs:1:1
|
1 | / typewit::inj_type_fn!{
2 | | struct Foo;
3 | |
4 | | impl u8 => u16;
5 | | impl<T> T => Vec<T>;
6 | | }
| |_^
= note: required for `Foo` to implement `InjTypeFn<u8>`
= note: this error originates in the macro `$crate::__tyfn_injtypefn_impl` which comes from the expansion of the macro `typewit::inj_type_fn` (in Nightly builds, run with -Z macro-backtrace for more info)
Loading

0 comments on commit 2e4911b

Please sign in to comment.