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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use orx_pinned_vec::PinnedVec;
use orx_split_vec::SplitVec;
use std::{cell::UnsafeCell, marker::PhantomData};

/// `ImpVec`, standing for immutable push vector 👿, is a data structure which allows appending elements with a shared reference.
///
/// Specifically, it extends vector capabilities with the following two methods:
/// * `fn imp_push(&self, value: T)`
/// * `fn imp_extend_from_slice(&self, slice: &[T])`
///
/// Note that both of these methods can be called with `&self` rather than `&mut self`.
///
/// # Motivation
///
/// Appending to a vector with a shared reference sounds unconventional, and it is.
/// However, if we consider our vector as a bag of or a container of things rather than having a collective meaning;
/// then, appending element or elements to the end of the vector:
/// * does not mutate any of already added elements, and hence,
/// * **it is not different than creating a new element in the scope**.
///
/// # Safety
///
/// It is natural to expect that appending elements to a vector does not affect already added elements.
/// However, this is usually not the case due to underlying memory management.
/// For instance, `std::vec::Vec` may move already added elements to different memory locations to maintain the contagious layout of the vector.
///
/// `PinnedVec` prevents such implicit changes in memory locations.
/// It guarantees that push and extend methods keep memory locations of already added elements intact.
/// Therefore, it is perfectly safe to hold on to references of the vector while appending elements.
///
/// Consider the classical example that does not compile, which is often presented to highlight the safety guarantees of rust:
///
/// ```rust
/// let mut vec = vec![0, 1, 2, 3];
///
/// let ref_to_first = &vec[0];
/// assert_eq!(ref_to_first, &0);
///
/// vec.push(4);
///
/// // does not compile due to the following reason:  cannot borrow `vec` as mutable because it is also borrowed as immutable
/// // assert_eq!(ref_to_first, &0);
/// ```
///
/// This wonderful feature of the borrow checker of rust is not required and used for `imp_push` and `imp_extend_from_slice` methods of `ImpVec`
/// since these methods do not require a `&mut self` reference.
/// Therefore, the following code compiles and runs perfectly safely.
///
/// ```rust
/// use orx_imp_vec::prelude::*;
///
/// let mut vec = ImpVec::new();
/// vec.extend_from_slice(&[0, 1, 2, 3]);
///
/// let ref_to_first = &vec[0];
/// assert_eq!(ref_to_first, &0);
///
/// vec.imp_push(4);
/// assert_eq!(vec.len(), 5);
///
/// vec.imp_extend_from_slice(&[6, 7]);
/// assert_eq!(vec.len(), 7);
///
/// assert_eq!(ref_to_first, &0);
/// ```
pub struct ImpVec<T, P = SplitVec<T>>
where
    P: PinnedVec<T>,
{
    pub(crate) pinned_vec: UnsafeCell<P>,
    pub(crate) phantom: PhantomData<T>,
}

impl<T, P: PinnedVec<T>> ImpVec<T, P> {
    /// Consumes the imp-vec into the wrapped inner pinned vector.
    ///
    /// # Example
    ///
    /// ```rust
    /// use orx_split_vec::SplitVec;
    /// use orx_imp_vec::ImpVec;
    ///
    /// let pinned_vec = SplitVec::new();
    ///
    /// let imp_vec = ImpVec::from(pinned_vec);
    /// imp_vec.imp_push(42);
    ///
    /// let pinned_vec = imp_vec.into_inner();
    /// assert_eq!(&pinned_vec, &[42]);
    /// ```
    pub fn into_inner(self) -> P {
        self.pinned_vec.into_inner()
    }

    /// Pushes the `value` to the vector.
    /// This method differs from the `push` method with the required reference.
    /// Unlike `push`, `imp_push` allows to push the element with a shared reference.
    ///
    /// # Example
    ///
    /// ```rust
    /// use orx_imp_vec::prelude::*;
    ///
    /// let mut vec = ImpVec::new();
    ///
    /// // regular push with &mut self
    /// vec.push(42);
    ///
    /// // hold on to a reference to the first element
    /// let ref_to_first = &vec[0];
    /// assert_eq!(ref_to_first, &42);
    ///
    /// // imp_push with &self
    /// vec.imp_push(7);
    ///
    /// // due to `PinnedVec` guarantees, this push will never invalidate prior references
    /// assert_eq!(ref_to_first, &42);
    /// ```
    ///
    /// # Safety
    ///
    /// Wrapping a `PinnedVec` with an `ImpVec` provides with two additional methods: `imp_push` and `imp_extend_from_slice`.
    /// Note that these push and extend methods grow the vector by appending elements to the end.
    ///
    /// It is natural to expect that these operations do not change the memory locations of already added elements.
    /// However, this is usually not the case due to underlying allocations.
    /// For instance, `std::vec::Vec` may move already added elements in memory to maintain the contagious layout of the vector.
    ///
    /// `PinnedVec` prevents such implicit changes in memory locations.
    /// It guarantees that push and extend methods keep memory locations of already added elements intact.
    /// Therefore, it is perfectly safe to hold on to references of the vector while appending elements.
    ///
    /// Consider the classical example that does not compile, which is often presented to highlight the safety guarantees of rust:
    ///
    /// ```rust
    /// let mut vec = vec![0, 1, 2, 3];
    ///
    /// let ref_to_first = &vec[0];
    /// assert_eq!(ref_to_first, &0);
    ///
    /// vec.push(4);
    ///
    /// // does not compile due to the following reason:  cannot borrow `vec` as mutable because it is also borrowed as immutable
    /// // assert_eq!(ref_to_first, &0);
    /// ```
    ///
    /// This wonderful feature of the borrow checker of rust is not required and used for `imp_push` and `imp_extend_from_slice` methods of `ImpVec`
    /// since these methods do not require a `&mut self` reference.
    /// Therefore, the following code compiles and runs perfectly safely.
    ///
    /// ```rust
    /// use orx_imp_vec::prelude::*;
    ///
    /// let mut vec = ImpVec::new();
    /// vec.extend_from_slice(&[0, 1, 2, 3]);
    ///
    /// let ref_to_first = &vec[0];
    /// assert_eq!(ref_to_first, &0);
    ///
    /// vec.imp_push(4);
    /// assert_eq!(vec.len(), 5);
    ///
    /// assert_eq!(ref_to_first, &0);
    /// ```
    ///
    /// Although unconventional, this makes sense when we consider the `ImpVec` as a bag or container of things, rather than having a collective meaning.
    /// In other words, when we do not rely on reduction methods, such as `count` or `sum`, appending element or elements to the end of the vector:
    /// * does not mutate any of already added elements, and hence,
    /// * **it is not different than creating a new element in the scope**.
    pub fn imp_push(&self, value: T) {
        self.pinned_mut().push(value);
    }

    /// Extends the vector with the given `slice`.
    /// This method differs from the `extend_from_slice` method with the required reference.
    /// Unlike `extend_from_slice`, `imp_extend_from_slice` allows to push the element with a shared reference.
    ///
    /// # Example
    ///
    /// ```rust
    /// use orx_imp_vec::prelude::*;
    ///
    /// let mut vec = ImpVec::new();
    ///
    /// // regular extend_from_slice with &mut self
    /// vec.extend_from_slice(&[42]);
    ///
    /// // hold on to a reference to the first element
    /// let ref_to_first = &vec[0];
    /// assert_eq!(ref_to_first, &42);
    ///
    /// // imp_extend_from_slice with &self
    /// vec.imp_extend_from_slice(&[0, 1, 2, 3]);
    /// assert_eq!(vec.len(), 5);
    ///
    /// // due to `PinnedVec` guarantees, this extend will never invalidate prior references
    /// assert_eq!(ref_to_first, &42);
    /// ```
    ///
    /// # Safety
    ///
    /// Wrapping a `PinnedVec` with an `ImpVec` provides with two additional methods: `imp_push` and `imp_extend_from_slice`.
    /// Note that these push and extend methods grow the vector by appending elements to the end.
    ///
    /// It is natural to expect that these operations do not change the memory locations of already added elements.
    /// However, this is usually not the case due to underlying allocations.
    /// For instance, `std::vec::Vec` may move already added elements in memory to maintain the contagious layout of the vector.
    ///
    /// `PinnedVec` prevents such implicit changes in memory locations.
    /// It guarantees that push and extend methods keep memory locations of already added elements intact.
    /// Therefore, it is perfectly safe to hold on to references of the vector while appending elements.
    ///
    /// Consider the classical example that does not compile, which is often presented to highlight the safety guarantees of rust:
    ///
    /// ```rust
    /// let mut vec = vec![0];
    ///
    /// let ref_to_first = &vec[0];
    /// assert_eq!(ref_to_first, &0);
    ///
    /// vec.extend_from_slice(&[1, 2, 3, 4]);
    ///
    /// // does not compile due to the following reason:  cannot borrow `vec` as mutable because it is also borrowed as immutable
    /// // assert_eq!(ref_to_first, &0);
    /// ```
    ///
    /// This wonderful feature of the borrow checker of rust is not required and used for `imp_push` and `imp_extend_from_slice` methods of `ImpVec`
    /// since these methods do not require a `&mut self` reference.
    /// Therefore, the following code compiles and runs perfectly safely.
    ///
    /// ```rust
    /// use orx_imp_vec::prelude::*;
    ///
    /// let mut vec = ImpVec::new();
    /// vec.push(0);
    ///
    /// let ref_to_first = &vec[0];
    /// assert_eq!(ref_to_first, &0);
    ///
    /// vec.imp_extend_from_slice(&[1, 2, 3, 4]);
    ///
    /// assert_eq!(ref_to_first, &0);
    /// ```
    ///
    /// Although unconventional, this makes sense when we consider the `ImpVec` as a bag or container of things, rather than having a collective meaning.
    /// In other words, when we do not rely on reduction methods, such as `count` or `sum`, appending element or elements to the end of the vector:
    /// * does not mutate any of already added elements, and hence,
    /// * **it is not different than creating a new element in the scope**.
    pub fn imp_extend_from_slice(&self, slice: &[T])
    where
        T: Clone,
    {
        self.pinned_mut().extend_from_slice(slice);
    }

    // helper
    #[allow(clippy::mut_from_ref)]
    pub(crate) fn pinned_mut(&self) -> &mut P {
        // SAFETY: `ImpVec` does not implement Send or Sync.
        // Further `imp_push` and `imp_extend_from_slice` methods are safe to call with a shared reference due to pinned vector guarantees.
        // All other calls to this internal method require a mutable reference.
        unsafe { &mut *self.pinned_vec.get() }
    }
}

impl<T> Default for ImpVec<T> {
    /// Creates a new empty imp-vec.
    ///
    /// # Example
    ///
    /// ```rust
    /// use orx_imp_vec::prelude::*;
    ///
    /// let imp_vec: ImpVec<usize> = ImpVec::default();
    /// assert!(imp_vec.is_empty());
    /// ```
    fn default() -> Self {
        Self::new()
    }
}

impl<T, P> Clone for ImpVec<T, P>
where
    P: PinnedVec<T> + Clone,
{
    fn clone(&self) -> Self {
        let pinned_vec = unsafe { &mut *self.pinned_vec.get() }.clone();
        Self {
            pinned_vec: pinned_vec.into(),
            phantom: self.phantom,
        }
    }
}