@@ -41,21 +41,47 @@ impl<'a, T> VolatileRef<'a, T>
41
41
where
42
42
T : ?Sized ,
43
43
{
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.
44
55
pub unsafe fn new ( pointer : NonNull < T > ) -> Self {
45
56
unsafe { VolatileRef :: new_restricted ( ReadWrite , pointer) }
46
57
}
47
58
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_.
55
69
pub const unsafe fn new_read_only ( pointer : NonNull < T > ) -> VolatileRef < ' a , T , ReadOnly > {
56
70
unsafe { Self :: new_restricted ( ReadOnly , pointer) }
57
71
}
58
72
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.
59
85
pub const unsafe fn new_restricted < A > ( access : A , pointer : NonNull < T > ) -> VolatileRef < ' a , T , A >
60
86
where
61
87
A : Access ,
@@ -64,13 +90,34 @@ where
64
90
unsafe { Self :: new_generic ( pointer) }
65
91
}
66
92
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.
67
100
pub fn from_ref ( reference : & ' a T ) -> VolatileRef < ' a , T , ReadOnly >
68
101
where
69
102
T : ' a ,
70
103
{
71
104
unsafe { VolatileRef :: new_restricted ( ReadOnly , reference. into ( ) ) }
72
105
}
73
106
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
+
74
121
const unsafe fn new_generic < A > ( pointer : NonNull < T > ) -> VolatileRef < ' a , T , A > {
75
122
VolatileRef {
76
123
pointer,
0 commit comments