@@ -79,6 +79,7 @@ use core::cmp::Ordering;
79
79
use core:: mem:: { align_of_val, size_of_val} ;
80
80
use core:: intrinsics:: abort;
81
81
use core:: mem;
82
+ use core:: mem:: uninitialized;
82
83
use core:: ops:: Deref ;
83
84
#[ cfg( not( stage0) ) ]
84
85
use core:: ops:: CoerceUnsized ;
@@ -910,6 +911,36 @@ impl<T> From<T> for Arc<T> {
910
911
}
911
912
}
912
913
914
+ impl < T > Weak < T > {
915
+ /// Constructs a new `Weak<T>` without an accompanying instance of T.
916
+ ///
917
+ /// This allocates memory for T, but does not initialize it. Calling
918
+ /// Weak<T>::upgrade() on the return value always gives None.
919
+ ///
920
+ /// # Examples
921
+ ///
922
+ /// ```
923
+ /// #![feature(downgraded_weak)]
924
+ ///
925
+ /// use std::sync::Arc;
926
+ ///
927
+ /// let five = Arc::new(5);
928
+ /// ```
929
+ #[ unstable( feature = "downgraded_weak" ,
930
+ reason = "recently added" ,
931
+ issue = "30425" ) ]
932
+ pub fn new ( ) -> Weak < T > {
933
+ unsafe {
934
+ let x: Box < _ > = box ArcInner {
935
+ strong : atomic:: AtomicUsize :: new ( 0 ) ,
936
+ weak : atomic:: AtomicUsize :: new ( 1 ) ,
937
+ data : uninitialized ( ) ,
938
+ } ;
939
+ Weak { _ptr : Shared :: new ( Box :: into_raw ( x) ) }
940
+ }
941
+ }
942
+ }
943
+
913
944
#[ cfg( test) ]
914
945
mod tests {
915
946
use std:: clone:: Clone ;
@@ -1160,6 +1191,12 @@ mod tests {
1160
1191
let foo_arc = Arc :: from ( foo) ;
1161
1192
assert ! ( 123 == * foo_arc) ;
1162
1193
}
1194
+
1195
+ #[ test]
1196
+ fn test_new_weak ( ) {
1197
+ let foo: Weak < usize > = Weak :: new ( ) ;
1198
+ assert ! ( foo. upgrade( ) . is_none( ) ) ;
1199
+ }
1163
1200
}
1164
1201
1165
1202
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
0 commit comments