59
59
//!
60
60
//! drop(gadget_owner);
61
61
//!
62
- //! // Despite dropping gadget_owner, we're still able to print out the name of
63
- //! // the Owner of the Gadgets. This is because we've only dropped the
62
+ //! // Despite dropping gadget_owner, we're still able to print out the name
63
+ //! // of the Owner of the Gadgets. This is because we've only dropped the
64
64
//! // reference count object, not the Owner it wraps. As long as there are
65
- //! // other `Rc<T>` objects pointing at the same Owner, it will remain allocated. Notice
66
- //! // that the `Rc<T>` wrapper around Gadget.owner gets automatically dereferenced
67
- //! // for us.
65
+ //! // other `Rc<T>` objects pointing at the same Owner, it will remain
66
+ //! // allocated. Notice that the `Rc<T>` wrapper around Gadget.owner gets
67
+ //! // automatically dereferenced for us.
68
68
//! println!("Gadget {} owned by {}", gadget1.id, gadget1.owner.name);
69
69
//! println!("Gadget {} owned by {}", gadget2.id, gadget2.owner.name);
70
70
//!
74
74
//! }
75
75
//! ```
76
76
//!
77
- //! If our requirements change, and we also need to be able to traverse from Owner → Gadget, we
78
- //! will run into problems: an `Rc<T>` pointer from Owner → Gadget introduces a cycle between the
79
- //! objects. This means that their reference counts can never reach 0, and the objects will remain
80
- //! allocated: a memory leak. In order to get around this, we can use `Weak<T>` pointers. These
81
- //! pointers don't contribute to the total count.
77
+ //! If our requirements change, and we also need to be able to traverse from
78
+ //! Owner → Gadget, we will run into problems: an `Rc<T>` pointer from Owner
79
+ //! → Gadget introduces a cycle between the objects. This means that their
80
+ //! reference counts can never reach 0, and the objects will remain allocated: a
81
+ //! memory leak. In order to get around this, we can use `Weak<T>` pointers.
82
+ //! These pointers don't contribute to the total count.
82
83
//!
83
- //! Rust actually makes it somewhat difficult to produce this loop in the first place: in order to
84
- //! end up with two objects that point at each other, one of them needs to be mutable. This is
85
- //! problematic because `Rc<T>` enforces memory safety by only giving out shared references to the
86
- //! object it wraps, and these don't allow direct mutation. We need to wrap the part of the object
87
- //! we wish to mutate in a `RefCell`, which provides *interior mutability*: a method to achieve
88
- //! mutability through a shared reference. `RefCell` enforces Rust's borrowing rules at runtime.
89
- //! Read the `Cell` documentation for more details on interior mutability.
84
+ //! Rust actually makes it somewhat difficult to produce this loop in the first
85
+ //! place: in order to end up with two objects that point at each other, one of
86
+ //! them needs to be mutable. This is problematic because `Rc<T>` enforces
87
+ //! memory safety by only giving out shared references to the object it wraps,
88
+ //! and these don't allow direct mutation. We need to wrap the part of the
89
+ //! object we wish to mutate in a `RefCell`, which provides *interior
90
+ //! mutability*: a method to achieve mutability through a shared reference.
91
+ //! `RefCell` enforces Rust's borrowing rules at runtime. Read the `Cell`
92
+ //! documentation for more details on interior mutability.
90
93
//!
91
94
//! ```rust
92
95
//! # #![feature(alloc)]
130
133
//! for gadget_opt in gadget_owner.gadgets.borrow().iter() {
131
134
//!
132
135
//! // gadget_opt is a Weak<Gadget>. Since weak pointers can't guarantee
133
- //! // that their object is still allocated, we need to call upgrade() on them
134
- //! // to turn them into a strong reference. This returns an Option, which
135
- //! // contains a reference to our object if it still exists.
136
+ //! // that their object is still allocated, we need to call upgrade()
137
+ //! // on them to turn them into a strong reference. This returns an
138
+ //! // Option, which contains a reference to our object if it still
139
+ //! // exists.
136
140
//! let gadget = gadget_opt.upgrade().unwrap();
137
141
//! println!("Gadget {} owned by {}", gadget.id, gadget.owner.name);
138
142
//! }
@@ -180,8 +184,8 @@ struct RcBox<T> {
180
184
#[ unsafe_no_drop_flag]
181
185
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
182
186
pub struct Rc < T > {
183
- // FIXME #12808: strange names to try to avoid interfering with field accesses of the contained
184
- // type via Deref
187
+ // FIXME #12808: strange names to try to avoid interfering with field
188
+ // accesses of the contained type via Deref
185
189
_ptr : NonZero < * mut RcBox < T > > ,
186
190
}
187
191
@@ -203,9 +207,10 @@ impl<T> Rc<T> {
203
207
pub fn new ( value : T ) -> Rc < T > {
204
208
unsafe {
205
209
Rc {
206
- // there is an implicit weak pointer owned by all the strong pointers, which
207
- // ensures that the weak destructor never frees the allocation while the strong
208
- // destructor is running, even if the weak pointer is stored inside the strong one.
210
+ // there is an implicit weak pointer owned by all the strong
211
+ // pointers, which ensures that the weak destructor never frees
212
+ // the allocation while the strong destructor is running, even
213
+ // if the weak pointer is stored inside the strong one.
209
214
_ptr : NonZero :: new ( boxed:: into_raw ( box RcBox {
210
215
value : value,
211
216
strong : Cell :: new ( 1 ) ,
@@ -245,7 +250,8 @@ pub fn weak_count<T>(this: &Rc<T>) -> usize { this.weak() - 1 }
245
250
#[ unstable( feature = "alloc" ) ]
246
251
pub fn strong_count < T > ( this : & Rc < T > ) -> usize { this. strong ( ) }
247
252
248
- /// Returns true if there are no other `Rc` or `Weak<T>` values that share the same inner value.
253
+ /// Returns true if there are no other `Rc` or `Weak<T>` values that share the
254
+ /// same inner value.
249
255
///
250
256
/// # Examples
251
257
///
@@ -330,8 +336,8 @@ pub fn get_mut<'a, T>(rc: &'a mut Rc<T>) -> Option<&'a mut T> {
330
336
impl < T : Clone > Rc < T > {
331
337
/// Make a mutable reference from the given `Rc<T>`.
332
338
///
333
- /// This is also referred to as a copy-on-write operation because the inner data is cloned if
334
- /// the reference count is greater than one.
339
+ /// This is also referred to as a copy-on-write operation because the inner
340
+ /// data is cloned if the reference count is greater than one.
335
341
///
336
342
/// # Examples
337
343
///
@@ -349,10 +355,11 @@ impl<T: Clone> Rc<T> {
349
355
if !is_unique ( self ) {
350
356
* self = Rc :: new ( ( * * self ) . clone ( ) )
351
357
}
352
- // This unsafety is ok because we're guaranteed that the pointer returned is the *only*
353
- // pointer that will ever be returned to T. Our reference count is guaranteed to be 1 at
354
- // this point, and we required the `Rc<T>` itself to be `mut`, so we're returning the only
355
- // possible reference to the inner value.
358
+ // This unsafety is ok because we're guaranteed that the pointer
359
+ // returned is the *only* pointer that will ever be returned to T. Our
360
+ // reference count is guaranteed to be 1 at this point, and we required
361
+ // the `Rc<T>` itself to be `mut`, so we're returning the only possible
362
+ // reference to the inner value.
356
363
let inner = unsafe { & mut * * self . _ptr } ;
357
364
& mut inner. value
358
365
}
@@ -373,8 +380,9 @@ impl<T> Deref for Rc<T> {
373
380
impl < T > Drop for Rc < T > {
374
381
/// Drops the `Rc<T>`.
375
382
///
376
- /// This will decrement the strong reference count. If the strong reference count becomes zero
377
- /// and the only other references are `Weak<T>` ones, `drop`s the inner value.
383
+ /// This will decrement the strong reference count. If the strong reference
384
+ /// count becomes zero and the only other references are `Weak<T>` ones,
385
+ /// `drop`s the inner value.
378
386
///
379
387
/// # Examples
380
388
///
@@ -404,8 +412,8 @@ impl<T> Drop for Rc<T> {
404
412
if self . strong ( ) == 0 {
405
413
ptr:: read ( & * * self ) ; // destroy the contained object
406
414
407
- // remove the implicit "strong weak" pointer now that we've destroyed the
408
- // contents.
415
+ // remove the implicit "strong weak" pointer now that we've
416
+ // destroyed the contents.
409
417
self . dec_weak ( ) ;
410
418
411
419
if self . weak ( ) == 0 {
@@ -627,7 +635,8 @@ impl<T: fmt::Debug> fmt::Debug for Rc<T> {
627
635
628
636
/// A weak version of `Rc<T>`.
629
637
///
630
- /// Weak references do not count when determining if the inner value should be dropped.
638
+ /// Weak references do not count when determining if the inner value should be
639
+ /// dropped.
631
640
///
632
641
/// See the [module level documentation](./index.html) for more.
633
642
#[ unsafe_no_drop_flag]
@@ -652,7 +661,8 @@ impl<T> Weak<T> {
652
661
///
653
662
/// Upgrades the `Weak<T>` reference to an `Rc<T>`, if possible.
654
663
///
655
- /// Returns `None` if there were no strong references and the data was destroyed.
664
+ /// Returns `None` if there were no strong references and the data was
665
+ /// destroyed.
656
666
///
657
667
/// # Examples
658
668
///
@@ -710,8 +720,8 @@ impl<T> Drop for Weak<T> {
710
720
let ptr = * self . _ptr ;
711
721
if !ptr. is_null ( ) {
712
722
self . dec_weak ( ) ;
713
- // the weak count starts at 1, and will only go to zero if all the strong pointers
714
- // have disappeared.
723
+ // the weak count starts at 1, and will only go to zero if all
724
+ // the strong pointers have disappeared.
715
725
if self . weak ( ) == 0 {
716
726
deallocate ( ptr as * mut u8 , size_of :: < RcBox < T > > ( ) ,
717
727
min_align_of :: < RcBox < T > > ( ) )
0 commit comments