Skip to content

Commit 5712e03

Browse files
committed
Auto merge of #42258 - Mark-Simulacrum:rollup, r=Mark-Simulacrum
Rollup of 10 pull requests - Successful merges: #42103, #42137, #42162, #42167, #42175, #42207, #42217, #42246, #42249, #42251 - Failed merges:
2 parents a11c26f + 7ffeb85 commit 5712e03

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+348
-1412
lines changed

src/Cargo.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/doc/unstable-book/src/SUMMARY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
- [cfg_target_has_atomic](language-features/cfg-target-has-atomic.md)
2626
- [cfg_target_thread_local](language-features/cfg-target-thread-local.md)
2727
- [cfg_target_vendor](language-features/cfg-target-vendor.md)
28-
- [closure_to_fn_coercion](language-features/closure-to-fn-coercion.md)
2928
- [compiler_builtins](language-features/compiler-builtins.md)
3029
- [concat_idents](language-features/concat-idents.md)
3130
- [conservative_impl_trait](language-features/conservative-impl-trait.md)
@@ -154,6 +153,7 @@
154153
- [io](library-features/io.md)
155154
- [ip](library-features/ip.md)
156155
- [iter_rfind](library-features/iter-rfind.md)
156+
- [iterator_step_by](library-features/iterator-step-by.md)
157157
- [libstd_io_internals](library-features/libstd-io-internals.md)
158158
- [libstd_sys_internals](library-features/libstd-sys-internals.md)
159159
- [libstd_thread_internals](library-features/libstd-thread-internals.md)

src/doc/unstable-book/src/language-features/closure-to-fn-coercion.md

-7
This file was deleted.

src/liballoc/arc.rs

+31-13
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
9191
/// strong `Arc` pointers from parent nodes to children, and [`Weak`][weak]
9292
/// pointers from children back to their parents.
9393
///
94+
/// # Cloning references
95+
///
96+
/// Creating a new reference from an existing reference counted pointer is done using the
97+
/// `Clone` trait implemented for [`Arc<T>`][`arc`] and [`Weak<T>`][`weak`].
98+
///
99+
/// ```
100+
/// use std::sync::Arc;
101+
/// let foo = Arc::new(vec![1.0, 2.0, 3.0]);
102+
/// // The two syntaxes below are equivalent.
103+
/// let a = foo.clone();
104+
/// let b = Arc::clone(&foo);
105+
/// // a and b both point to the same memory location as foo.
106+
/// ```
107+
///
108+
/// The `Arc::clone(&from)` syntax is the most idiomatic because it conveys more explicitly
109+
/// the meaning of the code. In the example above, this syntax makes it easier to see that
110+
/// this code is creating a new reference rather than copying the whole content of foo.
111+
///
94112
/// ## `Deref` behavior
95113
///
96114
/// `Arc<T>` automatically dereferences to `T` (via the [`Deref`][deref] trait),
@@ -138,7 +156,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
138156
/// let five = Arc::new(5);
139157
///
140158
/// for _ in 0..10 {
141-
/// let five = five.clone();
159+
/// let five = Arc::clone(&five);
142160
///
143161
/// thread::spawn(move || {
144162
/// println!("{:?}", five);
@@ -158,7 +176,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
158176
/// let val = Arc::new(AtomicUsize::new(5));
159177
///
160178
/// for _ in 0..10 {
161-
/// let val = val.clone();
179+
/// let val = Arc::clone(&val);
162180
///
163181
/// thread::spawn(move || {
164182
/// let v = val.fetch_add(1, Ordering::SeqCst);
@@ -282,7 +300,7 @@ impl<T> Arc<T> {
282300
/// assert_eq!(Arc::try_unwrap(x), Ok(3));
283301
///
284302
/// let x = Arc::new(4);
285-
/// let _y = x.clone();
303+
/// let _y = Arc::clone(&x);
286304
/// assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4);
287305
/// ```
288306
#[inline]
@@ -451,7 +469,7 @@ impl<T: ?Sized> Arc<T> {
451469
/// use std::sync::Arc;
452470
///
453471
/// let five = Arc::new(5);
454-
/// let _also_five = five.clone();
472+
/// let _also_five = Arc::clone(&five);
455473
///
456474
/// // This assertion is deterministic because we haven't shared
457475
/// // the `Arc` between threads.
@@ -499,7 +517,7 @@ impl<T: ?Sized> Arc<T> {
499517
/// use std::sync::Arc;
500518
///
501519
/// let five = Arc::new(5);
502-
/// let same_five = five.clone();
520+
/// let same_five = Arc::clone(&five);
503521
/// let other_five = Arc::new(5);
504522
///
505523
/// assert!(Arc::ptr_eq(&five, &same_five));
@@ -524,7 +542,7 @@ impl<T: ?Sized> Clone for Arc<T> {
524542
///
525543
/// let five = Arc::new(5);
526544
///
527-
/// five.clone();
545+
/// Arc::clone(&five);
528546
/// ```
529547
#[inline]
530548
fn clone(&self) -> Arc<T> {
@@ -591,7 +609,7 @@ impl<T: Clone> Arc<T> {
591609
/// let mut data = Arc::new(5);
592610
///
593611
/// *Arc::make_mut(&mut data) += 1; // Won't clone anything
594-
/// let mut other_data = data.clone(); // Won't clone inner data
612+
/// let mut other_data = Arc::clone(&data); // Won't clone inner data
595613
/// *Arc::make_mut(&mut data) += 1; // Clones inner data
596614
/// *Arc::make_mut(&mut data) += 1; // Won't clone anything
597615
/// *Arc::make_mut(&mut other_data) *= 2; // Won't clone anything
@@ -679,7 +697,7 @@ impl<T: ?Sized> Arc<T> {
679697
/// *Arc::get_mut(&mut x).unwrap() = 4;
680698
/// assert_eq!(*x, 4);
681699
///
682-
/// let _y = x.clone();
700+
/// let _y = Arc::clone(&x);
683701
/// assert!(Arc::get_mut(&mut x).is_none());
684702
/// ```
685703
#[inline]
@@ -751,7 +769,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc<T> {
751769
/// }
752770
///
753771
/// let foo = Arc::new(Foo);
754-
/// let foo2 = foo.clone();
772+
/// let foo2 = Arc::clone(&foo);
755773
///
756774
/// drop(foo); // Doesn't print anything
757775
/// drop(foo2); // Prints "dropped!"
@@ -903,11 +921,11 @@ impl<T: ?Sized> Clone for Weak<T> {
903921
/// # Examples
904922
///
905923
/// ```
906-
/// use std::sync::Arc;
924+
/// use std::sync::{Arc, Weak};
907925
///
908926
/// let weak_five = Arc::downgrade(&Arc::new(5));
909927
///
910-
/// weak_five.clone();
928+
/// Weak::clone(&weak_five);
911929
/// ```
912930
#[inline]
913931
fn clone(&self) -> Weak<T> {
@@ -956,7 +974,7 @@ impl<T: ?Sized> Drop for Weak<T> {
956974
/// # Examples
957975
///
958976
/// ```
959-
/// use std::sync::Arc;
977+
/// use std::sync::{Arc, Weak};
960978
///
961979
/// struct Foo;
962980
///
@@ -968,7 +986,7 @@ impl<T: ?Sized> Drop for Weak<T> {
968986
///
969987
/// let foo = Arc::new(Foo);
970988
/// let weak_foo = Arc::downgrade(&foo);
971-
/// let other_weak_foo = weak_foo.clone();
989+
/// let other_weak_foo = Weak::clone(&weak_foo);
972990
///
973991
/// drop(weak_foo); // Doesn't print anything
974992
/// drop(foo); // Prints "dropped!"

src/liballoc/rc.rs

+33-15
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@
5555
//! [`Weak<T>`][`Weak`] does not auto-dereference to `T`, because the value may have
5656
//! already been destroyed.
5757
//!
58+
//! # Cloning references
59+
//!
60+
//! Creating a new reference from an existing reference counted pointer is done using the
61+
//! `Clone` trait implemented for [`Rc<T>`][`Rc`] and [`Weak<T>`][`Weak`].
62+
//!
63+
//! ```
64+
//! use std::rc::Rc;
65+
//! let foo = Rc::new(vec![1.0, 2.0, 3.0]);
66+
//! // The two syntaxes below are equivalent.
67+
//! let a = foo.clone();
68+
//! let b = Rc::clone(&foo);
69+
//! // a and b both point to the same memory location as foo.
70+
//! ```
71+
//!
72+
//! The `Rc::clone(&from)` syntax is the most idiomatic because it conveys more explicitly
73+
//! the meaning of the code. In the example above, this syntax makes it easier to see that
74+
//! this code is creating a new reference rather than copying the whole content of foo.
75+
//!
5876
//! # Examples
5977
//!
6078
//! Consider a scenario where a set of `Gadget`s are owned by a given `Owner`.
@@ -90,11 +108,11 @@
90108
//! // the reference count in the process.
91109
//! let gadget1 = Gadget {
92110
//! id: 1,
93-
//! owner: gadget_owner.clone(),
111+
//! owner: Rc::clone(&gadget_owner),
94112
//! };
95113
//! let gadget2 = Gadget {
96114
//! id: 2,
97-
//! owner: gadget_owner.clone(),
115+
//! owner: Rc::clone(&gadget_owner),
98116
//! };
99117
//!
100118
//! // Dispose of our local variable `gadget_owner`.
@@ -163,13 +181,13 @@
163181
//! let gadget1 = Rc::new(
164182
//! Gadget {
165183
//! id: 1,
166-
//! owner: gadget_owner.clone(),
184+
//! owner: Rc::clone(&gadget_owner),
167185
//! }
168186
//! );
169187
//! let gadget2 = Rc::new(
170188
//! Gadget {
171189
//! id: 2,
172-
//! owner: gadget_owner.clone(),
190+
//! owner: Rc::clone(&gadget_owner),
173191
//! }
174192
//! );
175193
//!
@@ -316,7 +334,7 @@ impl<T> Rc<T> {
316334
/// assert_eq!(Rc::try_unwrap(x), Ok(3));
317335
///
318336
/// let x = Rc::new(4);
319-
/// let _y = x.clone();
337+
/// let _y = Rc::clone(&x);
320338
/// assert_eq!(*Rc::try_unwrap(x).unwrap_err(), 4);
321339
/// ```
322340
#[inline]
@@ -508,7 +526,7 @@ impl<T: ?Sized> Rc<T> {
508526
/// use std::rc::Rc;
509527
///
510528
/// let five = Rc::new(5);
511-
/// let _also_five = five.clone();
529+
/// let _also_five = Rc::clone(&five);
512530
///
513531
/// assert_eq!(2, Rc::strong_count(&five));
514532
/// ```
@@ -550,7 +568,7 @@ impl<T: ?Sized> Rc<T> {
550568
/// *Rc::get_mut(&mut x).unwrap() = 4;
551569
/// assert_eq!(*x, 4);
552570
///
553-
/// let _y = x.clone();
571+
/// let _y = Rc::clone(&x);
554572
/// assert!(Rc::get_mut(&mut x).is_none());
555573
/// ```
556574
#[inline]
@@ -576,7 +594,7 @@ impl<T: ?Sized> Rc<T> {
576594
/// use std::rc::Rc;
577595
///
578596
/// let five = Rc::new(5);
579-
/// let same_five = five.clone();
597+
/// let same_five = Rc::clone(&five);
580598
/// let other_five = Rc::new(5);
581599
///
582600
/// assert!(Rc::ptr_eq(&five, &same_five));
@@ -608,7 +626,7 @@ impl<T: Clone> Rc<T> {
608626
/// let mut data = Rc::new(5);
609627
///
610628
/// *Rc::make_mut(&mut data) += 1; // Won't clone anything
611-
/// let mut other_data = data.clone(); // Won't clone inner data
629+
/// let mut other_data = Rc::clone(&data); // Won't clone inner data
612630
/// *Rc::make_mut(&mut data) += 1; // Clones inner data
613631
/// *Rc::make_mut(&mut data) += 1; // Won't clone anything
614632
/// *Rc::make_mut(&mut other_data) *= 2; // Won't clone anything
@@ -680,7 +698,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
680698
/// }
681699
///
682700
/// let foo = Rc::new(Foo);
683-
/// let foo2 = foo.clone();
701+
/// let foo2 = Rc::clone(&foo);
684702
///
685703
/// drop(foo); // Doesn't print anything
686704
/// drop(foo2); // Prints "dropped!"
@@ -720,7 +738,7 @@ impl<T: ?Sized> Clone for Rc<T> {
720738
///
721739
/// let five = Rc::new(5);
722740
///
723-
/// five.clone();
741+
/// Rc::clone(&five);
724742
/// ```
725743
#[inline]
726744
fn clone(&self) -> Rc<T> {
@@ -1050,7 +1068,7 @@ impl<T: ?Sized> Drop for Weak<T> {
10501068
/// # Examples
10511069
///
10521070
/// ```
1053-
/// use std::rc::Rc;
1071+
/// use std::rc::{Rc, Weak};
10541072
///
10551073
/// struct Foo;
10561074
///
@@ -1062,7 +1080,7 @@ impl<T: ?Sized> Drop for Weak<T> {
10621080
///
10631081
/// let foo = Rc::new(Foo);
10641082
/// let weak_foo = Rc::downgrade(&foo);
1065-
/// let other_weak_foo = weak_foo.clone();
1083+
/// let other_weak_foo = Weak::clone(&weak_foo);
10661084
///
10671085
/// drop(weak_foo); // Doesn't print anything
10681086
/// drop(foo); // Prints "dropped!"
@@ -1090,11 +1108,11 @@ impl<T: ?Sized> Clone for Weak<T> {
10901108
/// # Examples
10911109
///
10921110
/// ```
1093-
/// use std::rc::Rc;
1111+
/// use std::rc::{Rc, Weak};
10941112
///
10951113
/// let weak_five = Rc::downgrade(&Rc::new(5));
10961114
///
1097-
/// weak_five.clone();
1115+
/// Weak::clone(&weak_five);
10981116
/// ```
10991117
#[inline]
11001118
fn clone(&self) -> Weak<T> {

src/libcore/iter/mod.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ impl<I> Iterator for Cycle<I> where I: Clone + Iterator {
520520
#[unstable(feature = "fused", issue = "35602")]
521521
impl<I> FusedIterator for Cycle<I> where I: Clone + Iterator {}
522522

523-
/// An iterator that steps by n elements every iteration.
523+
/// An adapter for stepping iterators by a custom amount.
524524
///
525525
/// This `struct` is created by the [`step_by`] method on [`Iterator`]. See
526526
/// its documentation for more.
@@ -553,8 +553,27 @@ impl<I> Iterator for StepBy<I> where I: Iterator {
553553
self.iter.nth(self.step)
554554
}
555555
}
556+
557+
#[inline]
558+
fn size_hint(&self) -> (usize, Option<usize>) {
559+
let inner_hint = self.iter.size_hint();
560+
561+
if self.first_take {
562+
let f = |n| if n == 0 { 0 } else { 1 + (n-1)/(self.step+1) };
563+
(f(inner_hint.0), inner_hint.1.map(f))
564+
} else {
565+
let f = |n| n / (self.step+1);
566+
(f(inner_hint.0), inner_hint.1.map(f))
567+
}
568+
}
556569
}
557570

571+
// StepBy can only make the iterator shorter, so the len will still fit.
572+
#[unstable(feature = "iterator_step_by",
573+
reason = "unstable replacement of Range::step_by",
574+
issue = "27741")]
575+
impl<I> ExactSizeIterator for StepBy<I> where I: ExactSizeIterator {}
576+
558577
/// An iterator that strings two iterators together.
559578
///
560579
/// This `struct` is created by the [`chain`] method on [`Iterator`]. See its

0 commit comments

Comments
 (0)