Skip to content

Commit fbcf935

Browse files
Add examples for std::ptr module functions
1 parent 772c600 commit fbcf935

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/libcore/ptr.rs

+52
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) {
@@ -178,6 +204,17 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
178204
/// `src` is not used before the data is overwritten again (e.g. with `write`,
179205
/// `zero_memory`, or `copy_memory`). Note that `*src = foo` counts as a use
180206
/// because it will attempt to drop the value previously at `*src`.
207+
///
208+
/// # Examples
209+
///
210+
/// Basic usage:
211+
///
212+
/// ```
213+
/// let x = 12;
214+
/// let y = &x as *const i32;
215+
///
216+
/// unsafe { println!("{}", std::ptr::read_volatile(y)); }
217+
/// ```
181218
#[inline]
182219
#[unstable(feature = "volatile", reason = "recently added", issue = "31756")]
183220
pub unsafe fn read_volatile<T>(src: *const T) -> T {
@@ -203,6 +240,21 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
203240
///
204241
/// This is appropriate for initializing uninitialized memory, or overwriting
205242
/// memory that has previously been `read` from.
243+
///
244+
/// # Examples
245+
///
246+
/// Basic usage:
247+
///
248+
/// ```
249+
/// let mut x = 0;
250+
/// let y = &mut x as *mut i32;
251+
/// let z = 12;
252+
///
253+
/// unsafe {
254+
/// std::ptr::write_volatile(y, z);
255+
/// println!("{}", std::ptr::read_volatile(y));
256+
/// }
257+
/// ```
206258
#[inline]
207259
#[unstable(feature = "volatile", reason = "recently added", issue = "31756")]
208260
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {

0 commit comments

Comments
 (0)