Skip to content

Commit 70b8b94

Browse files
committed
Auto merge of #32581 - GuillaumeGomez:patch-3, r=steveklabnik
Add doc examples on pointer types r? @steveklabnik Fixes #29336
2 parents a48c9a1 + 5b08ab5 commit 70b8b94

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

src/libcore/ptr.rs

+85
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
212212
#[lang = "const_ptr"]
213213
impl<T: ?Sized> *const T {
214214
/// Returns true if the pointer is null.
215+
///
216+
/// # Examples
217+
///
218+
/// Basic usage:
219+
///
220+
/// ```
221+
/// let s: &str = "Follow the rabbit";
222+
/// let ptr: *const u8 = s.as_ptr();
223+
/// assert!(ptr.is_null() == false);
224+
/// ```
215225
#[stable(feature = "rust1", since = "1.0.0")]
216226
#[inline]
217227
pub fn is_null(self) -> bool where T: Sized {
@@ -227,6 +237,20 @@ impl<T: ?Sized> *const T {
227237
/// null-safety, it is important to note that this is still an unsafe
228238
/// operation because the returned value could be pointing to invalid
229239
/// memory.
240+
///
241+
/// # Examples
242+
///
243+
/// Basic usage:
244+
///
245+
/// ```ignore
246+
/// let val: *const u8 = &10u8 as *const u8;
247+
///
248+
/// unsafe {
249+
/// if let Some(val_back) = val.as_ref() {
250+
/// println!("We got back the value: {}!", val_back);
251+
/// }
252+
/// }
253+
/// ```
230254
#[unstable(feature = "ptr_as_ref",
231255
reason = "Option is not clearly the right return type, and we \
232256
may want to tie the return lifetime to a borrow of \
@@ -250,6 +274,20 @@ impl<T: ?Sized> *const T {
250274
/// byte past the end of an allocated object. If either pointer is out of
251275
/// bounds or arithmetic overflow occurs then
252276
/// any further use of the returned value will result in undefined behavior.
277+
///
278+
/// # Examples
279+
///
280+
/// Basic usage:
281+
///
282+
/// ```
283+
/// let s: &str = "123";
284+
/// let ptr: *const u8 = s.as_ptr();
285+
///
286+
/// unsafe {
287+
/// println!("{}", *ptr.offset(1) as char);
288+
/// println!("{}", *ptr.offset(2) as char);
289+
/// }
290+
/// ```
253291
#[stable(feature = "rust1", since = "1.0.0")]
254292
#[inline]
255293
pub unsafe fn offset(self, count: isize) -> *const T where T: Sized {
@@ -260,6 +298,16 @@ impl<T: ?Sized> *const T {
260298
#[lang = "mut_ptr"]
261299
impl<T: ?Sized> *mut T {
262300
/// Returns true if the pointer is null.
301+
///
302+
/// # Examples
303+
///
304+
/// Basic usage:
305+
///
306+
/// ```
307+
/// let mut s = [1, 2, 3];
308+
/// let ptr: *mut u32 = s.as_mut_ptr();
309+
/// assert!(ptr.is_null() == false);
310+
/// ```
263311
#[stable(feature = "rust1", since = "1.0.0")]
264312
#[inline]
265313
pub fn is_null(self) -> bool where T: Sized {
@@ -275,6 +323,20 @@ impl<T: ?Sized> *mut T {
275323
/// null-safety, it is important to note that this is still an unsafe
276324
/// operation because the returned value could be pointing to invalid
277325
/// memory.
326+
///
327+
/// # Examples
328+
///
329+
/// Basic usage:
330+
///
331+
/// ```ignore
332+
/// let val: *mut u8 = &mut 10u8 as *mut u8;
333+
///
334+
/// unsafe {
335+
/// if let Some(val_back) = val.as_ref() {
336+
/// println!("We got back the value: {}!", val_back);
337+
/// }
338+
/// }
339+
/// ```
278340
#[unstable(feature = "ptr_as_ref",
279341
reason = "Option is not clearly the right return type, and we \
280342
may want to tie the return lifetime to a borrow of \
@@ -297,6 +359,20 @@ impl<T: ?Sized> *mut T {
297359
/// The offset must be in-bounds of the object, or one-byte-past-the-end.
298360
/// Otherwise `offset` invokes Undefined Behavior, regardless of whether
299361
/// the pointer is used.
362+
///
363+
/// # Examples
364+
///
365+
/// Basic usage:
366+
///
367+
/// ```
368+
/// let mut s = [1, 2, 3];
369+
/// let ptr: *mut u32 = s.as_mut_ptr();
370+
///
371+
/// unsafe {
372+
/// println!("{}", *ptr.offset(1));
373+
/// println!("{}", *ptr.offset(2));
374+
/// }
375+
/// ```
300376
#[stable(feature = "rust1", since = "1.0.0")]
301377
#[inline]
302378
pub unsafe fn offset(self, count: isize) -> *mut T where T: Sized {
@@ -310,6 +386,15 @@ impl<T: ?Sized> *mut T {
310386
///
311387
/// As with `as_ref`, this is unsafe because it cannot verify the validity
312388
/// of the returned pointer.
389+
///
390+
/// # Examples
391+
///
392+
/// Basic usage:
393+
///
394+
/// ```
395+
/// let mut s = [1, 2, 3];
396+
/// let ptr: *mut u32 = s.as_mut_ptr();
397+
/// ```
313398
#[unstable(feature = "ptr_as_ref",
314399
reason = "return value does not necessarily convey all possible \
315400
information",

0 commit comments

Comments
 (0)