typewit/type_fn.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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
//! Type-level functions.
//!
//! Type-level functions come in two flavors:
//! [injective](#injective), and [non-injective](#non-injective)
//!
//!
//! # Injective
//!
//! An injective function is any function `f` for which `a != b` implies `f(a) != f(b)`.
//! <br>(For both injective and non-injective functions, `f(a) != f(b)` implies `a != b`)
//!
//! The [`InjTypeFn`] trait encodes injective type-level functions,
//! requiring the type to implement both [`TypeFn`] and [`RevTypeFn`].
//!
//!
//! ### Example: injective function
//!
//! ```rust
//! # use typewit::CallInjFn;
//! #
//! typewit::inj_type_fn!{
//! struct Upcast;
//!
//! impl u8 => u16;
//! impl u16 => u32;
//! impl u32 => u64;
//! impl u64 => u128;
//! }
//! let _: CallInjFn<Upcast, u8> = 3u16;
//! let _: CallInjFn<Upcast, u16> = 5u32;
//! ```
//!
//! Because `Upcast` is injective,
//! it is possible to query the argument from the returned value:
//!
//! ```rust
//! # use typewit::UncallFn;
//! #
//! let _: UncallFn<Upcast, u16> = 3u8;
//! let _: UncallFn<Upcast, u128> = 5u64;
//! #
//! # typewit::inj_type_fn!{
//! # struct Upcast;
//! #
//! # impl u8 => u16;
//! # impl u16 => u32;
//! # impl u32 => u64;
//! # impl u64 => u128;
//! # }
//! ```
//!
//! # Non-injective
//!
//! The [`TypeFn`] trait allows implementors to be non-injective.
//!
//! ### Example: non-injective function
//!
//! ```rust
//! typewit::type_fn!{
//! struct Bar;
//!
//! impl<T> Vec<T> => T;
//! impl<T> Box<T> => T;
//! }
//! ```
//! `Bar` is *non*-injective because it maps both `Vec<T>` and `Box<T>` to `T`.
//!
//!
//! [`TypeFn`]: crate::type_fn::TypeFn
//! [`CallFn`]: crate::type_fn::CallFn
//!
use core::marker::PhantomData;
mod injective;
pub use self::injective::*;
pub(crate) use self::injective::simple_inj_type_fn;
#[doc(no_inline)]
pub use crate::inj_type_fn;
#[doc(no_inline)]
pub use crate::type_fn;
/// A function that operates purely on the level of types.
///
/// These can be used in `typewit` to
/// [map the type arguments of `TypeEq`](crate::TypeEq::project).
///
/// Type-level functions can also be declared with the
/// [`type_fn`](macro@crate::type_fn) macro.
///
/// # Properties
///
/// These are properties about `TypeFn` implementors that users can rely on.
///
/// For any given `F: TypeFn<A> + TypeFn<B>` these hold:
///
/// 1. If `A == B`, then `CallFn<F, A> == CallFn<F, B>`.
/// 2. If `CallFn<F, A> != CallFn<F, B>`, then `A != B`.
///
/// # Examples
///
/// ### Manual Implementation
///
/// ```rust
/// use typewit::{TypeFn, CallFn};
///
/// let string: CallFn<AddOutput<String>, &str> = "foo".to_string() + ", bar";
/// let _: String = string;
/// assert_eq!(string, "foo, bar");
///
///
/// struct AddOutput<Lhs>(core::marker::PhantomData<Lhs>);
///
/// // This part is optional,
/// // only necessary to pass the function as a value, not just as a type.
/// impl<Lhs> AddOutput<Lhs> {
/// const NEW: Self = Self(core::marker::PhantomData);
/// }
///
/// impl<Lhs, Rhs> TypeFn<Rhs> for AddOutput<Lhs>
/// where
/// Lhs: core::ops::Add<Rhs>
/// {
/// type Output = Lhs::Output;
/// }
/// ```
///
/// ### Macro-based Implementation
///
/// This example uses the [`type_fn`](macro@crate::type_fn) macro
/// to declare the type-level function,
/// and is otherwise equivalent to the manual one.
///
/// ```rust
/// use typewit::CallFn;
///
/// let string: CallFn<AddOutput<String>, &str> = "foo".to_string() + ", bar";
/// let _: String = string;
/// assert_eq!(string, "foo, bar");
///
/// typewit::type_fn! {
/// struct AddOutput<Lhs>;
///
/// impl<Rhs> Rhs => Lhs::Output
/// where Lhs: core::ops::Add<Rhs>
/// }
/// ```
///
pub trait TypeFn<T: ?Sized> {
/// The return value of the function
type Output: ?Sized;
/// Helper constant for adding asserts in the `TypeFn` impl;
const TYPE_FN_ASSERTS: () = ();
}
/// Calls the `F` [type-level function](TypeFn) with `T` as its argument.
///
/// For `F:`[`InjTypeFn<T>`](crate::InjTypeFn), it's better to
/// use [`CallInjFn`] instead of this type alias.
///
///
/// # Example
///
/// ```rust
/// use typewit::CallFn;
/// use core::ops::Mul;
///
/// assert_eq!(mul(3u8, &5u8), 15u8);
///
/// fn mul<L, R>(l: L, r: R) -> CallFn<MulOutput<L>, R>
/// where
/// L: core::ops::Mul<R>
/// {
/// l * r
/// }
///
/// // Declares `struct MulOutput<Lhs>`,
/// // a type-level function from `Rhs` to the return type of `Lhs * Rhs`.
/// typewit::type_fn! {
/// struct MulOutput<Lhs>;
///
/// impl<Rhs> Rhs => <Lhs as Mul<Rhs>>::Output
/// where Lhs: core::ops::Mul<Rhs>
/// }
/// ```
///
pub type CallFn<F, T> = <F as TypeFn<T>>::Output;
///////////////////////////////////////////////////////
/// Type-level function from `T` to `&'a T`
pub struct GRef<'a>(PhantomData<fn() -> &'a ()>);
impl<'a> GRef<'a> {
/// Make a value of this type-level function
pub const NEW: Self = Self(PhantomData);
}
simple_inj_type_fn!{
impl['a, T: 'a + ?Sized] (T => &'a T) for GRef<'a>
}
////////////////
/// Type-level function from `T` to `&'a mut T`
pub struct GRefMut<'a>(PhantomData<fn() -> &'a mut ()>);
impl<'a> GRefMut<'a> {
/// Make a value of this type-level function
pub const NEW: Self = Self(PhantomData);
}
simple_inj_type_fn!{
impl['a, T: 'a + ?Sized] (T => &'a mut T) for GRefMut<'a>
}
////////////////
/// Type-level function from `T` to `Box<T>`
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
pub struct GBox;
#[cfg(feature = "alloc")]
simple_inj_type_fn!{
impl[T: ?Sized] (T => alloc::boxed::Box<T>) for GBox
}
////////////////
/// Type-level identity function
pub struct FnIdentity;
simple_inj_type_fn!{
impl[T: ?Sized] (T => T) for FnIdentity
}
////////////////
/// Type-level function which implements `TypeFn` by delegating to `F`
///
/// This is mostly a workaround to write `F: TypeFn<T>` bounds in Rust 1.57.0
/// (trait bounds in `const fn`s were stabilized in Rust 1.61.0).
///
/// Because `Foo<F>: Trait`-style bounds unintentionally work in 1.57.0,
/// this crate uses `Invoke<F>: TypeFn<T>`
/// when the `"rust_1_61"` feature is disabled,
/// and `F: TypeFn<T>` when it is enabled.
///
pub struct Invoke<F>(PhantomData<fn() -> F>);
impl<F> Copy for Invoke<F> {}
impl<F> Clone for Invoke<F> {
fn clone(&self) -> Self {
*self
}
}
impl<F> Invoke<F> {
/// Constructs an `Invoke`
pub const NEW: Self = Self(PhantomData);
}
impl<F, T: ?Sized> TypeFn<T> for Invoke<F>
where
F: TypeFn<T>
{
type Output = CallFn<F, T>;
}
impl<F, R: ?Sized> RevTypeFn<R> for Invoke<F>
where
F: RevTypeFn<R>,
{
type Arg = UncallFn<F, R>;
}
////////////////////////////////////////////////////////////////////////////////
impl<F, T: ?Sized> TypeFn<T> for PhantomData<F>
where
F: TypeFn<T>
{
type Output = CallFn<F, T>;
}
impl<F, R: ?Sized> RevTypeFn<R> for PhantomData<F>
where
F: RevTypeFn<R>,
{
type Arg = UncallFn<F, R>;
}
////////////////////////////////////////////////////////////////////////////////
mod uses_const_marker {
use crate::const_marker::Usize;
/// TypeFn from `(T, Usize<N>)` to `[T; N]`
pub(crate) struct PairToArrayFn;
super::simple_inj_type_fn!{
impl[T, const N: usize] ((T, Usize<N>) => [T; N]) for PairToArrayFn
}
}
pub(crate) use uses_const_marker::*;
// This type alias makes it so that docs for newer Rust versions don't
// show `Invoke<F>`, keeping the method bounds the same as in 1.0.0.
#[cfg(not(feature = "rust_1_61"))]
pub(crate) type InvokeAlias<F> = Invoke<F>;
#[cfg(feature = "rust_1_61")]
pub(crate) type InvokeAlias<F> = F;