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
use crate::OwnedProjection;
use alloc::{borrow::ToOwned, boxed::Box};
use core::{
	borrow::Borrow,
	mem::{self},
	ops::Deref,
	pin::Pin,
};
use tap::Pipe;

mod sealed {
	use crate::OwnedProjection;
	use core::pin::Pin;

	pub trait Sealed: Sized {}
	impl<K, V> Sealed for Pin<OwnedProjection<K, V>> {}
}
use sealed::Sealed;

/// The value-pinning [`OwnedProjection`] API.
///
/// This can't be associated directly because `self: Pin<Self>` is currently not a valid method receiver.
///
/// # A note about performance
///
/// Due to Rust language limitations (as of v1.58.1), it is currently not possible to give most of the methods in
/// this trait their proper return types. This means quite a lot of them (at least formally) heap-allocate and use dynamic dispatch.
///
/// As a workaround, it is presently legal to reinterpret a [`Pin<OwnedProjection<K, V>>`] as [`OwnedProjection<K, V>`],
/// and to call methods that exist in both APIs in their [`OwnedProjection<K, V>`] that way as long as you treat their returned value references as pinning.
pub trait PinningOwnedProjection: Sealed {
	/// The type of stored keys.
	type K;
	/// The type of values.
	type V;

	/// Converts this instance back into a non-pinning [`OwnedProjection<K, V>`].
	///
	/// After calling this, drop implementation panics won't cause a double anymore, even without the `"std"` feature.
	fn unpin(self) -> OwnedProjection<Self::K, Self::V>
	where
		Self::V: Unpin;

	/// Retrieves a reference to the first value associated with `key`, iff available.
	fn get<Q>(&self, key: &Q) -> Option<Pin<&Self::V>>
	where
		Self::K: Borrow<Q>,
		Q: ?Sized + Eq;

	/// Retrieves a mutable reference to the first value associated with `key`, iff available.
	fn get_mut<Q>(&mut self, key: &Q) -> Option<Pin<&mut Self::V>>
	where
		Self::K: Borrow<Q>,
		Q: ?Sized + Eq;

	/// **Lazily** updates this map according to a sequence of item, **fallible** selector and **fallible** factory **triples**.
	///
	/// Values that aren't reused are dropped together with the returned iterator or on the next `.reproject…` method call.
	#[allow(clippy::type_complexity)]
	fn reproject_try_by_keyed_try_with_keyed<'a: 'b, 'b, T, Q, S, F, I, E>(
		&'a mut self,
		items_selectors_factories: I,
	) -> Box<dyn 'b + Iterator<Item = Result<(T, Pin<&'a mut Self::V>), E>>>
	where
		Self::K: Borrow<Q>,
		T: 'b,
		Q: 'b + ?Sized + Eq + ToOwned<Owned = Self::K>,
		S: 'b + FnOnce(&mut T) -> Result<&Q, E>,
		F: 'b + FnOnce(&mut T, &Self::K) -> Result<Self::V, E>,
		I: 'b + IntoIterator<Item = (T, S, F)>,
		E: 'b;

	/// **Lazily** updates this map according to a sequence of items, a **fallible** selector and **fallible** factory.
	///
	/// Values that aren't reused are dropped together with the returned iterator or on the next `.reproject…` method call.
	#[allow(clippy::type_complexity)]
	fn reproject_try_by_try_with<'a: 'b, 'b, T, Q, S, F, I, E>(
		&'a mut self,
		items: I,
		selector: S,
		factory: F,
	) -> Box<dyn 'b + Iterator<Item = Result<(T, Pin<&'a mut Self::V>), E>>>
	where
		Self::K: Borrow<Q>,
		T: 'b,
		Q: 'b + ?Sized + Eq + ToOwned<Owned = Self::K>,
		S: 'b + FnMut(&mut T) -> Result<&Q, E>,
		F: 'b + FnMut(&mut T, &Self::K) -> Result<Self::V, E>,
		I: 'b + IntoIterator<Item = T>,
		E: 'b;

	/// **Lazily** updates this map according to a sequence of item, selector and factory **triples**.
	///
	/// Values that aren't reused are dropped together with the returned iterator or on the next `.reproject…` method call.
	fn reproject_by_keyed_with_keyed<'a: 'b, 'b, T, Q, S, F, I>(
		&'a mut self,
		items_selectors_factories: I,
	) -> Box<dyn 'b + Iterator<Item = (T, Pin<&'a mut Self::V>)>>
	where
		Self::K: Borrow<Q>,
		T: 'b,
		Q: 'b + ?Sized + Eq + ToOwned<Owned = Self::K>,
		S: 'b + FnOnce(&mut T) -> &Q,
		F: 'b + FnOnce(&mut T, &Self::K) -> Self::V,
		I: 'b + IntoIterator<Item = (T, S, F)>;

	/// **Lazily** updates this map according to a sequence of items, a selector and a factory.
	///
	/// Values that aren't reused are dropped together with the returned iterator or on the next `.reproject…` method call.
	fn reproject_by_with<'a: 'b, 'b, T, Q, S, F, I>(
		&'a mut self,
		items: I,
		selector: S,
		factory: F,
	) -> Box<dyn 'b + Iterator<Item = (T, Pin<&'a mut Self::V>)>>
	where
		Self::K: Borrow<Q>,
		T: 'b,
		Q: 'b + ?Sized + Eq + ToOwned<Owned = Self::K>,
		S: 'b + FnMut(&mut T) -> &Q,
		F: 'b + FnMut(&mut T, &Self::K) -> Self::V,
		I: 'b + IntoIterator<Item = T>;
}
impl<K, V> PinningOwnedProjection for Pin<OwnedProjection<K, V>> {
	type K = K;

	type V = V;

	fn unpin(self) -> OwnedProjection<K, V>
	where
		V: Unpin,
	{
		let mut this: OwnedProjection<K, V> = unsafe { mem::transmute(self) };
		this.pinning = false;
		this
	}

	fn get<Q>(&self, key: &Q) -> Option<Pin<&V>>
	where
		K: Borrow<Q>,
		Q: ?Sized + Eq,
	{
		self.as_non_pin().get(key).map(wrap_in_pin)
	}

	fn get_mut<Q>(&mut self, key: &Q) -> Option<Pin<&mut V>>
	where
		K: Borrow<Q>,
		Q: ?Sized + Eq,
	{
		self.as_non_pin_mut().get_mut(key).map(wrap_in_pin)
	}

	#[allow(clippy::type_complexity)]
	fn reproject_try_by_keyed_try_with_keyed<'a: 'b, 'b, T, Q, S, F, I, E>(
		&'a mut self,
		items_selectors_factories: I,
	) -> Box<dyn 'b + Iterator<Item = Result<(T, Pin<&'a mut V>), E>>>
	where
		K: Borrow<Q>,
		T: 'b,
		Q: 'b + ?Sized + Eq + ToOwned<Owned = K>,
		S: 'b + FnOnce(&mut T) -> Result<&Q, E>,
		F: 'b + FnOnce(&mut T, &K) -> Result<V, E>,
		I: 'b + IntoIterator<Item = (T, S, F)>,
		E: 'b,
	{
		self.as_non_pin_mut()
			.reproject_try_by_keyed_try_with_keyed(items_selectors_factories)
			.map(wrap_value_in_result_in_pin)
			.pipe(Box::new)
	}

	#[allow(clippy::type_complexity)]
	fn reproject_try_by_try_with<'a: 'b, 'b, T, Q, S, F, I, E>(
		&'a mut self,
		items: I,
		selector: S,
		factory: F,
	) -> Box<dyn 'b + Iterator<Item = Result<(T, Pin<&'a mut V>), E>>>
	where
		K: Borrow<Q>,
		T: 'b,
		Q: 'b + ?Sized + Eq + ToOwned<Owned = K>,
		S: 'b + FnMut(&mut T) -> Result<&Q, E>,
		F: 'b + FnMut(&mut T, &K) -> Result<V, E>,
		I: 'b + IntoIterator<Item = T>,
		E: 'b,
	{
		self.as_non_pin_mut()
			.reproject_try_by_try_with(items, selector, factory)
			.map(wrap_value_in_result_in_pin)
			.pipe(Box::new)
	}

	fn reproject_by_keyed_with_keyed<'a: 'b, 'b, T, Q, S, F, I>(
		&'a mut self,
		items_selectors_factories: I,
	) -> Box<dyn 'b + Iterator<Item = (T, Pin<&'a mut V>)>>
	where
		K: Borrow<Q>,
		T: 'b,
		Q: 'b + ?Sized + Eq + ToOwned<Owned = K>,
		S: 'b + FnOnce(&mut T) -> &Q,
		F: 'b + FnOnce(&mut T, &K) -> V,
		I: 'b + IntoIterator<Item = (T, S, F)>,
	{
		self.as_non_pin_mut()
			.reproject_by_keyed_with_keyed(items_selectors_factories)
			.map(wrap_value_in_pin)
			.pipe(Box::new)
	}

	fn reproject_by_with<'a: 'b, 'b, T, Q, S, F, I>(
		&'a mut self,
		items: I,
		selector: S,
		factory: F,
	) -> Box<dyn 'b + Iterator<Item = (T, Pin<&'a mut V>)>>
	where
		K: Borrow<Q>,
		T: 'b,
		Q: 'b + ?Sized + Eq + ToOwned<Owned = K>,
		S: 'b + FnMut(&mut T) -> &Q,
		F: 'b + FnMut(&mut T, &K) -> V,
		I: 'b + IntoIterator<Item = T>,
	{
		self.as_non_pin_mut()
			.reproject_by_with(items, selector, factory)
			.map(wrap_value_in_pin)
			.pipe(Box::new)
	}
}

/// # Safety
///
/// This trait is only safe to implement if not misused.
unsafe trait PinHelper {
	type T;

	fn as_non_pin(&self) -> &Self::T;
	fn as_non_pin_mut(&mut self) -> &mut Self::T;
}

/// # Safety Notes
///
/// All methods on [`OwnedProjection`] that are callable through the pinning API act as if the values were always pinned.
unsafe impl<K, V> PinHelper for Pin<OwnedProjection<K, V>> {
	type T = OwnedProjection<K, V>;

	fn as_non_pin(&self) -> &Self::T {
		unsafe { &*(self as *const Pin<OwnedProjection<K, V>>).cast::<OwnedProjection<K, V>>() }
	}

	fn as_non_pin_mut(&mut self) -> &mut Self::T {
		unsafe { &mut *(self as *mut Pin<OwnedProjection<K, V>>).cast::<OwnedProjection<K, V>>() }
	}
}

/// # Safety Notes
///
/// This would be horribly unsafe if exposed. It acts as adapter in the pinning API here,
/// since the non-pinning API (privately!) already acts as if the values were pinned,
/// as far as it is callable through the public pinning API.
fn wrap_in_pin<V: Deref>(value: V) -> Pin<V> {
	unsafe { Pin::new_unchecked(value) }
}

/// # Safety Notes
///
/// This would be horribly unsafe if exposed. It acts as adapter in the pinning API here,
/// since the non-pinning API (privately!) already acts as if the values were pinned,
/// as far as it is callable through the public pinning API.
fn wrap_value_in_pin<T, V: Deref>((item, value): (T, V)) -> (T, Pin<V>) {
	(item, wrap_in_pin(value))
}

/// # Safety Notes
///
/// This would be horribly unsafe if exposed. It acts as adapter in the pinning API here,
/// since the non-pinning API (privately!) already acts as if the values were pinned,
/// as far as it is callable through the public pinning API.
fn wrap_value_in_result_in_pin<T, V: Deref, E>(
	result: Result<(T, V), E>,
) -> Result<(T, Pin<V>), E> {
	result.map(wrap_value_in_pin)
}