about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-03-19 14:50:25 +0100
committerGitHub <noreply@github.com>2022-03-19 14:50:25 +0100
commit6a730246617d7c40ed8fe71e50654cbef3d379c2 (patch)
treebe2a5332f56ca0d0011b20f84fd2a7092d3243f9
parentd1ef570a2f24d70bccf274934a8fd0d63de7073f (diff)
parenta358ad2aff788bf21cf9c17662da7e962d75fc83 (diff)
downloadrust-6a730246617d7c40ed8fe71e50654cbef3d379c2.tar.gz
rust-6a730246617d7c40ed8fe71e50654cbef3d379c2.zip
Rollup merge of #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.
-rw-r--r--library/alloc/src/rc.rs5
-rw-r--r--library/alloc/src/sync.rs5
2 files changed, 6 insertions, 4 deletions
diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs
index 3065169e5e2..ea651c075d9 100644
--- a/library/alloc/src/rc.rs
+++ b/library/alloc/src/rc.rs
@@ -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>) } }
     }
 }
 
diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs
index 2140c3f168d..ba3187294e6 100644
--- a/library/alloc/src/sync.rs
+++ b/library/alloc/src/sync.rs
@@ -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>) } }
     }
 }