@@ -174,6 +174,7 @@ struct RcBox<T> {
174
174
/// See the [module level documentation](../index.html) for more details.
175
175
#[ unsafe_no_drop_flag]
176
176
#[ stable]
177
+ #[ cfg( stage0) ] // NOTE remove impl after next snapshot
177
178
pub struct Rc < T > {
178
179
// FIXME #12808: strange names to try to avoid interfering with field accesses of the contained
179
180
// type via Deref
@@ -182,6 +183,24 @@ pub struct Rc<T> {
182
183
_noshare : marker:: NoSync
183
184
}
184
185
186
+ /// An immutable reference-counted pointer type.
187
+ ///
188
+ /// See the [module level documentation](../index.html) for more details.
189
+ #[ unsafe_no_drop_flag]
190
+ #[ stable]
191
+ #[ cfg( not( stage0) ) ] // NOTE remove cfg after next snapshot
192
+ pub struct Rc < T > {
193
+ // FIXME #12808: strange names to try to avoid interfering with field accesses of the contained
194
+ // type via Deref
195
+ _ptr : NonZero < * mut RcBox < T > > ,
196
+ }
197
+
198
+ #[ cfg( not( stage0) ) ] // NOTE remove cfg after next snapshot
199
+ impl < T > !marker:: Send for Rc < T > { }
200
+
201
+ #[ cfg( not( stage0) ) ] // NOTE remove cfg after next snapshot
202
+ impl < T > !marker:: Sync for Rc < T > { }
203
+
185
204
impl < T > Rc < T > {
186
205
/// Constructs a new `Rc<T>`.
187
206
///
@@ -193,6 +212,7 @@ impl<T> Rc<T> {
193
212
/// let five = Rc::new(5i);
194
213
/// ```
195
214
#[ stable]
215
+ #[ cfg( stage0) ] // NOTE remove after next snapshot
196
216
pub fn new ( value : T ) -> Rc < T > {
197
217
unsafe {
198
218
Rc {
@@ -210,6 +230,32 @@ impl<T> Rc<T> {
210
230
}
211
231
}
212
232
233
+ /// Constructs a new `Rc<T>`.
234
+ ///
235
+ /// # Examples
236
+ ///
237
+ /// ```
238
+ /// use std::rc::Rc;
239
+ ///
240
+ /// let five = Rc::new(5i);
241
+ /// ```
242
+ #[ stable]
243
+ #[ cfg( not( stage0) ) ] // NOTE remove cfg after next snapshot
244
+ pub fn new ( value : T ) -> Rc < T > {
245
+ unsafe {
246
+ Rc {
247
+ // there is an implicit weak pointer owned by all the strong pointers, which
248
+ // ensures that the weak destructor never frees the allocation while the strong
249
+ // destructor is running, even if the weak pointer is stored inside the strong one.
250
+ _ptr : NonZero :: new ( transmute ( box RcBox {
251
+ value : value,
252
+ strong : Cell :: new ( 1 ) ,
253
+ weak : Cell :: new ( 1 )
254
+ } ) ) ,
255
+ }
256
+ }
257
+ }
258
+
213
259
/// Downgrades the `Rc<T>` to a `Weak<T>` reference.
214
260
///
215
261
/// # Examples
@@ -221,6 +267,7 @@ impl<T> Rc<T> {
221
267
///
222
268
/// let weak_five = five.downgrade();
223
269
/// ```
270
+ #[ cfg( stage0) ] // NOTE remove after next snapshot
224
271
#[ unstable = "Weak pointers may not belong in this module" ]
225
272
pub fn downgrade ( & self ) -> Weak < T > {
226
273
self . inc_weak ( ) ;
@@ -230,6 +277,24 @@ impl<T> Rc<T> {
230
277
_noshare : marker:: NoSync
231
278
}
232
279
}
280
+
281
+ /// Downgrades the `Rc<T>` to a `Weak<T>` reference.
282
+ ///
283
+ /// # Examples
284
+ ///
285
+ /// ```
286
+ /// use std::rc::Rc;
287
+ ///
288
+ /// let five = Rc::new(5i);
289
+ ///
290
+ /// let weak_five = five.downgrade();
291
+ /// ```
292
+ #[ cfg( not( stage0) ) ] // NOTE remove cfg after next snapshot
293
+ #[ unstable = "Weak pointers may not belong in this module" ]
294
+ pub fn downgrade ( & self ) -> Weak < T > {
295
+ self . inc_weak ( ) ;
296
+ Weak { _ptr : self . _ptr }
297
+ }
233
298
}
234
299
235
300
/// Get the number of weak references to this value.
@@ -432,10 +497,31 @@ impl<T> Clone for Rc<T> {
432
497
/// five.clone();
433
498
/// ```
434
499
#[ inline]
500
+ #[ cfg( stage0) ] // NOTE remove after next snapshot
435
501
fn clone ( & self ) -> Rc < T > {
436
502
self . inc_strong ( ) ;
437
503
Rc { _ptr : self . _ptr , _nosend : marker:: NoSend , _noshare : marker:: NoSync }
438
504
}
505
+
506
+ /// Makes a clone of the `Rc<T>`.
507
+ ///
508
+ /// This increases the strong reference count.
509
+ ///
510
+ /// # Examples
511
+ ///
512
+ /// ```
513
+ /// use std::rc::Rc;
514
+ ///
515
+ /// let five = Rc::new(5i);
516
+ ///
517
+ /// five.clone();
518
+ /// ```
519
+ #[ inline]
520
+ #[ cfg( not( stage0) ) ] // NOTE remove cfg after next snapshot
521
+ fn clone ( & self ) -> Rc < T > {
522
+ self . inc_strong ( ) ;
523
+ Rc { _ptr : self . _ptr }
524
+ }
439
525
}
440
526
441
527
#[ stable]
@@ -636,6 +722,7 @@ impl<T: fmt::String> fmt::String for Rc<T> {
636
722
/// See the [module level documentation](../index.html) for more.
637
723
#[ unsafe_no_drop_flag]
638
724
#[ unstable = "Weak pointers may not belong in this module." ]
725
+ #[ cfg( stage0) ] // NOTE remove impl after next snapshot
639
726
pub struct Weak < T > {
640
727
// FIXME #12808: strange names to try to avoid interfering with
641
728
// field accesses of the contained type via Deref
@@ -644,6 +731,29 @@ pub struct Weak<T> {
644
731
_noshare : marker:: NoSync
645
732
}
646
733
734
+ /// A weak version of `Rc<T>`.
735
+ ///
736
+ /// Weak references do not count when determining if the inner value should be dropped.
737
+ ///
738
+ /// See the [module level documentation](../index.html) for more.
739
+ #[ unsafe_no_drop_flag]
740
+ #[ unstable = "Weak pointers may not belong in this module." ]
741
+ #[ cfg( not( stage0) ) ] // NOTE remove cfg after next snapshot
742
+ pub struct Weak < T > {
743
+ // FIXME #12808: strange names to try to avoid interfering with
744
+ // field accesses of the contained type via Deref
745
+ _ptr : NonZero < * mut RcBox < T > > ,
746
+ }
747
+
748
+ #[ cfg( not( stage0) ) ] // NOTE remove cfg after next snapshot
749
+ #[ allow( unstable) ]
750
+ impl < T > !marker:: Send for Weak < T > { }
751
+
752
+ #[ cfg( not( stage0) ) ] // NOTE remove cfg after next snapshot
753
+ #[ allow( unstable) ]
754
+ impl < T > !marker:: Sync for Weak < T > { }
755
+
756
+
647
757
#[ unstable = "Weak pointers may not belong in this module." ]
648
758
impl < T > Weak < T > {
649
759
/// Upgrades a weak reference to a strong reference.
@@ -663,6 +773,7 @@ impl<T> Weak<T> {
663
773
///
664
774
/// let strong_five: Option<Rc<_>> = weak_five.upgrade();
665
775
/// ```
776
+ #[ cfg( stage0) ] // NOTE remove after next snapshot
666
777
pub fn upgrade ( & self ) -> Option < Rc < T > > {
667
778
if self . strong ( ) == 0 {
668
779
None
@@ -671,6 +782,33 @@ impl<T> Weak<T> {
671
782
Some ( Rc { _ptr : self . _ptr , _nosend : marker:: NoSend , _noshare : marker:: NoSync } )
672
783
}
673
784
}
785
+
786
+ /// Upgrades a weak reference to a strong reference.
787
+ ///
788
+ /// Upgrades the `Weak<T>` reference to an `Rc<T>`, if possible.
789
+ ///
790
+ /// Returns `None` if there were no strong references and the data was destroyed.
791
+ ///
792
+ /// # Examples
793
+ ///
794
+ /// ```
795
+ /// use std::rc::Rc;
796
+ ///
797
+ /// let five = Rc::new(5i);
798
+ ///
799
+ /// let weak_five = five.downgrade();
800
+ ///
801
+ /// let strong_five: Option<Rc<_>> = weak_five.upgrade();
802
+ /// ```
803
+ #[ cfg( not( stage0) ) ] // NOTE remove cfg after next snapshot
804
+ pub fn upgrade ( & self ) -> Option < Rc < T > > {
805
+ if self . strong ( ) == 0 {
806
+ None
807
+ } else {
808
+ self . inc_strong ( ) ;
809
+ Some ( Rc { _ptr : self . _ptr } )
810
+ }
811
+ }
674
812
}
675
813
676
814
#[ unsafe_destructor]
@@ -733,10 +871,31 @@ impl<T> Clone for Weak<T> {
733
871
/// weak_five.clone();
734
872
/// ```
735
873
#[ inline]
874
+ #[ cfg( stage0) ] // NOTE remove after next snapshot
736
875
fn clone ( & self ) -> Weak < T > {
737
876
self . inc_weak ( ) ;
738
877
Weak { _ptr : self . _ptr , _nosend : marker:: NoSend , _noshare : marker:: NoSync }
739
878
}
879
+
880
+ /// Makes a clone of the `Weak<T>`.
881
+ ///
882
+ /// This increases the weak reference count.
883
+ ///
884
+ /// # Examples
885
+ ///
886
+ /// ```
887
+ /// use std::rc::Rc;
888
+ ///
889
+ /// let weak_five = Rc::new(5i).downgrade();
890
+ ///
891
+ /// weak_five.clone();
892
+ /// ```
893
+ #[ inline]
894
+ #[ cfg( not( stage0) ) ] // NOTE remove cfg after next snapshot
895
+ fn clone ( & self ) -> Weak < T > {
896
+ self . inc_weak ( ) ;
897
+ Weak { _ptr : self . _ptr }
898
+ }
740
899
}
741
900
742
901
#[ unstable = "Show is experimental." ]
0 commit comments