Skip to content

Commit 9fa3c4b

Browse files
committed
more docs & make projection traits unsafe
1 parent 12f81c5 commit 9fa3c4b

File tree

8 files changed

+36
-23
lines changed

8 files changed

+36
-23
lines changed

examples/kernel_mutex_rcu.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ mod rcu {
9797
type Safety = Safe;
9898
}
9999

100-
impl<'a, T, U, F> Project<F> for &'a RcuMutex<T>
100+
unsafe impl<'a, T, U, F> Project<F> for &'a RcuMutex<T>
101101
where
102102
F: UnalignedField<Base = T, Type = Rcu<U>>,
103103
U: 'a,
@@ -143,7 +143,7 @@ struct MyDriver {
143143

144144
impl MyDriver {
145145
fn flush_sensitivity<'a>(&'a self, rcu_guard: &'a RcuGuard) -> u8 {
146-
let buf = &self.buf;
146+
let buf: &'a RcuMutex<Buffer> = &self.buf;
147147
start_proj!(buf);
148148
// Here we use the special projections set up for `Mutex` with fields of type `Rcu<T>`.
149149
let cfg: &Rcu<Box<BufferConfig>> = p!(@buf->cfg);
@@ -158,14 +158,14 @@ impl MyDriver {
158158
}
159159

160160
fn set_buffer_config(&self, flush_sensitivity: u8) {
161-
// Our `Mutex` pins the value.
161+
// `RcuMutex` pins the value.
162162
let mut guard: Pin<RcuMutexGuard<'_, Buffer>> = self.buf.lock();
163-
let mut buf = guard.as_mut();
163+
let mut buf: Pin<&mut Buffer> = guard.as_mut();
164164
start_proj!(mut buf);
165165
// We can use pin-projections since we marked `cfg` as `#[pin]`
166166
let cfg: Pin<&mut Rcu<Box<BufferConfig>>> = p!(@mut buf->cfg);
167167
cfg.set(Box::new(BufferConfig { flush_sensitivity }));
168-
// ^^ this returns an `Old<Box<BufferConfig>>` and runs `synchronize_rcu` on drop.
168+
// ^^^ this returns an `Old<Box<BufferConfig>>` and runs `synchronize_rcu` on drop.
169169
}
170170
}
171171

examples/kernel_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ unsafe impl<'a, T: 'a> ProjectableExt for Ptr<'a, T> {
2626
type Safety = Safe;
2727
}
2828

29-
impl<'a, T, F> Project<F> for Ptr<'a, T>
29+
unsafe impl<'a, T, F> Project<F> for Ptr<'a, T>
3030
where
3131
T: 'a,
3232
F: Field<Base = T>,

src/ops.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,20 @@ pub trait Projectable: Sized {
1313
type Inner: ?Sized;
1414
}
1515

16+
/// Marks project operations as safe.
17+
///
18+
/// # Safety
19+
///
20+
/// * The `@base->field` and `@mut base->field` operations implemented by the [`Project`] and
21+
/// [`ProjectMut`] traits must not have additional safety requirements.
22+
pub unsafe trait SafeProject: Projectable {}
23+
1624
/// Shared projection operation `@base->field`.
17-
pub trait Project<F>: Projectable
25+
///
26+
/// # Safety
27+
///
28+
///
29+
pub unsafe trait Project<F>: Projectable
1830
where
1931
F: UnalignedField<Base = Self::Inner>,
2032
{
@@ -28,13 +40,20 @@ where
2840
/// # Safety
2941
///
3042
/// * `this` must be a dereferenceable pointer pointing at a valid value of `Self`.
43+
/// * for the duration of `'a`, the value at `this` is only used by other projection
44+
/// operations.
45+
/// * for the duration of `'a`, the value at `this` is not mutably projected with `F`.
3146
unsafe fn project<'a>(this: *const Self) -> Self::Output<'a>
3247
where
3348
Self: 'a;
3449
}
3550

3651
/// Exclusive projection operation `@mut base->field`.
37-
pub trait ProjectMut<F>: Projectable
52+
///
53+
/// # Safety
54+
///
55+
///
56+
pub unsafe trait ProjectMut<F>: Projectable
3857
where
3958
F: UnalignedField<Base = Self::Inner>,
4059
{
@@ -48,19 +67,13 @@ where
4867
/// # Safety
4968
///
5069
/// * `this` must be a dereferenceable pointer pointing at a valid value of `Self`.
51-
/// * this function must only be called once.
70+
/// * for the duration of `'a`, the value at `this` is only used by other projection
71+
/// operations for fields other than `F`.
5272
unsafe fn project_mut<'a>(this: *mut Self) -> Self::OutputMut<'a>
5373
where
5474
Self: 'a;
5575
}
5676

57-
/// Marks project operations as safe.
58-
///
59-
/// # Safety
60-
///
61-
/// * the `@base->field` and `@mut base->field` operations must be safe.
62-
pub unsafe trait SafeProject: Projectable {}
63-
6477
include!("./projections/maybe_uninit.rs");
6578
include!("./projections/non_null.rs");
6679
include!("./projections/pin.rs");

src/projections/maybe_uninit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ impl<T> Projectable for &mut MaybeUninit<T> {
66

77
unsafe impl<T> SafeProject for &mut MaybeUninit<T> {}
88

9-
impl<'a, T, F> ProjectMut<F> for &'a mut MaybeUninit<T>
9+
unsafe impl<'a, T, F> ProjectMut<F> for &'a mut MaybeUninit<T>
1010
where
1111
F: Field<Base = T>,
1212
F::Type: Sized + 'a,

src/projections/non_null.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ impl<T: ?Sized> Projectable for NonNull<T> {
44
type Inner = T;
55
}
66

7-
impl<T, F> Project<F> for NonNull<T>
7+
unsafe impl<T, F> Project<F> for NonNull<T>
88
where
99
F: UnalignedField<Base = T>,
1010
F::Type: Sized,

src/projections/pin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ impl<T> Projectable for Pin<&mut T> {
66

77
unsafe impl<T> SafeProject for Pin<&mut T> {}
88

9-
impl<'a, T, F> Project<F> for Pin<&'a mut T>
9+
unsafe impl<'a, T, F> Project<F> for Pin<&'a mut T>
1010
where
1111
F: PinableField<Base = T> + Field<Base = T>,
1212
F::Type: Sized + 'a,
@@ -26,7 +26,7 @@ where
2626
}
2727
}
2828

29-
impl<'a, T, F> ProjectMut<F> for Pin<&'a mut T>
29+
unsafe impl<'a, T, F> ProjectMut<F> for Pin<&'a mut T>
3030
where
3131
F: PinableField<Base = T> + Field<Base = T>,
3232
F::Type: Sized + 'a,

src/projections/raw_ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ impl<T> Projectable for *const T {
22
type Inner = T;
33
}
44

5-
impl<T, F> Project<F> for *const T
5+
unsafe impl<T, F> Project<F> for *const T
66
where
77
F: UnalignedField<Base = T>,
88
F::Type: Sized,
@@ -25,7 +25,7 @@ impl<T> Projectable for *mut T {
2525
type Inner = T;
2626
}
2727

28-
impl<T, F> Project<F> for *mut T
28+
unsafe impl<T, F> Project<F> for *mut T
2929
where
3030
F: UnalignedField<Base = T>,
3131
F::Type: Sized,

src/projections/unsafe_cell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ impl<T> Projectable for &UnsafeCell<T> {
44
type Inner = T;
55
}
66

7-
impl<'a, T, F> Project<F> for &'a UnsafeCell<T>
7+
unsafe impl<'a, T, F> Project<F> for &'a UnsafeCell<T>
88
where
99
F: Field<Base = T>,
1010
F::Type: 'a + Sized,

0 commit comments

Comments
 (0)