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
use std::marker::PhantomData;

use crate::{
    assertable::Assertable, maybe_borrowed::MaybeBorrowed, raw_assert::r#trait::RawAssertable,
};

/// This Trait represents something that is assertable with some additional data. See [`Assertable`] for general details.
pub trait AssertableWithBounds<'a, T> {
    /// The output of the assertion
    type Output: RawAssertable<'a>;

    /// Do the assertion with the given bounds
    fn do_assert(&self, bounds: T) -> Self::Output;
}

impl<'a, T, U> AssertableWithBounds<'a, T> for &'a U
where
    U: AssertableWithBounds<'a, T>,
{
    type Output = U::Output;

    fn do_assert(&self, bounds: T) -> Self::Output {
        (*self).do_assert(bounds)
    }
}

/// This struct represents something that is assertable with some additional data, where that data was provided.
pub struct ResolvedBounds<'a, T, U>
where
    U: AssertableWithBounds<'a, T>,
{
    bounds: T,
    assertable: MaybeBorrowed<'a, U>,
    _lt: PhantomData<&'a ()>,
}

impl<'a, T, U> ResolvedBounds<'a, T, U>
where
    U: AssertableWithBounds<'a, T>,
{
    fn new(bounds: T, assertable: MaybeBorrowed<'a, U>) -> Self {
        Self {
            bounds,
            assertable,
            _lt: PhantomData,
        }
    }
}

impl<'a, T, U> Assertable<'a> for ResolvedBounds<'a, T, U>
where
    U: AssertableWithBounds<'a, T>,
{
    type Output = <U as AssertableWithBounds<'a, T>>::Output;

    fn do_assert(self) -> Self::Output {
        self.assertable.do_assert(self.bounds)
    }
}

pub trait ProvideBounds<'a, T>
where
    Self: Sized + AssertableWithBounds<'a, T>,
{
    fn provide_bounds(self, bounds: T) -> ResolvedBounds<'a, T, Self>;
}

impl<'a, T, U> ProvideBounds<'a, T> for U
where
    U: Sized + AssertableWithBounds<'a, T>,
{
    fn provide_bounds(self, bounds: T) -> ResolvedBounds<'a, T, Self> {
        ResolvedBounds::new(bounds, self.into())
    }
}

pub trait ResolveBounds<'a, T>
where
    Self: Sized,
    T: Sized + AssertableWithBounds<'a, Self> + 'a,
{
    fn resolve_for(
        self,
        assertable: impl Into<MaybeBorrowed<'a, T>>,
    ) -> ResolvedBounds<'a, Self, T>;
}

impl<'a, T, U> ResolveBounds<'a, T> for U
where
    T: Sized + AssertableWithBounds<'a, Self> + 'a,
    Self: Sized,
{
    fn resolve_for(
        self,
        assertable: impl Into<MaybeBorrowed<'a, T>>,
    ) -> ResolvedBounds<'a, Self, T> {
        ResolvedBounds::new(self, assertable.into())
    }
}