Skip to content

Commit e2b05ea

Browse files
authored
Rollup merge of rust-lang#94991 - CAD97:const-weak-new, r=dtolnay
Make Weak::new const Simple enough. This is const creation of an allocating container, but no actual allocation is done, because it's defined to.
2 parents 16098e2 + 07c2152 commit e2b05ea

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

alloc/src/rc.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2112,9 +2112,10 @@ impl<T> Weak<T> {
21122112
/// assert!(empty.upgrade().is_none());
21132113
/// ```
21142114
#[stable(feature = "downgraded_weak", since = "1.10.0")]
2115+
#[rustc_const_unstable(feature = "const_weak_new", issue = "95091", reason = "recently added")]
21152116
#[must_use]
2116-
pub fn new() -> Weak<T> {
2117-
Weak { ptr: NonNull::new(usize::MAX as *mut RcBox<T>).expect("MAX is not 0") }
2117+
pub const fn new() -> Weak<T> {
2118+
Weak { ptr: unsafe { NonNull::new_unchecked(usize::MAX as *mut RcBox<T>) } }
21182119
}
21192120
}
21202121

alloc/src/sync.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1742,9 +1742,10 @@ impl<T> Weak<T> {
17421742
/// assert!(empty.upgrade().is_none());
17431743
/// ```
17441744
#[stable(feature = "downgraded_weak", since = "1.10.0")]
1745+
#[rustc_const_unstable(feature = "const_weak_new", issue = "95091", reason = "recently added")]
17451746
#[must_use]
1746-
pub fn new() -> Weak<T> {
1747-
Weak { ptr: NonNull::new(usize::MAX as *mut ArcInner<T>).expect("MAX is not 0") }
1747+
pub const fn new() -> Weak<T> {
1748+
Weak { ptr: unsafe { NonNull::new_unchecked(usize::MAX as *mut ArcInner<T>) } }
17481749
}
17491750
}
17501751

0 commit comments

Comments
 (0)