Skip to content

Commit 380dd7d

Browse files
committed
Add rc::Weak.ptr_eq
1 parent 6a2d1b4 commit 380dd7d

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/liballoc/rc.rs

+46
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,52 @@ impl<T: ?Sized> Weak<T> {
12601260
Some(unsafe { self.ptr.as_ref() })
12611261
}
12621262
}
1263+
1264+
/// Returns true if the two `Weak`s point to the same value (not just values
1265+
/// that compare as equal).
1266+
///
1267+
/// # Notes
1268+
///
1269+
/// Since this compares pointers it means that `Weak::new()` will equal each
1270+
/// other, even though they don't point to any value.
1271+
///
1272+
/// # Examples
1273+
///
1274+
/// ```
1275+
/// #![feature(weak_ptr_eq)]
1276+
/// use std::rc::{Rc, Weak};
1277+
///
1278+
/// let first_rc = Rc::new(5);
1279+
/// let first = Rc::downgrade(&first_rc);
1280+
/// let second = Rc::downgrade(&first_rc);
1281+
///
1282+
/// assert!(Weak::ptr_eq(&first, &second));
1283+
///
1284+
/// let third_rc = Rc::new(5);
1285+
/// let third = Rc::downgrade(&third_rc);
1286+
///
1287+
/// assert!(!Weak::ptr_eq(&first, &third));
1288+
/// ```
1289+
///
1290+
/// Comparing `Weak::new`.
1291+
///
1292+
/// ```
1293+
/// #![feature(weak_ptr_eq)]
1294+
/// use std::rc::{Rc, Weak};
1295+
///
1296+
/// let first = Weak::new();
1297+
/// let second = Weak::new();
1298+
/// assert!(Weak::ptr_eq(&first, &second));
1299+
///
1300+
/// let third_rc = Rc::new(());
1301+
/// let third = Rc::downgrade(&third_rc);
1302+
/// assert!(!Weak::ptr_eq(&first, &third));
1303+
/// ```
1304+
#[inline]
1305+
#[unstable(feature = "weak_ptr_eq", issue = "55981")]
1306+
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
1307+
this.ptr.as_ptr() == other.ptr.as_ptr()
1308+
}
12631309
}
12641310

12651311
#[stable(feature = "rc_weak", since = "1.4.0")]

0 commit comments

Comments
 (0)