Skip to content

Commit 40a4ed5

Browse files
committed
add read-only projections to MaybeUninit
1 parent 434b8d7 commit 40a4ed5

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/projections/maybe_uninit.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,59 @@
11
use core::mem::MaybeUninit;
22

3+
impl<T> Projectable for &MaybeUninit<T> {
4+
type Inner = T;
5+
}
6+
37
impl<T> Projectable for &mut MaybeUninit<T> {
48
type Inner = T;
59
}
610

11+
// SAFETY: no additional safety requirements on `Project[Mut]::project[_mut]`.
12+
unsafe impl<T> SafeProject for &MaybeUninit<T> {}
13+
714
// SAFETY: no additional safety requirements on `Project[Mut]::project[_mut]`.
815
unsafe impl<T> SafeProject for &mut MaybeUninit<T> {}
916

17+
// No additional safety requirements for `project`.
18+
impl<'a, T, F> Project<F> for &'a MaybeUninit<T>
19+
where
20+
F: Field<Base = T>,
21+
F::Type: Sized + 'a,
22+
{
23+
type Output<'b>
24+
= &'b MaybeUninit<F::Type>
25+
where
26+
Self: 'b;
27+
28+
unsafe fn project<'b>(this: *const Self) -> Self::Output<'a>
29+
where
30+
Self: 'b,
31+
{
32+
let ptr: *const MaybeUninit<T> = unsafe { this.read() };
33+
unsafe { &*ptr.byte_add(F::OFFSET).cast() }
34+
}
35+
}
36+
37+
// No additional safety requirements for `project`.
38+
impl<'a, T, F> Project<F> for &'a mut MaybeUninit<T>
39+
where
40+
F: Field<Base = T>,
41+
F::Type: Sized + 'a,
42+
{
43+
type Output<'b>
44+
= &'b MaybeUninit<F::Type>
45+
where
46+
Self: 'b;
47+
48+
unsafe fn project<'b>(this: *const Self) -> Self::Output<'a>
49+
where
50+
Self: 'b,
51+
{
52+
let ptr: *const MaybeUninit<T> = unsafe { this.read() };
53+
unsafe { &*ptr.byte_add(F::OFFSET).cast() }
54+
}
55+
}
56+
1057
// No additional safety requirements for `project_mut`.
1158
impl<'a, T, F> ProjectMut<F> for &'a mut MaybeUninit<T>
1259
where
@@ -29,6 +76,10 @@ where
2976

3077
// Compat
3178

79+
unsafe impl<T> compat::ProjectableExt for &MaybeUninit<T> {
80+
type Safety = compat::Safe;
81+
}
82+
3283
unsafe impl<T> compat::ProjectableExt for &mut MaybeUninit<T> {
3384
type Safety = compat::Safe;
3485
}

0 commit comments

Comments
 (0)