Skip to content

Commit

Permalink
Rework documentation into examples
Browse files Browse the repository at this point in the history
  • Loading branch information
czipperz committed Mar 25, 2019
1 parent e91689c commit fbfc808
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2483,14 +2483,6 @@ impl<T: ?Sized> Eq for *mut T {}
/// by their address rather than comparing the values they point to
/// (which is what the `PartialEq for &T` implementation does).
///
/// A reference in Rust is sometimes stored different than a raw
/// memory address. These cases are called fat pointers. A reference
/// to a slice must store both the address of the slice and the length
/// of the slice. A reference to an object satisfying a trait must
/// also point to the vtable for the trait's methods. Since this
/// function compares pointers in totality, careful consideration to
/// the type of the variable must be made.
///
/// # Examples
///
/// ```
Expand All @@ -2508,6 +2500,52 @@ impl<T: ?Sized> Eq for *mut T {}
/// assert!(five_ref == other_five_ref);
/// assert!(!ptr::eq(five_ref, other_five_ref));
/// ```
///
/// Slices are also compared by their length (fat pointers):
///
/// ```
/// let a = [1, 2, 3];
/// assert!(std::ptr::eq(&a[..3], &a[..3]));
/// assert!(!std::ptr::eq(&a[..2], &a[..3]));
/// assert!(!std::ptr::eq(&a[0..2], &a[1..3]));
/// ```
///
/// Traits are also compared by their implementation:
///
/// ```
/// #[repr(transparent)]
/// struct Wrapper { member: i32 }
///
/// trait Trait {}
/// impl Trait for Wrapper {}
/// impl Trait for i32 {}
///
/// fn main() {
/// let wrapper = Wrapper { member: 10 };
///
/// // Pointers are equal address
/// assert!(std::ptr::eq(
/// &wrapper as *const Wrapper as *const u8,
/// &wrapper.member as *const i32 as *const u8
/// ));
///
/// // Objects have equal addresses, but `Trait` has different implementations
/// assert!(!std::ptr::eq(
/// &wrapper as &Trait,
/// &wrapper.member as &Trait,
/// ));
/// assert!(!std::ptr::eq(
/// &wrapper as &Trait as *const Trait,
/// &wrapper.member as &Trait as *const Trait,
/// ));
///
/// // Converting the reference to a `*const u8` compares by address
/// assert!(std::ptr::eq(
/// &wrapper as &Trait as *const Trait as *const u8,
/// &wrapper.member as &Trait as *const Trait as *const u8,
/// ));
/// }
/// ```
#[stable(feature = "ptr_eq", since = "1.17.0")]
#[inline]
pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
Expand Down

0 comments on commit fbfc808

Please sign in to comment.