-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_constructors.rs
82 lines (60 loc) · 2.5 KB
/
type_constructors.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Higher Kinded Types for [`BaseTypeWitness`]es
#![cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_65")))]
use crate::{BaseTypeWitness, TypeCmp, TypeEq, TypeNe};
use core::fmt::Debug;
/// The type constructor for a [`BaseTypeWitness`],
/// only implemented for [`TcTypeCmp`]/[`TcTypeEq`]/[`TcTypeNe`].
///
/// A type constructor is a "type" which needs to be provided
/// generic arguments to produce a concrete type.
///
/// This crate emulates type constructors for [`BaseTypeWitness`] types by using this trait,
/// concrete types are produced with the [`Type`](Self::Type) associated constant.
///
pub trait BaseTypeWitnessTc: 'static + Copy + Debug {
/// The [`BaseTypeWitness`] type that corresponds to this type constructor.
///
/// For [`TcTypeCmp`], this is [`TypeCmp`]`<L, R>`
/// <br>
/// For [`TcTypeEq`], this is [`TypeEq`]`<L, R>`
/// <br>
/// For [`TcTypeNe`], this is [`TypeNe`]`<L, R>`
///
type Type<L: ?Sized, R: ?Sized>: BaseTypeWitness<L = L, R = R, TypeCtor = Self>;
}
/// Computes a [`BaseTypeWitness`] type from a [`BaseTypeWitnessTc`]
pub type TcToBaseTypeWitness<TC, L, R> = <TC as BaseTypeWitnessTc>::Type::<L, R>;
/// Queries the [`BaseTypeWitnessTc`] of a [`BaseTypeWitness`]
pub type BaseTypeWitnessToTc<W> = <W as BaseTypeWitness>::TypeCtor;
/// Computes `W:`[`BaseTypeWitness`] with its type arguments replaced with `L` and `R`
pub type BaseTypeWitnessReparam<W, L, R> =
<BaseTypeWitnessToTc<W> as BaseTypeWitnessTc>::Type::<
L,
R,
>;
/// The type returned by `W::project::<F>()`,
/// where `W` is a [`BaseTypeWitness`]
pub type MapBaseTypeWitness<W, F> =
<BaseTypeWitnessToTc<W> as BaseTypeWitnessTc>::Type::<
crate::CallFn<F, <W as BaseTypeWitness>::L>,
crate::CallFn<F, <W as BaseTypeWitness>::R>,
>;
/////////////////////////////////////////////////////////////////////////////
/// The [*type constructor*](BaseTypeWitnessTc) for [`TypeCmp`].
#[derive(Debug, Copy, Clone)]
pub struct TcTypeCmp;
impl BaseTypeWitnessTc for TcTypeCmp {
type Type<L: ?Sized, R: ?Sized> = TypeCmp<L, R>;
}
/// The [*type constructor*](BaseTypeWitnessTc) for [`TypeEq`].
#[derive(Debug, Copy, Clone)]
pub struct TcTypeEq;
impl BaseTypeWitnessTc for TcTypeEq {
type Type<L: ?Sized, R: ?Sized> = TypeEq<L, R>;
}
/// The [*type constructor*](BaseTypeWitnessTc) for [`TypeNe`].
#[derive(Debug, Copy, Clone)]
pub struct TcTypeNe;
impl BaseTypeWitnessTc for TcTypeNe {
type Type<L: ?Sized, R: ?Sized> = TypeNe<L, R>;
}