Skip to content

Commit 19ac297

Browse files
committed
add ref_cell projections
1 parent 69af413 commit 19ac297

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/ops.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,5 @@ include!("./projections/maybe_uninit.rs");
7878
include!("./projections/non_null.rs");
7979
include!("./projections/pin.rs");
8080
include!("./projections/raw_ptr.rs");
81+
include!("./projections/ref_cell.rs");
8182
include!("./projections/unsafe_cell.rs");

src/projections/ref_cell.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use core::cell::{Ref, RefMut};
2+
3+
use crate::helper::project_ref;
4+
5+
impl<T> Projectable for Ref<'_, T> {
6+
type Inner = T;
7+
}
8+
9+
impl<T> Projectable for RefMut<'_, T> {
10+
type Inner = T;
11+
}
12+
13+
unsafe impl<T> SafeProject for Ref<'_, T> {}
14+
unsafe impl<T> SafeProject for RefMut<'_, T> {}
15+
16+
impl<'a, T, F> Project<F> for Ref<'a, T>
17+
where
18+
F: Field<Base = T>,
19+
F::Type: Sized + 'a,
20+
{
21+
type Output<'b>
22+
= Ref<'b, F::Type>
23+
where
24+
Self: 'b;
25+
unsafe fn project<'b>(this: *const Self) -> Self::Output<'b>
26+
where
27+
Self: 'b,
28+
{
29+
let this = unsafe { &*this };
30+
Ref::map(Ref::clone(this), |r| project_ref::<F>(r))
31+
}
32+
}
33+
34+
impl<'a, T, F> ProjectMut<F> for RefMut<'a, T>
35+
where
36+
F: Field<Base = T>,
37+
F::Type: Sized + 'a,
38+
{
39+
type OutputMut<'b>
40+
= RefMut<'b, F::Type>
41+
where
42+
Self: 'b;
43+
unsafe fn project_mut<'b>(_this: *mut Self) -> Self::OutputMut<'b>
44+
where
45+
Self: 'b,
46+
{
47+
// this can't be implemented without being able to increment the mutable borrow count
48+
// but it would look like the project for `Ref`
49+
todo!("cannot be implemented outside of the stdlib")
50+
}
51+
}
52+
53+
unsafe impl<T> compat::ProjectableExt for Ref<'_, T> {
54+
type Safety = compat::Safe;
55+
}
56+
57+
unsafe impl<T> compat::ProjectableExt for RefMut<'_, T> {
58+
type Safety = compat::Safe;
59+
}

0 commit comments

Comments
 (0)