-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathpin.rs
63 lines (59 loc) · 2.66 KB
/
pin.rs
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
use core::{array::IntoIter, pin::Pin};
use std::{iter::Map, slice::SliceIndex};
// From: `futures_rs::join_all!` -- https://github.com/rust-lang/futures-rs/blob/b48eb2e9a9485ef7388edc2f177094a27e08e28b/futures-util/src/future/join_all.rs#L18-L23
pub(crate) fn iter_pin_mut<T>(slice: Pin<&mut [T]>) -> impl Iterator<Item = Pin<&mut T>> {
// SAFETY: `std` _could_ make this unsound if it were to decide Pin's
// invariants aren't required to transmit through slices. Otherwise this has
// the same safety as a normal field pin projection.
unsafe { slice.get_unchecked_mut() }
.iter_mut()
.map(|t| unsafe { Pin::new_unchecked(t) })
}
// From: `futures_rs::join_all!` -- https://github.com/rust-lang/futures-rs/blob/b48eb2e9a9485ef7388edc2f177094a27e08e28b/futures-util/src/future/join_all.rs#L18-L23
pub(crate) fn iter_pin_mut_vec<T>(slice: Pin<&mut Vec<T>>) -> impl Iterator<Item = Pin<&mut T>> {
// SAFETY: `std` _could_ make this unsound if it were to decide Pin's
// invariants aren't required to transmit through slices. Otherwise this has
// the same safety as a normal field pin projection.
unsafe { slice.get_unchecked_mut() }
.iter_mut()
.map(|t| unsafe { Pin::new_unchecked(t) })
}
/// Returns a pinned mutable reference to an element or subslice depending on the
/// type of index (see `get`) or `None` if the index is out of bounds.
// From: https://github.com/rust-lang/rust/pull/78370/files
#[inline]
pub(crate) fn get_pin_mut<T, I>(slice: Pin<&mut [T]>, index: I) -> Option<Pin<&mut I::Output>>
where
I: SliceIndex<[T]>,
{
// SAFETY: `get_unchecked_mut` is never used to move the slice inside `self` (`SliceIndex`
// is sealed and all `SliceIndex::get_mut` implementations never move elements).
// `x` is guaranteed to be pinned because it comes from `self` which is pinned.
unsafe {
slice
.get_unchecked_mut()
.get_mut(index)
.map(|x| Pin::new_unchecked(x))
}
}
// NOTE: If this is implemented through the trait, this will work on both vecs and
// slices.
//
// From: https://github.com/rust-lang/rust/pull/78370/files
pub(crate) fn get_pin_mut_from_vec<T, I>(
slice: Pin<&mut Vec<T>>,
index: I,
) -> Option<Pin<&mut I::Output>>
where
I: SliceIndex<[T]>,
{
// SAFETY: `get_unchecked_mut` is never used to move the slice inside `self` (`SliceIndex`
// is sealed and all `SliceIndex::get_mut` implementations never move elements).
// `x` is guaranteed to be pinned because it comes from `self` which is pinned.
unsafe {
slice
.get_unchecked_mut()
.get_mut(index)
.map(|x| Pin::new_unchecked(x))
}
}