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
TypeSlice
ofbool
s. - char
- Define a type-level
TypeSlice
ofchar
s. - from_
bytes macros
- Define a type-level
TypeSlice
ofu8
s using a single bytestring literal. This can be more ergonomic than specifying each byte individually using theu8
macro. - from_
str macros
- Define a type-level
TypeSlice
ofchar
s using a single string literal. This can be more ergonomic than specifying each char individually using thechar
macro. - i8
- Define a type-level
TypeSlice
ofi8
s. - i16
- Define a type-level
TypeSlice
ofi16
s. - i32
- Define a type-level
TypeSlice
ofi32
s. - i64
- Define a type-level
TypeSlice
ofi64
s. - i128
- Define a type-level
TypeSlice
ofi128
s. - isize
- Define a type-level
TypeSlice
ofisize
s. - u8
- Define a type-level
TypeSlice
ofu8
s. - u16
- Define a type-level
TypeSlice
ofu16
s. - u32
- Define a type-level
TypeSlice
ofu32
s. - u64
- Define a type-level
TypeSlice
ofu64
s. - u128
- Define a type-level
TypeSlice
ofu128
s. - usize
- Define a type-level
TypeSlice
ofusize
s. - utf8
macros
- Define a type-level
TypeSlice
ofu8
s 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
TypeSlice
and runtime logic, allowing access to elements defined at the type level.
Traits§
- Type
Slice - A type-level slice of items.