Skip to content

Commit

Permalink
Add doc examples on pointer types
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Mar 28, 2016
1 parent 641204a commit 5b08ab5
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
#[lang = "const_ptr"]
impl<T: ?Sized> *const T {
/// Returns true if the pointer is null.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let s: &str = "Follow the rabbit";
/// let ptr: *const u8 = s.as_ptr();
/// assert!(ptr.is_null() == false);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_null(self) -> bool where T: Sized {
Expand All @@ -227,6 +237,20 @@ impl<T: ?Sized> *const T {
/// null-safety, it is important to note that this is still an unsafe
/// operation because the returned value could be pointing to invalid
/// memory.
///
/// # Examples
///
/// Basic usage:
///
/// ```ignore
/// let val: *const u8 = &10u8 as *const u8;
///
/// unsafe {
/// if let Some(val_back) = val.as_ref() {
/// println!("We got back the value: {}!", val_back);
/// }
/// }
/// ```
#[unstable(feature = "ptr_as_ref",
reason = "Option is not clearly the right return type, and we \
may want to tie the return lifetime to a borrow of \
Expand All @@ -250,6 +274,20 @@ impl<T: ?Sized> *const T {
/// byte past the end of an allocated object. If either pointer is out of
/// bounds or arithmetic overflow occurs then
/// any further use of the returned value will result in undefined behavior.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let s: &str = "123";
/// let ptr: *const u8 = s.as_ptr();
///
/// unsafe {
/// println!("{}", *ptr.offset(1) as char);
/// println!("{}", *ptr.offset(2) as char);
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub unsafe fn offset(self, count: isize) -> *const T where T: Sized {
Expand All @@ -260,6 +298,16 @@ impl<T: ?Sized> *const T {
#[lang = "mut_ptr"]
impl<T: ?Sized> *mut T {
/// Returns true if the pointer is null.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let mut s = [1, 2, 3];
/// let ptr: *mut u32 = s.as_mut_ptr();
/// assert!(ptr.is_null() == false);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_null(self) -> bool where T: Sized {
Expand All @@ -275,6 +323,20 @@ impl<T: ?Sized> *mut T {
/// null-safety, it is important to note that this is still an unsafe
/// operation because the returned value could be pointing to invalid
/// memory.
///
/// # Examples
///
/// Basic usage:
///
/// ```ignore
/// let val: *mut u8 = &mut 10u8 as *mut u8;
///
/// unsafe {
/// if let Some(val_back) = val.as_ref() {
/// println!("We got back the value: {}!", val_back);
/// }
/// }
/// ```
#[unstable(feature = "ptr_as_ref",
reason = "Option is not clearly the right return type, and we \
may want to tie the return lifetime to a borrow of \
Expand All @@ -297,6 +359,20 @@ impl<T: ?Sized> *mut T {
/// The offset must be in-bounds of the object, or one-byte-past-the-end.
/// Otherwise `offset` invokes Undefined Behavior, regardless of whether
/// the pointer is used.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let mut s = [1, 2, 3];
/// let ptr: *mut u32 = s.as_mut_ptr();
///
/// unsafe {
/// println!("{}", *ptr.offset(1));
/// println!("{}", *ptr.offset(2));
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub unsafe fn offset(self, count: isize) -> *mut T where T: Sized {
Expand All @@ -310,6 +386,15 @@ impl<T: ?Sized> *mut T {
///
/// As with `as_ref`, this is unsafe because it cannot verify the validity
/// of the returned pointer.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let mut s = [1, 2, 3];
/// let ptr: *mut u32 = s.as_mut_ptr();
/// ```
#[unstable(feature = "ptr_as_ref",
reason = "return value does not necessarily convey all possible \
information",
Expand Down

0 comments on commit 5b08ab5

Please sign in to comment.