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
use core::intrinsics::type_id;

use crate::{Cons, Nil};

pub trait LocalMaybeContains<Item, const HEAD: bool> {
    const CONTAINS: bool;

    type Inserted;
    type Removed;

    fn try_get(&self) -> Option<&Item>;

    fn try_get_mut(&mut self) -> Option<&mut Item>;

    fn try_remove(self) -> (Option<Item>, Self::Removed);

    fn try_insert(self, value: Item) -> (Option<Item>, Self::Inserted);
}

impl<Item> LocalMaybeContains<Item, true> for Nil
where
    Item: 'static,
{
    const CONTAINS: bool = false;

    type Inserted = Cons<Item, Nil>;
    type Removed = Nil;

    fn try_get(&self) -> Option<&Item> {
        None
    }

    fn try_get_mut(&mut self) -> Option<&mut Item> {
        None
    }

    fn try_remove(self) -> (Option<Item>, Self::Removed) {
        (None, self)
    }

    fn try_insert(self, head: Item) -> (Option<Item>, Self::Inserted) {
        (None, Cons { head, tail: self })
    }
}

impl<Item, T> LocalMaybeContains<Item, true> for Cons<Item, T>
where
    Item: 'static,
{
    const CONTAINS: bool = true;

    type Inserted = Self;
    type Removed = T;

    fn try_get(&self) -> Option<&Item> {
        Some(&self.head)
    }

    fn try_get_mut(&mut self) -> Option<&mut Item> {
        Some(&mut self.head)
    }

    fn try_remove(self) -> (Option<Item>, Self::Removed) {
        let Self { head, tail } = self;
        (Some(head), tail)
    }

    fn try_insert(self, item: Item) -> (Option<Item>, Self::Inserted) {
        let Self { head, tail } = self;
        (Some(head), Cons { head: item, tail })
    }
}

impl<Item, H, T> LocalMaybeContains<Item, false> for Cons<H, T>
where
    Item: 'static,
    T: MaybeContains<Item>,
{
    const CONTAINS: bool = T::CONTAINS;

    type Inserted = Cons<H, T::Inserted>;
    type Removed = Cons<H, T::Removed>;

    fn try_get(&self) -> Option<&Item> {
        self.tail.try_get()
    }

    fn try_get_mut(&mut self) -> Option<&mut Item> {
        self.tail.try_get_mut()
    }

    fn try_remove(self) -> (Option<Item>, Self::Removed) {
        let Self { head, tail } = self;
        let (item, removed) = tail.try_remove();
        let removed: Self::Removed = Cons {
            head,
            tail: removed,
        };
        (item, removed)
    }

    fn try_insert(self, item: Item) -> (Option<Item>, Self::Inserted) {
        let Self { head, tail } = self;
        let (item, inserted) = tail.try_insert(item);
        let inserted = Cons {
            head,
            tail: inserted,
        };
        (item, inserted)
    }
}

/// A trait marking whether `T` is maybe present.
///
/// See [Traits](crate#traits) section of crate documentation for more information.
pub trait MaybeContains<Item> {
    #[doc(hidden)]
    const CONTAINS: bool;

    #[doc(hidden)]
    type Removed;
    #[doc(hidden)]
    type Inserted;

    #[doc(hidden)]
    fn try_get(&self) -> Option<&Item>;

    #[doc(hidden)]
    fn try_get_mut(&mut self) -> Option<&mut Item>;

    #[doc(hidden)]
    fn try_insert(self, item: Item) -> (Option<Item>, Self::Inserted);

    #[doc(hidden)]
    fn try_remove(self) -> (Option<Item>, Self::Removed);
}

impl<Item> MaybeContains<Item> for Nil {
    const CONTAINS: bool = false;

    type Inserted = Cons<Item, Nil>;
    type Removed = Nil;

    fn try_get(&self) -> Option<&Item> {
        None
    }

    fn try_get_mut(&mut self) -> Option<&mut Item> {
        None
    }

    fn try_insert(self, head: Item) -> (Option<Item>, Self::Inserted) {
        (None, Cons { head, tail: self })
    }

    fn try_remove(self) -> (Option<Item>, Self::Removed) {
        (None, Nil)
    }
}

impl<Item, H, T> MaybeContains<Item> for Cons<H, T>
where
    Item: 'static,
    H: 'static,
    Self: LocalMaybeContains<Item, { type_id::<Item>() == type_id::<H>() }>,
{
    type Inserted =
        <Self as LocalMaybeContains<Item, { type_id::<Item>() == type_id::<H>() }>>::Inserted;
    type Removed =
        <Self as LocalMaybeContains<Item, { type_id::<Item>() == type_id::<H>() }>>::Removed;

    const CONTAINS: bool =
        <Self as LocalMaybeContains<Item, { type_id::<Item>() == type_id::<H>() }>>::CONTAINS;

    fn try_get(&self) -> Option<&Item> {
        <Self as LocalMaybeContains<Item, { type_id::<Item>() == type_id::<H>() }>>::try_get(self)
    }

    fn try_get_mut(&mut self) -> Option<&mut Item> {
        <Self as LocalMaybeContains<Item, { type_id::<Item>() == type_id::<H>() }>>::try_get_mut(
            self,
        )
    }

    fn try_insert(self, item: Item) -> (Option<Item>, Self::Inserted) {
        <Self as LocalMaybeContains<Item, { type_id::<Item>() == type_id::<H>() }>>::try_insert(
            self, item,
        )
    }

    fn try_remove(self) -> (Option<Item>, Self::Removed) {
        <Self as LocalMaybeContains<Item, { type_id::<Item>() == type_id::<H>() }>>::try_remove(
            self,
        )
    }
}

#[cfg(test)]
mod tests {
    use crate::{Cons, MaybeContains, Nil};

    static_assertions::assert_impl_all!(Nil: MaybeContains<u32, CONTAINS = false>);
    static_assertions::assert_impl_all!(Cons<u32, Nil>: MaybeContains<u32, CONTAINS = true>);
}