Skip to content

Commit 2382a7a

Browse files
committed
docs & safety improvements
1 parent cd3ea6a commit 2382a7a

File tree

10 files changed

+49
-34
lines changed

10 files changed

+49
-34
lines changed

examples/cpp_ref/cpp_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ unsafe impl<T: ?Sized> ProjectableExt for CppMutRef<T> {
3131
type Safety = Safe;
3232
}
3333

34-
unsafe impl<T, F> ProjectMut<F> for CppMutRef<T>
34+
impl<T, F> ProjectMut<F> for CppMutRef<T>
3535
where
3636
F: Field<Base = T>,
3737
F::Type: Sized,

examples/kernel_mutex_rcu/rcu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ unsafe impl<T> ProjectableExt for &RcuMutex<T> {
9090
type Safety = Safe;
9191
}
9292

93-
unsafe impl<'a, T, U, F> Project<F> for &'a RcuMutex<T>
93+
impl<'a, T, U, F> Project<F> for &'a RcuMutex<T>
9494
where
9595
F: UnalignedField<Base = T, Type = Rcu<U>>,
9696
U: 'a,

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

src/marker.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// A field of a `struct`, `union` or tuple.
1+
/// Type representing a field of a `struct`, `union` or tuple.
22
///
33
/// # Safety
44
///
@@ -15,15 +15,15 @@ pub unsafe trait UnalignedField: Sized {
1515
const OFFSET: usize;
1616
}
1717

18-
/// An aligned field of a `struct`, `union` or tuple.
18+
/// Type representing an aligned field of a `struct`, `union` or tuple.
1919
///
2020
/// # Safety
2121
///
2222
/// Given a well-aligned value of type `Self::Base`, the field at `Self::OFFSET` of type
2323
/// `Self::Type` is well-aligned.
2424
pub unsafe trait Field: UnalignedField {}
2525

26-
/// A field of a `struct`, `union` or tuple with structural pinning information.
26+
/// Type representing a field of a `struct`, `union` or tuple with structural pinning information.
2727
///
2828
/// # Safety
2929
///

src/ops.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@ use crate::{
33
marker::{Field, PinableField, UnalignedField},
44
};
55

6-
/// Type supporting projections.
6+
/// Type supporting field projections.
77
///
8-
/// The exact kind of projection is governed by [`Project`] and [`ProjectMut`]. This trait only
9-
/// gives the compiler access to the `Self::Inner` type which is the type containing the projected
10-
/// fields. So given an expression `base` of type `Self`, then the `field` ident in the expressions
11-
/// `@base->field` and `@mut base->field` refer to a field of the type `Self::Inner`.
8+
/// The exact kind of field projection is governed by [`Project`] and [`ProjectMut`]. This trait
9+
/// only gives the compiler access to the `Self::Inner` type which is the type containing the
10+
/// potentially projectable fields. So given an expression `base` of type `Self`, then the `field`
11+
/// ident in the expressions `@base->field` and `@mut base->field` refer to a field of the type
12+
/// `Self::Inner`.
13+
///
14+
/// If the projection `@base->field` is available still depends on weather `Self` implements
15+
/// `Project<field_of!(Self::Inner, field)>`.
1216
pub trait Projectable: Sized {
1317
type Inner: ?Sized;
1418
}
1519

16-
/// Marks project operations as safe.
20+
/// Marks project operations on `Self` as safe.
1721
///
1822
/// # Safety
1923
///
20-
/// * The `@base->field` and `@mut base->field` operations implemented by the [`Project`] and
21-
/// [`ProjectMut`] traits must not have additional safety requirements.
24+
/// * The [`Project::project`] and [`ProjectMut::project_mut`] functions implemented for `Self`
25+
/// must not have additional safety requirements.
2226
pub unsafe trait SafeProject: Projectable {}
2327

2428
/// Shared projection operation `@base->field`.
25-
///
26-
/// # Safety
27-
///
28-
///
29-
pub unsafe trait Project<F>: Projectable
29+
pub trait Project<F>: Projectable
3030
where
3131
F: UnalignedField<Base = Self::Inner>,
3232
{
@@ -39,21 +39,19 @@ where
3939
///
4040
/// # Safety
4141
///
42-
/// * `this` must be a dereferenceable pointer pointing at a valid value of `Self`.
42+
/// * `this` must be a valid pointer pointing at a valid value of `Self`.
4343
/// * for the duration of `'a`, the value at `this` is only used by other projection
4444
/// operations.
4545
/// * for the duration of `'a`, the value at `this` is not mutably projected with `F`.
46+
/// * Implementers may impose additional safety requirements. These must be documented on the
47+
/// implementation of this trait.
4648
unsafe fn project<'a>(this: *const Self) -> Self::Output<'a>
4749
where
4850
Self: 'a;
4951
}
5052

5153
/// Exclusive projection operation `@mut base->field`.
52-
///
53-
/// # Safety
54-
///
55-
///
56-
pub unsafe trait ProjectMut<F>: Projectable
54+
pub trait ProjectMut<F>: Projectable
5755
where
5856
F: UnalignedField<Base = Self::Inner>,
5957
{
@@ -66,9 +64,11 @@ where
6664
///
6765
/// # Safety
6866
///
69-
/// * `this` must be a dereferenceable pointer pointing at a valid value of `Self`.
70-
/// * for the duration of `'a`, the value at `this` is only used by other projection
67+
/// * `this` must be a valid pointer pointing at a valid value of `Self`.
68+
/// * For the duration of `'a`, the value at `this` is only used by other projection
7169
/// operations for fields other than `F`.
70+
/// * Implementers may impose additional safety requirements. These must be documented on the
71+
/// implementation of this trait.
7272
unsafe fn project_mut<'a>(this: *mut Self) -> Self::OutputMut<'a>
7373
where
7474
Self: 'a;

src/projections/maybe_uninit.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ impl<T> Projectable for &mut MaybeUninit<T> {
44
type Inner = T;
55
}
66

7+
// SAFETY: no additional safety requirements on `Project[Mut]::project[_mut]`.
78
unsafe impl<T> SafeProject for &mut MaybeUninit<T> {}
89

9-
unsafe impl<'a, T, F> ProjectMut<F> for &'a mut MaybeUninit<T>
10+
// No additional safety requirements for `project_mut`.
11+
impl<'a, T, F> ProjectMut<F> for &'a mut MaybeUninit<T>
1012
where
1113
F: Field<Base = T>,
1214
F::Type: Sized + 'a,

src/projections/non_null.rs

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

7-
unsafe impl<T, F> Project<F> for NonNull<T>
7+
// Additional safety requirements for `project`:
8+
// * The pointer pointed at by `this` must point to an allocated object at least as large as
9+
// `size_of::<T>()`.
10+
impl<T, F> Project<F> for NonNull<T>
811
where
912
F: UnalignedField<Base = T>,
1013
F::Type: Sized,

src/projections/pin.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ impl<T> Projectable for Pin<&mut T> {
44
type Inner = T;
55
}
66

7+
// SAFETY: no additional safety requirements on `Project[Mut]::project[_mut]`.
78
unsafe impl<T> SafeProject for Pin<&mut T> {}
89

9-
unsafe impl<'a, T, F> Project<F> for Pin<&'a mut T>
10+
// No additional safety requirements for `project_mut`.
11+
impl<'a, T, F> Project<F> for Pin<&'a mut T>
1012
where
1113
F: PinableField<Base = T> + Field<Base = T>,
1214
F::Type: Sized + 'a,
@@ -26,7 +28,8 @@ where
2628
}
2729
}
2830

29-
unsafe impl<'a, T, F> ProjectMut<F> for Pin<&'a mut T>
31+
// No additional safety requirements for `project_mut`.
32+
impl<'a, T, F> ProjectMut<F> for Pin<&'a mut T>
3033
where
3134
F: PinableField<Base = T> + Field<Base = T>,
3235
F::Type: Sized + 'a,

src/projections/raw_ptr.rs

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

5-
unsafe impl<T, F> Project<F> for *const T
5+
// Additional safety requirements for `project`:
6+
// * The pointer pointed at by `this` must point to an allocated object at least as large as
7+
// `size_of::<T>()`.
8+
impl<T, F> Project<F> for *const T
69
where
710
F: UnalignedField<Base = T>,
811
F::Type: Sized,
@@ -25,7 +28,10 @@ impl<T> Projectable for *mut T {
2528
type Inner = T;
2629
}
2730

28-
unsafe impl<T, F> Project<F> for *mut T
31+
// Additional safety requirements for `project`:
32+
// * The pointer pointed at by `this` must point to an allocated object at least as large as
33+
// `size_of::<T>()`.
34+
impl<T, F> Project<F> for *mut T
2935
where
3036
F: UnalignedField<Base = T>,
3137
F::Type: Sized,

src/projections/unsafe_cell.rs

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

7-
unsafe impl<'a, T, F> Project<F> for &'a UnsafeCell<T>
7+
// No additional safety requirements for `project_mut`.
8+
impl<'a, T, F> Project<F> for &'a UnsafeCell<T>
89
where
910
F: Field<Base = T>,
1011
F::Type: 'a + Sized,

0 commit comments

Comments
 (0)