@@ -2495,11 +2495,57 @@ impl<T: ?Sized> Eq for *mut T {}
2495
2495
/// let other_five_ref = &other_five;
2496
2496
///
2497
2497
/// assert!(five_ref == same_five_ref);
2498
- /// assert!(five_ref == other_five_ref);
2499
- ///
2500
2498
/// assert!(ptr::eq(five_ref, same_five_ref));
2499
+ ///
2500
+ /// assert!(five_ref == other_five_ref);
2501
2501
/// assert!(!ptr::eq(five_ref, other_five_ref));
2502
2502
/// ```
2503
+ ///
2504
+ /// Slices are also compared by their length (fat pointers):
2505
+ ///
2506
+ /// ```
2507
+ /// let a = [1, 2, 3];
2508
+ /// assert!(std::ptr::eq(&a[..3], &a[..3]));
2509
+ /// assert!(!std::ptr::eq(&a[..2], &a[..3]));
2510
+ /// assert!(!std::ptr::eq(&a[0..2], &a[1..3]));
2511
+ /// ```
2512
+ ///
2513
+ /// Traits are also compared by their implementation:
2514
+ ///
2515
+ /// ```
2516
+ /// #[repr(transparent)]
2517
+ /// struct Wrapper { member: i32 }
2518
+ ///
2519
+ /// trait Trait {}
2520
+ /// impl Trait for Wrapper {}
2521
+ /// impl Trait for i32 {}
2522
+ ///
2523
+ /// fn main() {
2524
+ /// let wrapper = Wrapper { member: 10 };
2525
+ ///
2526
+ /// // Pointers have equal addresses.
2527
+ /// assert!(std::ptr::eq(
2528
+ /// &wrapper as *const Wrapper as *const u8,
2529
+ /// &wrapper.member as *const i32 as *const u8
2530
+ /// ));
2531
+ ///
2532
+ /// // Objects have equal addresses, but `Trait` has different implementations.
2533
+ /// assert!(!std::ptr::eq(
2534
+ /// &wrapper as &dyn Trait,
2535
+ /// &wrapper.member as &dyn Trait,
2536
+ /// ));
2537
+ /// assert!(!std::ptr::eq(
2538
+ /// &wrapper as &dyn Trait as *const dyn Trait,
2539
+ /// &wrapper.member as &dyn Trait as *const dyn Trait,
2540
+ /// ));
2541
+ ///
2542
+ /// // Converting the reference to a `*const u8` compares by address.
2543
+ /// assert!(std::ptr::eq(
2544
+ /// &wrapper as &dyn Trait as *const dyn Trait as *const u8,
2545
+ /// &wrapper.member as &dyn Trait as *const dyn Trait as *const u8,
2546
+ /// ));
2547
+ /// }
2548
+ /// ```
2503
2549
#[ stable( feature = "ptr_eq" , since = "1.17.0" ) ]
2504
2550
#[ inline]
2505
2551
pub fn eq < T : ?Sized > ( a : * const T , b : * const T ) -> bool {
0 commit comments