Skip to content

Commit dc4f39c

Browse files
Rollup merge of #77079 - poliorcetics:more-self-in-docs, r=jyn514
Use `Self` in docs when possible Fixes #76542. I used `rg '\s*//[!/]\s+fn [\w_]+\(&?self, ' .` in `library/` to find instances, I found some with that and some by manually checking. @rustbot modify labels: C-enhancement T-doc
2 parents 28e0bc9 + ec4e9cd commit dc4f39c

File tree

6 files changed

+60
-57
lines changed

6 files changed

+60
-57
lines changed

library/alloc/src/collections/binary_heap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
//! // Explicitly implement the trait so the queue becomes a min-heap
3131
//! // instead of a max-heap.
3232
//! impl Ord for State {
33-
//! fn cmp(&self, other: &State) -> Ordering {
33+
//! fn cmp(&self, other: &Self) -> Ordering {
3434
//! // Notice that the we flip the ordering on costs.
3535
//! // In case of a tie we compare positions - this step is necessary
3636
//! // to make implementations of `PartialEq` and `Ord` consistent.
@@ -41,7 +41,7 @@
4141
//!
4242
//! // `PartialOrd` needs to be implemented as well.
4343
//! impl PartialOrd for State {
44-
//! fn partial_cmp(&self, other: &State) -> Option<Ordering> {
44+
//! fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
4545
//! Some(self.cmp(other))
4646
//! }
4747
//! }

library/core/src/cmp.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -726,19 +726,19 @@ impl PartialOrd for Ordering {
726726
/// }
727727
///
728728
/// impl PartialOrd for Person {
729-
/// fn partial_cmp(&self, other: &Person) -> Option<Ordering> {
729+
/// fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
730730
/// Some(self.cmp(other))
731731
/// }
732732
/// }
733733
///
734734
/// impl Ord for Person {
735-
/// fn cmp(&self, other: &Person) -> Ordering {
735+
/// fn cmp(&self, other: &Self) -> Ordering {
736736
/// self.height.cmp(&other.height)
737737
/// }
738738
/// }
739739
///
740740
/// impl PartialEq for Person {
741-
/// fn eq(&self, other: &Person) -> bool {
741+
/// fn eq(&self, other: &Self) -> bool {
742742
/// self.height == other.height
743743
/// }
744744
/// }

library/core/src/marker.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,9 @@ macro_rules! impls {
643643
/// }
644644
///
645645
/// impl<R: ResType> ExternalResource<R> {
646-
/// fn new() -> ExternalResource<R> {
646+
/// fn new() -> Self {
647647
/// let size_of_res = mem::size_of::<R>();
648-
/// ExternalResource {
648+
/// Self {
649649
/// resource_handle: foreign_lib::new(size_of_res),
650650
/// resource_type: PhantomData,
651651
/// }

library/core/src/ops/arith.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ add_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
128128
/// }
129129
///
130130
/// impl Sub for Point {
131-
/// type Output = Point;
131+
/// type Output = Self;
132132
///
133-
/// fn sub(self, other: Point) -> Point {
134-
/// Point {
133+
/// fn sub(self, other: Self) -> Self::Output {
134+
/// Self {
135135
/// x: self.x - other.x,
136136
/// y: self.y - other.y,
137137
/// }
@@ -241,7 +241,7 @@ sub_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
241241
/// // Reduce to lowest terms by dividing by the greatest common
242242
/// // divisor.
243243
/// let gcd = gcd(numerator, denominator);
244-
/// Rational {
244+
/// Self {
245245
/// numerator: numerator / gcd,
246246
/// denominator: denominator / gcd,
247247
/// }
@@ -255,7 +255,7 @@ sub_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
255255
/// fn mul(self, rhs: Self) -> Self {
256256
/// let numerator = self.numerator * rhs.numerator;
257257
/// let denominator = self.denominator * rhs.denominator;
258-
/// Rational::new(numerator, denominator)
258+
/// Self::new(numerator, denominator)
259259
/// }
260260
/// }
261261
///
@@ -291,7 +291,7 @@ sub_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
291291
/// type Output = Self;
292292
///
293293
/// fn mul(self, rhs: Scalar) -> Self::Output {
294-
/// Vector { value: self.value.iter().map(|v| v * rhs.value).collect() }
294+
/// Self { value: self.value.iter().map(|v| v * rhs.value).collect() }
295295
/// }
296296
/// }
297297
///
@@ -369,7 +369,7 @@ mul_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
369369
/// // Reduce to lowest terms by dividing by the greatest common
370370
/// // divisor.
371371
/// let gcd = gcd(numerator, denominator);
372-
/// Rational {
372+
/// Self {
373373
/// numerator: numerator / gcd,
374374
/// denominator: denominator / gcd,
375375
/// }
@@ -387,7 +387,7 @@ mul_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
387387
///
388388
/// let numerator = self.numerator * rhs.denominator;
389389
/// let denominator = self.denominator * rhs.numerator;
390-
/// Rational::new(numerator, denominator)
390+
/// Self::new(numerator, denominator)
391391
/// }
392392
/// }
393393
///
@@ -423,7 +423,7 @@ mul_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
423423
/// type Output = Self;
424424
///
425425
/// fn div(self, rhs: Scalar) -> Self::Output {
426-
/// Vector { value: self.value.iter().map(|v| v / rhs.value).collect() }
426+
/// Self { value: self.value.iter().map(|v| v / rhs.value).collect() }
427427
/// }
428428
/// }
429429
///
@@ -515,7 +515,7 @@ div_impl_float! { f32 f64 }
515515
/// let len = self.slice.len();
516516
/// let rem = len % modulus;
517517
/// let start = len - rem;
518-
/// SplitSlice {slice: &self.slice[start..]}
518+
/// Self {slice: &self.slice[start..]}
519519
/// }
520520
/// }
521521
///
@@ -615,7 +615,7 @@ rem_impl_float! { f32 f64 }
615615
/// }
616616
///
617617
/// impl Neg for Sign {
618-
/// type Output = Sign;
618+
/// type Output = Self;
619619
///
620620
/// fn neg(self) -> Self::Output {
621621
/// match self {

library/core/src/ops/bit.rs

+36-33
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/// }
1616
///
1717
/// impl Not for Answer {
18-
/// type Output = Answer;
18+
/// type Output = Self;
1919
///
2020
/// fn not(self) -> Self::Output {
2121
/// match self {
@@ -85,7 +85,7 @@ not_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
8585
///
8686
/// // rhs is the "right-hand side" of the expression `a & b`
8787
/// fn bitand(self, rhs: Self) -> Self::Output {
88-
/// Scalar(self.0 & rhs.0)
88+
/// Self(self.0 & rhs.0)
8989
/// }
9090
/// }
9191
///
@@ -106,10 +106,13 @@ not_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
106106
/// impl BitAnd for BooleanVector {
107107
/// type Output = Self;
108108
///
109-
/// fn bitand(self, BooleanVector(rhs): Self) -> Self::Output {
110-
/// let BooleanVector(lhs) = self;
109+
/// fn bitand(self, Self(rhs): Self) -> Self::Output {
110+
/// let Self(lhs) = self;
111111
/// assert_eq!(lhs.len(), rhs.len());
112-
/// BooleanVector(lhs.iter().zip(rhs.iter()).map(|(x, y)| *x && *y).collect())
112+
/// Self(lhs.iter()
113+
/// .zip(rhs.iter())
114+
/// .map(|(x, y)| *x && *y)
115+
/// .collect())
113116
/// }
114117
/// }
115118
///
@@ -179,8 +182,8 @@ bitand_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
179182
/// type Output = Self;
180183
///
181184
/// // rhs is the "right-hand side" of the expression `a | b`
182-
/// fn bitor(self, rhs: Self) -> Self {
183-
/// Scalar(self.0 | rhs.0)
185+
/// fn bitor(self, rhs: Self) -> Self::Output {
186+
/// Self(self.0 | rhs.0)
184187
/// }
185188
/// }
186189
///
@@ -201,10 +204,10 @@ bitand_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
201204
/// impl BitOr for BooleanVector {
202205
/// type Output = Self;
203206
///
204-
/// fn bitor(self, BooleanVector(rhs): Self) -> Self::Output {
205-
/// let BooleanVector(lhs) = self;
207+
/// fn bitor(self, Self(rhs): Self) -> Self::Output {
208+
/// let Self(lhs) = self;
206209
/// assert_eq!(lhs.len(), rhs.len());
207-
/// BooleanVector(lhs.iter().zip(rhs.iter()).map(|(x, y)| *x || *y).collect())
210+
/// Self(lhs.iter().zip(rhs.iter()).map(|(x, y)| *x || *y).collect())
208211
/// }
209212
/// }
210213
///
@@ -275,7 +278,7 @@ bitor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
275278
///
276279
/// // rhs is the "right-hand side" of the expression `a ^ b`
277280
/// fn bitxor(self, rhs: Self) -> Self::Output {
278-
/// Scalar(self.0 ^ rhs.0)
281+
/// Self(self.0 ^ rhs.0)
279282
/// }
280283
/// }
281284
///
@@ -296,13 +299,13 @@ bitor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
296299
/// impl BitXor for BooleanVector {
297300
/// type Output = Self;
298301
///
299-
/// fn bitxor(self, BooleanVector(rhs): Self) -> Self::Output {
300-
/// let BooleanVector(lhs) = self;
302+
/// fn bitxor(self, Self(rhs): Self) -> Self::Output {
303+
/// let Self(lhs) = self;
301304
/// assert_eq!(lhs.len(), rhs.len());
302-
/// BooleanVector(lhs.iter()
303-
/// .zip(rhs.iter())
304-
/// .map(|(x, y)| (*x || *y) && !(*x && *y))
305-
/// .collect())
305+
/// Self(lhs.iter()
306+
/// .zip(rhs.iter())
307+
/// .map(|(x, y)| (*x || *y) && !(*x && *y))
308+
/// .collect())
306309
/// }
307310
/// }
308311
///
@@ -375,9 +378,9 @@ bitxor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
375378
/// impl Shl<Scalar> for Scalar {
376379
/// type Output = Self;
377380
///
378-
/// fn shl(self, Scalar(rhs): Self) -> Scalar {
379-
/// let Scalar(lhs) = self;
380-
/// Scalar(lhs << rhs)
381+
/// fn shl(self, Self(rhs): Self) -> Self::Output {
382+
/// let Self(lhs) = self;
383+
/// Self(lhs << rhs)
381384
/// }
382385
/// }
383386
///
@@ -400,10 +403,10 @@ bitxor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
400403
/// fn shl(self, rhs: usize) -> Self::Output {
401404
/// // Rotate the vector by `rhs` places.
402405
/// let (a, b) = self.vec.split_at(rhs);
403-
/// let mut spun_vector: Vec<T> = vec![];
406+
/// let mut spun_vector = vec![];
404407
/// spun_vector.extend_from_slice(b);
405408
/// spun_vector.extend_from_slice(a);
406-
/// SpinVector { vec: spun_vector }
409+
/// Self { vec: spun_vector }
407410
/// }
408411
/// }
409412
///
@@ -493,9 +496,9 @@ shl_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 isize i128 }
493496
/// impl Shr<Scalar> for Scalar {
494497
/// type Output = Self;
495498
///
496-
/// fn shr(self, Scalar(rhs): Self) -> Scalar {
497-
/// let Scalar(lhs) = self;
498-
/// Scalar(lhs >> rhs)
499+
/// fn shr(self, Self(rhs): Self) -> Self::Output {
500+
/// let Self(lhs) = self;
501+
/// Self(lhs >> rhs)
499502
/// }
500503
/// }
501504
///
@@ -518,10 +521,10 @@ shl_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 isize i128 }
518521
/// fn shr(self, rhs: usize) -> Self::Output {
519522
/// // Rotate the vector by `rhs` places.
520523
/// let (a, b) = self.vec.split_at(self.vec.len() - rhs);
521-
/// let mut spun_vector: Vec<T> = vec![];
524+
/// let mut spun_vector = vec![];
522525
/// spun_vector.extend_from_slice(b);
523526
/// spun_vector.extend_from_slice(a);
524-
/// SpinVector { vec: spun_vector }
527+
/// Self { vec: spun_vector }
525528
/// }
526529
/// }
527530
///
@@ -606,7 +609,7 @@ shr_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
606609
/// impl BitAndAssign for Scalar {
607610
/// // rhs is the "right-hand side" of the expression `a &= b`
608611
/// fn bitand_assign(&mut self, rhs: Self) {
609-
/// *self = Scalar(self.0 & rhs.0)
612+
/// *self = Self(self.0 & rhs.0)
610613
/// }
611614
/// }
612615
///
@@ -640,11 +643,11 @@ shr_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
640643
/// // `rhs` is the "right-hand side" of the expression `a &= b`.
641644
/// fn bitand_assign(&mut self, rhs: Self) {
642645
/// assert_eq!(self.0.len(), rhs.0.len());
643-
/// *self = BooleanVector(self.0
644-
/// .iter()
645-
/// .zip(rhs.0.iter())
646-
/// .map(|(x, y)| *x && *y)
647-
/// .collect());
646+
/// *self = Self(self.0
647+
/// .iter()
648+
/// .zip(rhs.0.iter())
649+
/// .map(|(x, y)| *x && *y)
650+
/// .collect());
648651
/// }
649652
/// }
650653
///

library/core/src/ops/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@
4949
//! }
5050
//!
5151
//! impl Add for Point {
52-
//! type Output = Point;
52+
//! type Output = Self;
5353
//!
54-
//! fn add(self, other: Point) -> Point {
55-
//! Point {x: self.x + other.x, y: self.y + other.y}
54+
//! fn add(self, other: Self) -> Self {
55+
//! Self {x: self.x + other.x, y: self.y + other.y}
5656
//! }
5757
//! }
5858
//!
5959
//! impl Sub for Point {
60-
//! type Output = Point;
60+
//! type Output = Self;
6161
//!
62-
//! fn sub(self, other: Point) -> Point {
63-
//! Point {x: self.x - other.x, y: self.y - other.y}
62+
//! fn sub(self, other: Self) -> Self {
63+
//! Self {x: self.x - other.x, y: self.y - other.y}
6464
//! }
6565
//! }
6666
//!

0 commit comments

Comments
 (0)