Skip to content

Commit

Permalink
Rollup merge of #94991 - CAD97:const-weak-new, r=dtolnay
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dylan-DPC authored Mar 19, 2022
2 parents d1ef570 + a358ad2 commit 6a73024
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2112,9 +2112,10 @@ impl<T> Weak<T> {
/// assert!(empty.upgrade().is_none());
/// ```
#[stable(feature = "downgraded_weak", since = "1.10.0")]
#[rustc_const_unstable(feature = "const_weak_new", issue = "95091", reason = "recently added")]
#[must_use]
pub fn new() -> Weak<T> {
Weak { ptr: NonNull::new(usize::MAX as *mut RcBox<T>).expect("MAX is not 0") }
pub const fn new() -> Weak<T> {
Weak { ptr: unsafe { NonNull::new_unchecked(usize::MAX as *mut RcBox<T>) } }
}
}

Expand Down
5 changes: 3 additions & 2 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1742,9 +1742,10 @@ impl<T> Weak<T> {
/// assert!(empty.upgrade().is_none());
/// ```
#[stable(feature = "downgraded_weak", since = "1.10.0")]
#[rustc_const_unstable(feature = "const_weak_new", issue = "95091", reason = "recently added")]
#[must_use]
pub fn new() -> Weak<T> {
Weak { ptr: NonNull::new(usize::MAX as *mut ArcInner<T>).expect("MAX is not 0") }
pub const fn new() -> Weak<T> {
Weak { ptr: unsafe { NonNull::new_unchecked(usize::MAX as *mut ArcInner<T>) } }
}
}

Expand Down

0 comments on commit 6a73024

Please sign in to comment.