@@ -212,6 +212,16 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
212
212
#[ lang = "const_ptr" ]
213
213
impl < T : ?Sized > * const T {
214
214
/// 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
+ /// ```
215
225
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
216
226
#[ inline]
217
227
pub fn is_null ( self ) -> bool where T : Sized {
@@ -227,6 +237,20 @@ impl<T: ?Sized> *const T {
227
237
/// null-safety, it is important to note that this is still an unsafe
228
238
/// operation because the returned value could be pointing to invalid
229
239
/// 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
+ /// ```
230
254
#[ unstable( feature = "ptr_as_ref" ,
231
255
reason = "Option is not clearly the right return type, and we \
232
256
may want to tie the return lifetime to a borrow of \
@@ -250,6 +274,20 @@ impl<T: ?Sized> *const T {
250
274
/// byte past the end of an allocated object. If either pointer is out of
251
275
/// bounds or arithmetic overflow occurs then
252
276
/// 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
+ /// ```
253
291
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
254
292
#[ inline]
255
293
pub unsafe fn offset ( self , count : isize ) -> * const T where T : Sized {
@@ -260,6 +298,16 @@ impl<T: ?Sized> *const T {
260
298
#[ lang = "mut_ptr" ]
261
299
impl < T : ?Sized > * mut T {
262
300
/// 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
+ /// ```
263
311
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
264
312
#[ inline]
265
313
pub fn is_null ( self ) -> bool where T : Sized {
@@ -275,6 +323,20 @@ impl<T: ?Sized> *mut T {
275
323
/// null-safety, it is important to note that this is still an unsafe
276
324
/// operation because the returned value could be pointing to invalid
277
325
/// 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
+ /// ```
278
340
#[ unstable( feature = "ptr_as_ref" ,
279
341
reason = "Option is not clearly the right return type, and we \
280
342
may want to tie the return lifetime to a borrow of \
@@ -297,6 +359,20 @@ impl<T: ?Sized> *mut T {
297
359
/// The offset must be in-bounds of the object, or one-byte-past-the-end.
298
360
/// Otherwise `offset` invokes Undefined Behavior, regardless of whether
299
361
/// 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
+ /// ```
300
376
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
301
377
#[ inline]
302
378
pub unsafe fn offset ( self , count : isize ) -> * mut T where T : Sized {
@@ -310,6 +386,15 @@ impl<T: ?Sized> *mut T {
310
386
///
311
387
/// As with `as_ref`, this is unsafe because it cannot verify the validity
312
388
/// 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
+ /// ```
313
398
#[ unstable( feature = "ptr_as_ref" ,
314
399
reason = "return value does not necessarily convey all possible \
315
400
information",
0 commit comments