Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ptr::eq documentation mention fat-pointer behavior #59390

Merged
merged 4 commits into from
Mar 28, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2495,11 +2495,57 @@ impl<T: ?Sized> Eq for *mut T {}
/// let other_five_ref = &other_five;
///
/// assert!(five_ref == same_five_ref);
/// assert!(five_ref == other_five_ref);
///
/// assert!(ptr::eq(five_ref, same_five_ref));
///
/// 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
Centril marked this conversation as resolved.
Show resolved Hide resolved
/// 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
Centril marked this conversation as resolved.
Show resolved Hide resolved
/// assert!(!std::ptr::eq(
/// &wrapper as &Trait,
Centril marked this conversation as resolved.
Show resolved Hide resolved
/// &wrapper.member as &Trait,
Centril marked this conversation as resolved.
Show resolved Hide resolved
/// ));
/// assert!(!std::ptr::eq(
/// &wrapper as &Trait as *const Trait,
Centril marked this conversation as resolved.
Show resolved Hide resolved
/// &wrapper.member as &Trait as *const Trait,
Centril marked this conversation as resolved.
Show resolved Hide resolved
/// ));
///
/// // Converting the reference to a `*const u8` compares by address
Centril marked this conversation as resolved.
Show resolved Hide resolved
/// assert!(std::ptr::eq(
/// &wrapper as &Trait as *const Trait as *const u8,
Centril marked this conversation as resolved.
Show resolved Hide resolved
/// &wrapper.member as &Trait as *const Trait as *const u8,
Centril marked this conversation as resolved.
Show resolved Hide resolved
/// ));
/// }
/// ```
#[stable(feature = "ptr_eq", since = "1.17.0")]
#[inline]
pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
Expand Down