This crate provides abstractions for creating type witnesses.
The inciting motivation for this crate is emulating trait polymorphism in const fn
(as of 2023-04-30 it's not possible to call trait methods in const contexts on stable).
What are type witnesses
Type witnesses are enums that allow coercing between a type parameter and a range of possible types (one per variant).
The simplest type witness is TypeEq<L, R>,
which only allows coercing between L and R.
Most type witnesses are enums with TypeEq fields,
which can coerce between a type parameter and as many types as there are variants.
Examples
Polymorphic function
This demonstrates how one can write a return-type-polymorphic const fn
(as of 2023-04-30, trait methods can't be called in const fns)
use ;
assert_eq!;
assert_eq!;
const
// This macro declares a type witness enum
simple_type_witness!
Indexing polymorphism
This function demonstrates const fn polymorphism
and projecting TypeEq by implementing TypeFn.
(this example requires Rust 1.61.0, because of the I: SliceIndex<T>, bound)
use Range;
use ;
const
// This macro declares a type witness enum
simple_type_witness!
/// Trait for all types that can be used as slice indices
///
/// The `HasTypeWitness` supertrait allows getting a `IndexWitness<Self>`
/// with its `WITNESS` associated constant.
type SliceIndexRet<I, T> = Returns;
// Declares `struct FnSliceIndexRet<T>`
// a type-level function (TypeFn implementor) from `I` to `SliceIndexRet<I, T>`
type_fn!
When the wrong type is passed for the index, the compile-time error is the same as with normal generic functions:
error[E0277]: the trait bound `RangeFull: SliceIndex<{integer}>` is not satisfied
--> src/main.rs:43:30
|
13 | assert_eq!(index(&array, ..), [13, 21]);
| ----- ^^ the trait `SliceIndex<{integer}>` is not implemented for `RangeFull`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `SliceIndex<T>`:
std::ops::Range<usize>
usize
Cargo features
These are the features of this crates:
-
"rust_1_61": allows thetypewitcrate to use Rust 1.61.0 features. -
"rust_stable": enables all the"rust_1_*"features. -
"alloc": enable items that use anything from the standardalloccrate. -
"const_marker"(enabled by default): Enables theconst_parammodule, and all items that depend on it. -
"mut_refs": turns functions that take mutable references into const fns. note: as of April 2023, this crate feature requires a stable compiler from the future. -
"nightly_mut_refs"(requires the nightly compiler): Enables the"mut_refs"crate feature and theconst_mut_refsnightly feature.
None of the crate features are enabled by default.
No-std support
typewit is #![no_std], it can be used anywhere Rust can be used.
You need to enable the "alloc" feature to enable items that use anything
from the standard alloc crate.
Minimum Supported Rust Version
typewit supports Rust 1.57.0.
Features that require newer versions of Rust, or the nightly compiler, need to be explicitly enabled with crate features.