diff options
| author | Josh Stone <cuviper@gmail.com> | 2019-03-27 18:15:30 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-03-27 18:15:30 -0700 |
| commit | 35b339bd5fbf678dd319c62ea1316a5d1160f0df (patch) | |
| tree | ae77b7d02742a5b147a83094fed874c01598efb1 | |
| parent | a4bf8557b2b3c134c9c1d449f86dd38b842b43b1 (diff) | |
| parent | 61b6c56f50af6ca2848f4bd623c8b2cd2b24cb77 (diff) | |
| download | rust-35b339bd5fbf678dd319c62ea1316a5d1160f0df.tar.gz rust-35b339bd5fbf678dd319c62ea1316a5d1160f0df.zip | |
Rollup merge of #59390 - czipperz:ptr_eq_smart_pointer, r=Centril,steveklabnik
Make `ptr::eq` documentation mention fat-pointer behavior Resolves #59214
| -rw-r--r-- | src/libcore/ptr.rs | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index c29da758b34..dabf914fdb2 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -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 have equal addresses. +/// 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 &dyn Trait, +/// &wrapper.member as &dyn Trait, +/// )); +/// assert!(!std::ptr::eq( +/// &wrapper as &dyn Trait as *const dyn Trait, +/// &wrapper.member as &dyn Trait as *const dyn Trait, +/// )); +/// +/// // Converting the reference to a `*const u8` compares by address. +/// assert!(std::ptr::eq( +/// &wrapper as &dyn Trait as *const dyn Trait as *const u8, +/// &wrapper.member as &dyn Trait as *const dyn 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 { |
