perspective_viewer/utils/
tee.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13/// Trait for polymorphic return value via turbofish-const-generic syntax.
14///
15/// The associated type `Output` is a functional dependency of the const generic
16/// parameter `N`, so there's typically no need to specify this parameter when
17/// invoking, which is what we want - this parameter is quite verbose and
18/// repetitive (see below), vs `N` which is a single character.
19pub trait TeeInternal<const N: usize> {
20    type Output: Clone;
21    fn tee_internal(self) -> Self::Output;
22}
23
24pub trait Tee {
25    /// Extension method to add `tee()` method to all `T: Clone`.
26    ///
27    /// This can't be done directly as part of `TeeInternal` because it makes
28    /// specifying the const param at the `tee()` invocation site
29    /// cumbersome: `TeeInternal::<N>::tee(&obj)` as opposed to
30    /// `obj.tee::<N>()`. The constraint `Self: TeeInternal<N>` collapses
31    /// the potential `impl` matches to exactly 1, which makes the call to
32    /// `tee_internal()` unambiguous. This constraint is also allowed to
33    /// contain the generic parameter `N` because it is specified as a
34    /// constraint to the method (as opposed to a constraint on the trait).
35    /// I'm honestly quite surprised this works ...
36    ///
37    /// # Examples
38    ///
39    /// ```
40    /// let x = "test".tee::<2>();
41    /// assert_eq!(x.0, x.1);
42    /// assert_eq!(x.0, "test");
43    /// ```
44    fn tee<const N: usize>(self) -> <Self as TeeInternal<N>>::Output
45    where
46        Self: TeeInternal<N>;
47}
48
49impl<T: Clone> Tee for T {
50    fn tee<const N: usize>(self) -> <Self as TeeInternal<N>>::Output
51    where
52        Self: TeeInternal<N>,
53    {
54        self.tee_internal()
55    }
56}
57
58macro_rules! gen_tee {
59    ($($x:ty),*) => {
60        impl<T: Clone> TeeInternal<{${count($x)} + 1}> for T {
61            type Output = ($($x),*, T);
62            fn tee_internal(self) -> Self::Output {
63                ($( ${ignore($x)} self.clone() ),*, self)
64            }
65        }
66    };
67}
68
69gen_tee!(T);
70gen_tee!(T, T);
71gen_tee!(T, T, T);
72gen_tee!(T, T, T, T);
73gen_tee!(T, T, T, T, T);
74gen_tee!(T, T, T, T, T, T);
75gen_tee!(T, T, T, T, T, T, T);
76gen_tee!(T, T, T, T, T, T, T, T);
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn test_test1() {
84        let x = "test".tee::<2>();
85        assert_eq!(x.0, x.1);
86        assert_eq!(x.0, "test");
87    }
88}