Skip to content

Commit 29b5438

Browse files
committed
Test fixes and rebase conflicts, round 2
1 parent 3112716 commit 29b5438

File tree

26 files changed

+179
-149
lines changed

26 files changed

+179
-149
lines changed

src/doc/trpl/documentation.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ Here’s an example of documenting a macro:
361361
#[macro_export]
362362
macro_rules! panic_unless {
363363
($condition:expr, $($rest:expr),+) => ({ if ! $condition { panic!($($rest),+); } });
364-
}
364+
}
365+
# fn main() {}
365366
```
366367
367368
You’ll note three things: we need to add our own `extern crate` line, so that

src/liballoc/arc.rs

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ unsafe impl<T: Sync + Send> Sync for Arc<T> { }
128128

129129
/// A weak pointer to an `Arc`.
130130
///
131-
/// Weak pointers will not keep the data inside of the `Arc` alive, and can be used to break cycles
132-
/// between `Arc` pointers.
131+
/// Weak pointers will not keep the data inside of the `Arc` alive, and can be
132+
/// used to break cycles between `Arc` pointers.
133133
#[unsafe_no_drop_flag]
134134
#[unstable(feature = "alloc",
135135
reason = "Weak pointers may not belong in this module.")]
@@ -218,8 +218,8 @@ impl<T> Arc<T> {
218218
unsafe fn drop_slow(&mut self) {
219219
let ptr = *self._ptr;
220220

221-
// Destroy the data at this time, even though we may not free the box allocation itself
222-
// (there may still be weak pointers lying around).
221+
// Destroy the data at this time, even though we may not free the box
222+
// allocation itself (there may still be weak pointers lying around).
223223
drop(ptr::read(&self.inner().data));
224224

225225
if self.inner().weak.fetch_sub(1, Release) == 1 {
@@ -286,8 +286,8 @@ impl<T> Deref for Arc<T> {
286286
impl<T: Send + Sync + Clone> Arc<T> {
287287
/// Make a mutable reference from the given `Arc<T>`.
288288
///
289-
/// This is also referred to as a copy-on-write operation because the inner data is cloned if
290-
/// the reference count is greater than one.
289+
/// This is also referred to as a copy-on-write operation because the inner
290+
/// data is cloned if the reference count is greater than one.
291291
///
292292
/// # Examples
293293
///
@@ -302,16 +302,18 @@ impl<T: Send + Sync + Clone> Arc<T> {
302302
#[inline]
303303
#[unstable(feature = "alloc")]
304304
pub fn make_unique(&mut self) -> &mut T {
305-
// Note that we hold a strong reference, which also counts as a weak reference, so we only
306-
// clone if there is an additional reference of either kind.
305+
// Note that we hold a strong reference, which also counts as a weak
306+
// reference, so we only clone if there is an additional reference of
307+
// either kind.
307308
if self.inner().strong.load(SeqCst) != 1 ||
308309
self.inner().weak.load(SeqCst) != 1 {
309310
*self = Arc::new((**self).clone())
310311
}
311-
// This unsafety is ok because we're guaranteed that the pointer returned is the *only*
312-
// pointer that will ever be returned to T. Our reference count is guaranteed to be 1 at
313-
// this point, and we required the Arc itself to be `mut`, so we're returning the only
314-
// possible reference to the inner data.
312+
// This unsafety is ok because we're guaranteed that the pointer
313+
// returned is the *only* pointer that will ever be returned to T. Our
314+
// reference count is guaranteed to be 1 at this point, and we required
315+
// the Arc itself to be `mut`, so we're returning the only possible
316+
// reference to the inner data.
315317
let inner = unsafe { &mut **self._ptr };
316318
&mut inner.data
317319
}
@@ -322,8 +324,9 @@ impl<T: Send + Sync + Clone> Arc<T> {
322324
impl<T: Sync + Send> Drop for Arc<T> {
323325
/// Drops the `Arc<T>`.
324326
///
325-
/// This will decrement the strong reference count. If the strong reference count becomes zero
326-
/// and the only other references are `Weak<T>` ones, `drop`s the inner value.
327+
/// This will decrement the strong reference count. If the strong reference
328+
/// count becomes zero and the only other references are `Weak<T>` ones,
329+
/// `drop`s the inner value.
327330
///
328331
/// # Examples
329332
///
@@ -347,29 +350,32 @@ impl<T: Sync + Send> Drop for Arc<T> {
347350
/// ```
348351
#[inline]
349352
fn drop(&mut self) {
350-
// This structure has #[unsafe_no_drop_flag], so this drop glue may run more than once (but
351-
// it is guaranteed to be zeroed after the first if it's run more than once)
353+
// This structure has #[unsafe_no_drop_flag], so this drop glue may run
354+
// more than once (but it is guaranteed to be zeroed after the first if
355+
// it's run more than once)
352356
let ptr = *self._ptr;
353357
if ptr.is_null() { return }
354358

355-
// Because `fetch_sub` is already atomic, we do not need to synchronize with other threads
356-
// unless we are going to delete the object. This same logic applies to the below
357-
// `fetch_sub` to the `weak` count.
359+
// Because `fetch_sub` is already atomic, we do not need to synchronize
360+
// with other threads unless we are going to delete the object. This
361+
// same logic applies to the below `fetch_sub` to the `weak` count.
358362
if self.inner().strong.fetch_sub(1, Release) != 1 { return }
359363

360-
// This fence is needed to prevent reordering of use of the data and deletion of the data.
361-
// Because it is marked `Release`, the decreasing of the reference count synchronizes with
362-
// this `Acquire` fence. This means that use of the data happens before decreasing the
363-
// reference count, which happens before this fence, which happens before the deletion of
364-
// the data.
364+
// This fence is needed to prevent reordering of use of the data and
365+
// deletion of the data. Because it is marked `Release`, the decreasing
366+
// of the reference count synchronizes with this `Acquire` fence. This
367+
// means that use of the data happens before decreasing the reference
368+
// count, which happens before this fence, which happens before the
369+
// deletion of the data.
365370
//
366371
// As explained in the [Boost documentation][1],
367372
//
368-
// > It is important to enforce any possible access to the object in one thread (through an
369-
// > existing reference) to *happen before* deleting the object in a different thread. This
370-
// > is achieved by a "release" operation after dropping a reference (any access to the
371-
// > object through this reference must obviously happened before), and an "acquire"
372-
// > operation before deleting the object.
373+
// > It is important to enforce any possible access to the object in one
374+
// > thread (through an existing reference) to *happen before* deleting
375+
// > the object in a different thread. This is achieved by a "release"
376+
// > operation after dropping a reference (any access to the object
377+
// > through this reference must obviously happened before), and an
378+
// > "acquire" operation before deleting the object.
373379
//
374380
// [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html)
375381
atomic::fence(Acquire);
@@ -387,7 +393,8 @@ impl<T: Sync + Send> Weak<T> {
387393
///
388394
/// Upgrades the `Weak<T>` reference to an `Arc<T>`, if possible.
389395
///
390-
/// Returns `None` if there were no strong references and the data was destroyed.
396+
/// Returns `None` if there were no strong references and the data was
397+
/// destroyed.
391398
///
392399
/// # Examples
393400
///
@@ -402,8 +409,8 @@ impl<T: Sync + Send> Weak<T> {
402409
/// let strong_five: Option<Arc<_>> = weak_five.upgrade();
403410
/// ```
404411
pub fn upgrade(&self) -> Option<Arc<T>> {
405-
// We use a CAS loop to increment the strong count instead of a fetch_add because once the
406-
// count hits 0 is must never be above 0.
412+
// We use a CAS loop to increment the strong count instead of a
413+
// fetch_add because once the count hits 0 is must never be above 0.
407414
let inner = self.inner();
408415
loop {
409416
let n = inner.strong.load(SeqCst);
@@ -480,8 +487,9 @@ impl<T: Sync + Send> Drop for Weak<T> {
480487
// see comments above for why this check is here
481488
if ptr.is_null() { return }
482489

483-
// If we find out that we were the last weak pointer, then its time to deallocate the data
484-
// entirely. See the discussion in Arc::drop() about the memory orderings
490+
// If we find out that we were the last weak pointer, then its time to
491+
// deallocate the data entirely. See the discussion in Arc::drop() about
492+
// the memory orderings
485493
if self.inner().weak.fetch_sub(1, Release) == 1 {
486494
atomic::fence(Acquire);
487495
unsafe { deallocate(ptr as *mut u8, size_of::<ArcInner<T>>(),

src/liballoc/rc.rs

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@
5959
//!
6060
//! drop(gadget_owner);
6161
//!
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
6464
//! // 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.
6868
//! println!("Gadget {} owned by {}", gadget1.id, gadget1.owner.name);
6969
//! println!("Gadget {} owned by {}", gadget2.id, gadget2.owner.name);
7070
//!
@@ -74,19 +74,22 @@
7474
//! }
7575
//! ```
7676
//!
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.
8283
//!
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.
9093
//!
9194
//! ```rust
9295
//! # #![feature(alloc)]
@@ -130,9 +133,10 @@
130133
//! for gadget_opt in gadget_owner.gadgets.borrow().iter() {
131134
//!
132135
//! // 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.
136140
//! let gadget = gadget_opt.upgrade().unwrap();
137141
//! println!("Gadget {} owned by {}", gadget.id, gadget.owner.name);
138142
//! }
@@ -180,8 +184,8 @@ struct RcBox<T> {
180184
#[unsafe_no_drop_flag]
181185
#[stable(feature = "rust1", since = "1.0.0")]
182186
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
185189
_ptr: NonZero<*mut RcBox<T>>,
186190
}
187191

@@ -203,9 +207,10 @@ impl<T> Rc<T> {
203207
pub fn new(value: T) -> Rc<T> {
204208
unsafe {
205209
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.
209214
_ptr: NonZero::new(boxed::into_raw(box RcBox {
210215
value: value,
211216
strong: Cell::new(1),
@@ -245,7 +250,8 @@ pub fn weak_count<T>(this: &Rc<T>) -> usize { this.weak() - 1 }
245250
#[unstable(feature = "alloc")]
246251
pub fn strong_count<T>(this: &Rc<T>) -> usize { this.strong() }
247252

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.
249255
///
250256
/// # Examples
251257
///
@@ -330,8 +336,8 @@ pub fn get_mut<'a, T>(rc: &'a mut Rc<T>) -> Option<&'a mut T> {
330336
impl<T: Clone> Rc<T> {
331337
/// Make a mutable reference from the given `Rc<T>`.
332338
///
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.
335341
///
336342
/// # Examples
337343
///
@@ -349,10 +355,11 @@ impl<T: Clone> Rc<T> {
349355
if !is_unique(self) {
350356
*self = Rc::new((**self).clone())
351357
}
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.
356363
let inner = unsafe { &mut **self._ptr };
357364
&mut inner.value
358365
}
@@ -373,8 +380,9 @@ impl<T> Deref for Rc<T> {
373380
impl<T> Drop for Rc<T> {
374381
/// Drops the `Rc<T>`.
375382
///
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.
378386
///
379387
/// # Examples
380388
///
@@ -404,8 +412,8 @@ impl<T> Drop for Rc<T> {
404412
if self.strong() == 0 {
405413
ptr::read(&**self); // destroy the contained object
406414

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.
409417
self.dec_weak();
410418

411419
if self.weak() == 0 {
@@ -627,7 +635,8 @@ impl<T: fmt::Debug> fmt::Debug for Rc<T> {
627635

628636
/// A weak version of `Rc<T>`.
629637
///
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.
631640
///
632641
/// See the [module level documentation](./index.html) for more.
633642
#[unsafe_no_drop_flag]
@@ -652,7 +661,8 @@ impl<T> Weak<T> {
652661
///
653662
/// Upgrades the `Weak<T>` reference to an `Rc<T>`, if possible.
654663
///
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.
656666
///
657667
/// # Examples
658668
///
@@ -710,8 +720,8 @@ impl<T> Drop for Weak<T> {
710720
let ptr = *self._ptr;
711721
if !ptr.is_null() {
712722
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.
715725
if self.weak() == 0 {
716726
deallocate(ptr as *mut u8, size_of::<RcBox<T>>(),
717727
min_align_of::<RcBox<T>>())

0 commit comments

Comments
 (0)