Skip to content

Commit 8a5cb26

Browse files
authored
Rollup merge of rust-lang#55987 - Thomasdezeeuw:weak-ptr_eq, r=sfackler
Add Weak.ptr_eq I hope the doc tests alone are good enough. We also might want to discuss the dangling pointer case (from `Weak::new()`). Updates rust-lang#55981.
2 parents 1bca455 + 38e21f9 commit 8a5cb26

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

src/liballoc/rc.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -1187,8 +1187,9 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Weak<U>> for Weak<T> {}
11871187

11881188
impl<T> Weak<T> {
11891189
/// Constructs a new `Weak<T>`, without allocating any memory.
1190-
/// Calling [`upgrade`][Weak::upgrade] on the return value always gives [`None`].
1190+
/// Calling [`upgrade`] on the return value always gives [`None`].
11911191
///
1192+
/// [`upgrade`]: #method.upgrade
11921193
/// [`None`]: ../../std/option/enum.Option.html
11931194
///
11941195
/// # Examples
@@ -1260,6 +1261,52 @@ impl<T: ?Sized> Weak<T> {
12601261
Some(unsafe { self.ptr.as_ref() })
12611262
}
12621263
}
1264+
1265+
/// Returns true if the two `Weak`s point to the same value (not just values
1266+
/// that compare as equal).
1267+
///
1268+
/// # Notes
1269+
///
1270+
/// Since this compares pointers it means that `Weak::new()` will equal each
1271+
/// other, even though they don't point to any value.
1272+
///
1273+
/// # Examples
1274+
///
1275+
/// ```
1276+
/// #![feature(weak_ptr_eq)]
1277+
/// use std::rc::{Rc, Weak};
1278+
///
1279+
/// let first_rc = Rc::new(5);
1280+
/// let first = Rc::downgrade(&first_rc);
1281+
/// let second = Rc::downgrade(&first_rc);
1282+
///
1283+
/// assert!(Weak::ptr_eq(&first, &second));
1284+
///
1285+
/// let third_rc = Rc::new(5);
1286+
/// let third = Rc::downgrade(&third_rc);
1287+
///
1288+
/// assert!(!Weak::ptr_eq(&first, &third));
1289+
/// ```
1290+
///
1291+
/// Comparing `Weak::new`.
1292+
///
1293+
/// ```
1294+
/// #![feature(weak_ptr_eq)]
1295+
/// use std::rc::{Rc, Weak};
1296+
///
1297+
/// let first = Weak::new();
1298+
/// let second = Weak::new();
1299+
/// assert!(Weak::ptr_eq(&first, &second));
1300+
///
1301+
/// let third_rc = Rc::new(());
1302+
/// let third = Rc::downgrade(&third_rc);
1303+
/// assert!(!Weak::ptr_eq(&first, &third));
1304+
/// ```
1305+
#[inline]
1306+
#[unstable(feature = "weak_ptr_eq", issue = "55981")]
1307+
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
1308+
this.ptr.as_ptr() == other.ptr.as_ptr()
1309+
}
12631310
}
12641311

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

src/liballoc/sync.rs

+47
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,53 @@ impl<T: ?Sized> Weak<T> {
11301130
Some(unsafe { self.ptr.as_ref() })
11311131
}
11321132
}
1133+
1134+
/// Returns true if the two `Weak`s point to the same value (not just values
1135+
/// that compare as equal).
1136+
///
1137+
/// # Notes
1138+
///
1139+
/// Since this compares pointers it means that `Weak::new()` will equal each
1140+
/// other, even though they don't point to any value.
1141+
///
1142+
///
1143+
/// # Examples
1144+
///
1145+
/// ```
1146+
/// #![feature(weak_ptr_eq)]
1147+
/// use std::sync::{Arc, Weak};
1148+
///
1149+
/// let first_rc = Arc::new(5);
1150+
/// let first = Arc::downgrade(&first_rc);
1151+
/// let second = Arc::downgrade(&first_rc);
1152+
///
1153+
/// assert!(Weak::ptr_eq(&first, &second));
1154+
///
1155+
/// let third_rc = Arc::new(5);
1156+
/// let third = Arc::downgrade(&third_rc);
1157+
///
1158+
/// assert!(!Weak::ptr_eq(&first, &third));
1159+
/// ```
1160+
///
1161+
/// Comparing `Weak::new`.
1162+
///
1163+
/// ```
1164+
/// #![feature(weak_ptr_eq)]
1165+
/// use std::sync::{Arc, Weak};
1166+
///
1167+
/// let first = Weak::new();
1168+
/// let second = Weak::new();
1169+
/// assert!(Weak::ptr_eq(&first, &second));
1170+
///
1171+
/// let third_rc = Arc::new(());
1172+
/// let third = Arc::downgrade(&third_rc);
1173+
/// assert!(!Weak::ptr_eq(&first, &third));
1174+
/// ```
1175+
#[inline]
1176+
#[unstable(feature = "weak_ptr_eq", issue = "55981")]
1177+
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
1178+
this.ptr.as_ptr() == other.ptr.as_ptr()
1179+
}
11331180
}
11341181

11351182
#[stable(feature = "arc_weak", since = "1.4.0")]

0 commit comments

Comments
 (0)