Expand description
Type-level slices of primitives.
Rust permits certain constant parameters in generics:
struct Foo<const CHAR: char>;Presently these are limited to the primitive integers, char and bool,
so e.g slices of different chars cannot be represented.
ⓘ
struct Fails<const CHARS: [char]>;
type Message = Fails<['h', 'e', 'l', 'l', 'o']>;This crate emulates the above with recursive types,
and the TypeSlice trait.
type Message = typeslice::char!['h', 'e', 'l', 'l', 'o'];
// or, equivalently
type Message = typeslice::from_str!("hello");You can inspect the message at const time or runtime through the List
in TypeSlice::LIST:
use typeslice::TypeSlice;
fn get_reply<T: TypeSlice<char>>() -> &'static str {
if T::LIST.slice_eq(&['h', 'i']) {
return "hello"
}
if T::LIST.str_eq("👋👋") {
return "😎😎"
}
if T::LIST.into_iter().copied().eq("salut".chars()) {
return "bonjour"
}
"¿que?"
}
assert_eq!(get_reply::<typeslice::from_str!("hi")>(), "hello");Modules§
Macros§
- bool
- Define a type-level
TypeSliceofbools. - char
- Define a type-level
TypeSliceofchars. - from_
bytes macros - Define a type-level
TypeSliceofu8s using a single bytestring literal. This can be more ergonomic than specifying each byte individually using theu8macro. - from_
str macros - Define a type-level
TypeSliceofchars using a single string literal. This can be more ergonomic than specifying each char individually using thecharmacro. - i8
- Define a type-level
TypeSliceofi8s. - i16
- Define a type-level
TypeSliceofi16s. - i32
- Define a type-level
TypeSliceofi32s. - i64
- Define a type-level
TypeSliceofi64s. - i128
- Define a type-level
TypeSliceofi128s. - isize
- Define a type-level
TypeSliceofisizes. - u8
- Define a type-level
TypeSliceofu8s. - u16
- Define a type-level
TypeSliceofu16s. - u32
- Define a type-level
TypeSliceofu32s. - u64
- Define a type-level
TypeSliceofu64s. - u128
- Define a type-level
TypeSliceofu128s. - usize
- Define a type-level
TypeSliceofusizes. - utf8
macros - Define a type-level
TypeSliceofu8s using a single string literal, encoding it in utf8
Structs§
- Iter
- Iterator over the elements in a list.
See
List::iter.
Enums§
- List
- The bridge between a
TypeSliceand runtime logic, allowing access to elements defined at the type level.
Traits§
- Type
Slice - A type-level slice of items.