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
//! A region that copies its inputs.

use std::fmt::{Debug, Formatter};
use std::marker::PhantomData;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{Index, IntoOwned, Push, Region, RegionPreference, ReserveItems};

/// A region for types where the read item type is equal to the index type.
///
/// This region is useful where the type is not larger than roughly two `usize`s (or 1.5x with
/// some optimizations), or looking up the value is too costly. For larger copy types, the memory
/// required to store the copy type and an index is only marginally bigger, with the benefit
/// that the index remains compact.
///
/// # Examples
///
/// For [`MirrorRegion`]s, we can index with a copy type:
/// ```
/// # use flatcontainer::{MirrorRegion, Region};
/// let r = <MirrorRegion<u8>>::default();
/// let output: u8 = r.index(42);
/// assert_eq!(output, 42);
/// ```
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MirrorRegion<T>(PhantomData<T>);

impl<T> Default for MirrorRegion<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T> Debug for MirrorRegion<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "MirrorRegion<{}>", std::any::type_name::<T>())
    }
}

impl<T> Region for MirrorRegion<T>
where
    for<'a> T: Index + IntoOwned<'a, Owned = T>,
{
    type Owned = T;
    type ReadItem<'a> = T where T: 'a;
    type Index = T;

    #[inline]
    fn merge_regions<'a>(_regions: impl Iterator<Item = &'a Self> + Clone) -> Self
    where
        Self: 'a,
    {
        Self::default()
    }

    #[inline]
    fn index(&self, index: Self::Index) -> Self::ReadItem<'_> {
        index
    }

    #[inline(always)]
    fn reserve_regions<'a, I>(&mut self, _regions: I)
    where
        Self: 'a,
        I: Iterator<Item = &'a Self> + Clone,
    {
        // No storage
    }

    #[inline(always)]
    fn clear(&mut self) {
        // No storage
    }

    #[inline]
    fn heap_size<F: FnMut(usize, usize)>(&self, _callback: F) {
        // No storage
    }

    #[inline]
    fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
    where
        Self: 'a,
    {
        item
    }
}

impl<T> Push<T> for MirrorRegion<T>
where
    for<'a> T: Index + IntoOwned<'a, Owned = T>,
{
    #[inline(always)]
    fn push(&mut self, item: T) -> T {
        item
    }
}

impl<T> Push<&T> for MirrorRegion<T>
where
    for<'a> T: Index + IntoOwned<'a, Owned = T>,
{
    #[inline(always)]
    fn push(&mut self, item: &T) -> T {
        *item
    }
}

impl<T> Push<&&T> for MirrorRegion<T>
where
    for<'a> T: Index + IntoOwned<'a, Owned = T>,
{
    #[inline(always)]
    fn push(&mut self, item: &&T) -> T {
        **item
    }
}

impl<T> ReserveItems<T> for MirrorRegion<T>
where
    for<'a> T: Index + IntoOwned<'a, Owned = T>,
{
    #[inline(always)]
    fn reserve_items<I>(&mut self, _items: I)
    where
        I: Iterator<Item = T> + Clone,
    {
        // No storage
    }
}

impl<'a, T> ReserveItems<&'a T> for MirrorRegion<T>
where
    for<'b> T: Index + IntoOwned<'b, Owned = T>,
{
    #[inline(always)]
    fn reserve_items<I>(&mut self, _items: I)
    where
        I: Iterator<Item = &'a T> + Clone,
    {
        // No storage
    }
}

macro_rules! implement_for {
    ($index_type:ty) => {
        impl RegionPreference for $index_type {
            type Owned = Self;
            type Region = MirrorRegion<Self>;
        }

        impl<'a> IntoOwned<'a> for $index_type {
            type Owned = $index_type;

            #[inline]
            fn into_owned(self) -> Self::Owned {
                self
            }

            #[inline]
            fn clone_onto(self, other: &mut Self::Owned) {
                *other = self;
            }

            #[inline]
            fn borrow_as(owned: &'a Self::Owned) -> Self {
                *owned
            }
        }
    };
}

implement_for!(());
implement_for!(bool);
implement_for!(char);

implement_for!(u8);
implement_for!(u16);
implement_for!(u32);
implement_for!(u64);
implement_for!(u128);
implement_for!(usize);

implement_for!(i8);
implement_for!(i16);
implement_for!(i32);
implement_for!(i64);
implement_for!(i128);
implement_for!(isize);

implement_for!(f32);
implement_for!(f64);

implement_for!(std::num::Wrapping<i8>);
implement_for!(std::num::Wrapping<i16>);
implement_for!(std::num::Wrapping<i32>);
implement_for!(std::num::Wrapping<i64>);
implement_for!(std::num::Wrapping<i128>);
implement_for!(std::num::Wrapping<isize>);

implement_for!(std::time::Duration);

#[cfg(test)]
mod tests {
    use crate::ReserveItems;

    use super::*;

    #[test]
    fn test_reserve_regions() {
        let mut r = MirrorRegion::<u8>::default();
        ReserveItems::reserve_items(&mut r, std::iter::once(0));
    }
}