typeslice 0.2.0

type-level slices
Documentation

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, [prim@char] and [prim@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'];

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', 'e', 'l', 'l', 'o']) {
return "hi!"
}
if T::LIST.into_iter().copied().eq("bonjour".chars()) {
return "salut!"
}
"I didn't understand that"
}

If you enjoy this crate, you may also like typenum or frunk