Skip to content

Commit de477ed

Browse files
committed
Rollup merge of rust-lang#32956 - GuillaumeGomez:ptr_examples, r=steveklabnik
Add examples for std::ptr module functions Part of rust-lang#29371. r? @steveklabnik
2 parents e59af6c + fbcf935 commit de477ed

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/libcore/ptr.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T {
119119
/// `src` is not used before the data is overwritten again (e.g. with `write`,
120120
/// `zero_memory`, or `copy_memory`). Note that `*src = foo` counts as a use
121121
/// because it will attempt to drop the value previously at `*src`.
122+
///
123+
/// # Examples
124+
///
125+
/// Basic usage:
126+
///
127+
/// ```
128+
/// let x = 12;
129+
/// let y = &x as *const i32;
130+
///
131+
/// unsafe { println!("{}", std::ptr::read(y)); }
132+
/// ```
122133
#[inline(always)]
123134
#[stable(feature = "rust1", since = "1.0.0")]
124135
pub unsafe fn read<T>(src: *const T) -> T {
@@ -155,6 +166,21 @@ pub unsafe fn read_and_drop<T>(dest: *mut T) -> T {
155166
///
156167
/// This is appropriate for initializing uninitialized memory, or overwriting
157168
/// memory that has previously been `read` from.
169+
///
170+
/// # Examples
171+
///
172+
/// Basic usage:
173+
///
174+
/// ```
175+
/// let mut x = 0;
176+
/// let y = &mut x as *mut i32;
177+
/// let z = 12;
178+
///
179+
/// unsafe {
180+
/// std::ptr::write(y, z);
181+
/// println!("{}", std::ptr::read(y));
182+
/// }
183+
/// ```
158184
#[inline]
159185
#[stable(feature = "rust1", since = "1.0.0")]
160186
pub unsafe fn write<T>(dst: *mut T, src: T) {
@@ -185,6 +211,17 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
185211
/// `src` is not used before the data is overwritten again (e.g. with `write`,
186212
/// `zero_memory`, or `copy_memory`). Note that `*src = foo` counts as a use
187213
/// because it will attempt to drop the value previously at `*src`.
214+
///
215+
/// # Examples
216+
///
217+
/// Basic usage:
218+
///
219+
/// ```
220+
/// let x = 12;
221+
/// let y = &x as *const i32;
222+
///
223+
/// unsafe { println!("{}", std::ptr::read_volatile(y)); }
224+
/// ```
188225
#[inline]
189226
#[stable(feature = "volatile", since = "1.9.0")]
190227
pub unsafe fn read_volatile<T>(src: *const T) -> T {
@@ -217,6 +254,21 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
217254
///
218255
/// This is appropriate for initializing uninitialized memory, or overwriting
219256
/// memory that has previously been `read` from.
257+
///
258+
/// # Examples
259+
///
260+
/// Basic usage:
261+
///
262+
/// ```
263+
/// let mut x = 0;
264+
/// let y = &mut x as *mut i32;
265+
/// let z = 12;
266+
///
267+
/// unsafe {
268+
/// std::ptr::write_volatile(y, z);
269+
/// println!("{}", std::ptr::read_volatile(y));
270+
/// }
271+
/// ```
220272
#[inline]
221273
#[stable(feature = "volatile", since = "1.9.0")]
222274
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {

0 commit comments

Comments
 (0)