metaprogramming/
lib.rs

1// This file is part of the metaprogramming project.
2//
3// Copyright (C) 2024 Chen-Pang He <jdh8@skymizer.com>
4//
5// This Source Code Form is subject to the terms of the Mozilla
6// Public License v. 2.0. If a copy of the MPL was not distributed
7// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
8#![no_std]
9
10use core::marker::PhantomData;
11
12pub trait Truth {}
13
14pub struct BoolConstant<const VALUE: bool>;
15impl Truth for BoolConstant<true> {}
16
17pub struct Same<T, U>(PhantomData<T>, PhantomData<U>);
18
19#[allow(clippy::mismatching_type_param_order)]
20impl<T> Truth for Same<T, T> {}
21
22pub trait AssociatedType {
23    type Type;
24}
25
26pub struct Conditional<const COND: bool, T, F>(PhantomData<T>, PhantomData<F>);
27
28impl<T, F> AssociatedType for Conditional<true, T, F> {
29    type Type = T;
30}
31
32impl<T, F> AssociatedType for Conditional<false, T, F> {
33    type Type = F;
34}
35
36pub type ConditionalType<const COND: bool, T, F> = <Conditional<COND, T, F> as AssociatedType>::Type;
37
38pub struct EnableIf<const COND: bool, T>(PhantomData<T>);
39
40impl<T> AssociatedType for EnableIf<true, T> {
41    type Type = T;
42}
43
44pub type EnableIfType<const COND: bool, T> = <EnableIf<COND, T> as AssociatedType>::Type;