Crate typeslice

Source
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");

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

Modules§

types
Types that implement TypeSlice for all primitives that can be const-generics.

Macros§

bool
Define a type-level TypeSlice of bools.
char
Define a type-level TypeSlice of chars.
from_bytesmacros
Define a type-level TypeSlice of u8s using a single bytestring literal. This can be more ergonomic than specifying each byte individually using the u8 macro.
from_strmacros
Define a type-level TypeSlice of chars using a single string literal. This can be more ergonomic than specifying each char individually using the char macro.
i8
Define a type-level TypeSlice of i8s.
i16
Define a type-level TypeSlice of i16s.
i32
Define a type-level TypeSlice of i32s.
i64
Define a type-level TypeSlice of i64s.
i128
Define a type-level TypeSlice of i128s.
isize
Define a type-level TypeSlice of isizes.
u8
Define a type-level TypeSlice of u8s.
u16
Define a type-level TypeSlice of u16s.
u32
Define a type-level TypeSlice of u32s.
u64
Define a type-level TypeSlice of u64s.
u128
Define a type-level TypeSlice of u128s.
usize
Define a type-level TypeSlice of usizes.
utf8macros
Define a type-level TypeSlice of u8s 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§

TypeSlice
A type-level slice of items.