gazebo/types.rs
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under both the MIT license found in the
5 * LICENSE-MIT file in the root directory of this source tree and the Apache
6 * License, Version 2.0 found in the LICENSE-APACHE file in the root directory
7 * of this source tree.
8 */
9
10//! Operations working on Rust types.
11
12// If we need more type operations we should probably model them on
13// https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Type-Equality.html
14
15/// A trait witnessing that two types are equal.
16///
17/// For example:
18///
19/// ```
20/// use gazebo::types::TEq;
21/// fn foo<A : TEq<String>>(x: A) -> String {
22/// x.teq()
23/// }
24/// ```
25///
26/// You should not make any further implementations of this trait.
27///
28/// Originally taken from a request to have Rust allow equality constraints in
29/// traits, from [Issue 20041](https://github.com/rust-lang/rust/issues/20041#issuecomment-414551783).
30pub trait TEq<T> {
31 /// Convert between two equal types.
32 fn teq(self) -> T;
33 /// Convert between references to two equal types.
34 fn teq_ref(&self) -> &T;
35 /// Convert between mutable references to two equal types.
36 fn teq_mut(&mut self) -> &mut T;
37}
38
39impl<T> TEq<T> for T {
40 fn teq(self) -> Self {
41 self
42 }
43 fn teq_ref(&self) -> &Self {
44 self
45 }
46 fn teq_mut(&mut self) -> &mut T {
47 self
48 }
49}