Skip to content

Commit f220854

Browse files
committed
Document the constructor functions
1 parent 16dcea3 commit f220854

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

src/volatile_ref.rs

+54-7
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,47 @@ impl<'a, T> VolatileRef<'a, T>
4141
where
4242
T: ?Sized,
4343
{
44+
/// Turns the given pointer into a `VolatileRef`.
45+
///
46+
/// ## Safety
47+
///
48+
/// - The pointer must be properly aligned.
49+
/// - It must be “dereferenceable” in the sense defined in the [`core::ptr`] documentation.
50+
/// - The pointer must point to an initialized instance of T.
51+
/// - You must enforce Rust’s aliasing rules, since the returned lifetime 'a is arbitrarily
52+
/// chosen and does not necessarily reflect the actual lifetime of the data. In particular,
53+
/// while this `VolatileRef` exists, the memory the pointer points to must not get accessed
54+
/// (_read or written_) through any other pointer.
4455
pub unsafe fn new(pointer: NonNull<T>) -> Self {
4556
unsafe { VolatileRef::new_restricted(ReadWrite, pointer) }
4657
}
4758

48-
pub fn from_mut_ref(reference: &'a mut T) -> Self
49-
where
50-
T: 'a,
51-
{
52-
unsafe { VolatileRef::new(reference.into()) }
53-
}
54-
59+
/// Turns the given pointer into a read-only `VolatileRef`.
60+
///
61+
/// ## Safety
62+
///
63+
/// - The pointer must be properly aligned.
64+
/// - It must be “dereferenceable” in the sense defined in the [`core::ptr`] documentation.
65+
/// - The pointer must point to an initialized instance of T.
66+
/// - You must enforce Rust’s aliasing rules, since the returned lifetime 'a is arbitrarily
67+
/// chosen and does not necessarily reflect the actual lifetime of the data. In particular,
68+
/// while this `VolatileRef` exists, the memory the pointer points to _must not get mutated_.
5569
pub const unsafe fn new_read_only(pointer: NonNull<T>) -> VolatileRef<'a, T, ReadOnly> {
5670
unsafe { Self::new_restricted(ReadOnly, pointer) }
5771
}
5872

73+
/// Turns the given pointer into a `VolatileRef` instance with the given access.
74+
///
75+
/// ## Safety
76+
///
77+
/// - The pointer must be properly aligned.
78+
/// - It must be “dereferenceable” in the sense defined in the [`core::ptr`] documentation.
79+
/// - The pointer must point to an initialized instance of T.
80+
/// - You must enforce Rust’s aliasing rules, since the returned lifetime 'a is arbitrarily
81+
/// chosen and does not necessarily reflect the actual lifetime of the data. In particular,
82+
/// while this `VolatileRef` exists, the memory the pointer points to _must not get mutated_.
83+
/// If the given `access` parameter allows write access, the pointer _must not get read
84+
/// either_ while this `VolatileRef` exists.
5985
pub const unsafe fn new_restricted<A>(access: A, pointer: NonNull<T>) -> VolatileRef<'a, T, A>
6086
where
6187
A: Access,
@@ -64,13 +90,34 @@ where
6490
unsafe { Self::new_generic(pointer) }
6591
}
6692

93+
/// Creates a `VolatileRef` from the given shared reference.
94+
///
95+
/// **Note:** This function is only intended for testing, not for accessing real volatile
96+
/// data. The reason is that the `&mut T` argument is considered _dereferenceable_ by Rust,
97+
/// so the compiler is allowed to insert non-volatile reads. This might lead to undesired
98+
/// (or even undefined?) behavior when accessing volatile data. So to be safe, only create
99+
/// raw pointers to volatile data and use the [`Self::new`] constructor instead.
67100
pub fn from_ref(reference: &'a T) -> VolatileRef<'a, T, ReadOnly>
68101
where
69102
T: 'a,
70103
{
71104
unsafe { VolatileRef::new_restricted(ReadOnly, reference.into()) }
72105
}
73106

107+
/// Creates a `VolatileRef` from the given mutable reference.
108+
///
109+
/// **Note:** This function is only intended for testing, not for accessing real volatile
110+
/// data. The reason is that the `&mut T` argument is considered _dereferenceable_ by Rust,
111+
/// so the compiler is allowed to insert non-volatile reads. This might lead to undesired
112+
/// (or even undefined?) behavior when accessing volatile data. So to be safe, only create
113+
/// raw pointers to volatile data and use the [`Self::new`] constructor instead.
114+
pub fn from_mut_ref(reference: &'a mut T) -> Self
115+
where
116+
T: 'a,
117+
{
118+
unsafe { VolatileRef::new(reference.into()) }
119+
}
120+
74121
const unsafe fn new_generic<A>(pointer: NonNull<T>) -> VolatileRef<'a, T, A> {
75122
VolatileRef {
76123
pointer,

0 commit comments

Comments
 (0)