Skip to content

Commit f5a17e8

Browse files
authored
Rollup merge of #59056 - scottmcm:even-fewer-lifetimes, r=sfackler
Use lifetime contravariance to elide more lifetimes in core+alloc+std Sample: ```diff - impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b mut B> for &'a mut A where A: PartialEq<B> { + impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &mut A where A: PartialEq<B> { #[inline] - fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) } + fn eq(&self, other: &&mut B) -> bool { PartialEq::eq(*self, *other) } #[inline] - fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) } + fn ne(&self, other: &&mut B) -> bool { PartialEq::ne(*self, *other) } } ``` [I didn't know this worked](https://internals.rust-lang.org/t/why-can-you-use-different-unconstrained-lifetimes-to-implement-traits/9544/2?u=scottmcm) until recently, but since defining methods contravariantly in their lifetimes this way has worked back to Rust 1.0, we might as well take advantage of combining it with IHLE.
2 parents ab0ccf1 + df4ea90 commit f5a17e8

File tree

19 files changed

+93
-93
lines changed

19 files changed

+93
-93
lines changed

src/liballoc/boxed.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ impl<T: ?Sized> From<Box<T>> for Pin<Box<T>> {
489489
}
490490

491491
#[stable(feature = "box_from_slice", since = "1.17.0")]
492-
impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
492+
impl<T: Copy> From<&[T]> for Box<[T]> {
493493
/// Converts a `&[T]` into a `Box<[T]>`
494494
///
495495
/// This conversion allocates on the heap
@@ -503,15 +503,15 @@ impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
503503
///
504504
/// println!("{:?}", boxed_slice);
505505
/// ```
506-
fn from(slice: &'a [T]) -> Box<[T]> {
506+
fn from(slice: &[T]) -> Box<[T]> {
507507
let mut boxed = unsafe { RawVec::with_capacity(slice.len()).into_box() };
508508
boxed.copy_from_slice(slice);
509509
boxed
510510
}
511511
}
512512

513513
#[stable(feature = "box_from_slice", since = "1.17.0")]
514-
impl<'a> From<&'a str> for Box<str> {
514+
impl From<&str> for Box<str> {
515515
/// Converts a `&str` into a `Box<str>`
516516
///
517517
/// This conversion allocates on the heap
@@ -523,7 +523,7 @@ impl<'a> From<&'a str> for Box<str> {
523523
/// println!("{}", boxed);
524524
/// ```
525525
#[inline]
526-
fn from(s: &'a str) -> Box<str> {
526+
fn from(s: &str) -> Box<str> {
527527
unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) }
528528
}
529529
}

src/liballoc/rc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1145,15 +1145,15 @@ impl<T> From<T> for Rc<T> {
11451145
}
11461146

11471147
#[stable(feature = "shared_from_slice", since = "1.21.0")]
1148-
impl<'a, T: Clone> From<&'a [T]> for Rc<[T]> {
1148+
impl<T: Clone> From<&[T]> for Rc<[T]> {
11491149
#[inline]
11501150
fn from(v: &[T]) -> Rc<[T]> {
11511151
<Self as RcFromSlice<T>>::from_slice(v)
11521152
}
11531153
}
11541154

11551155
#[stable(feature = "shared_from_slice", since = "1.21.0")]
1156-
impl<'a> From<&'a str> for Rc<str> {
1156+
impl From<&str> for Rc<str> {
11571157
#[inline]
11581158
fn from(v: &str) -> Rc<str> {
11591159
let rc = Rc::<[u8]>::from(v.as_bytes());

src/liballoc/string.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2172,9 +2172,9 @@ impl AsRef<[u8]> for String {
21722172
}
21732173

21742174
#[stable(feature = "rust1", since = "1.0.0")]
2175-
impl<'a> From<&'a str> for String {
2175+
impl From<&str> for String {
21762176
#[inline]
2177-
fn from(s: &'a str) -> String {
2177+
fn from(s: &str) -> String {
21782178
s.to_owned()
21792179
}
21802180
}

src/liballoc/vec.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2182,25 +2182,25 @@ impl<T> AsMut<[T]> for Vec<T> {
21822182
}
21832183

21842184
#[stable(feature = "rust1", since = "1.0.0")]
2185-
impl<'a, T: Clone> From<&'a [T]> for Vec<T> {
2185+
impl<T: Clone> From<&[T]> for Vec<T> {
21862186
#[cfg(not(test))]
2187-
fn from(s: &'a [T]) -> Vec<T> {
2187+
fn from(s: &[T]) -> Vec<T> {
21882188
s.to_vec()
21892189
}
21902190
#[cfg(test)]
2191-
fn from(s: &'a [T]) -> Vec<T> {
2191+
fn from(s: &[T]) -> Vec<T> {
21922192
crate::slice::to_vec(s)
21932193
}
21942194
}
21952195

21962196
#[stable(feature = "vec_from_mut", since = "1.19.0")]
2197-
impl<'a, T: Clone> From<&'a mut [T]> for Vec<T> {
2197+
impl<T: Clone> From<&mut [T]> for Vec<T> {
21982198
#[cfg(not(test))]
2199-
fn from(s: &'a mut [T]) -> Vec<T> {
2199+
fn from(s: &mut [T]) -> Vec<T> {
22002200
s.to_vec()
22012201
}
22022202
#[cfg(test)]
2203-
fn from(s: &'a mut [T]) -> Vec<T> {
2203+
fn from(s: &mut [T]) -> Vec<T> {
22042204
crate::slice::to_vec(s)
22052205
}
22062206
}
@@ -2231,8 +2231,8 @@ impl<T> From<Vec<T>> for Box<[T]> {
22312231
}
22322232

22332233
#[stable(feature = "rust1", since = "1.0.0")]
2234-
impl<'a> From<&'a str> for Vec<u8> {
2235-
fn from(s: &'a str) -> Vec<u8> {
2234+
impl From<&str> for Vec<u8> {
2235+
fn from(s: &str) -> Vec<u8> {
22362236
From::from(s.as_bytes())
22372237
}
22382238
}

src/libcore/array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ macro_rules! array_impls {
139139
}
140140

141141
#[stable(feature = "try_from", since = "1.34.0")]
142-
impl<'a, T> TryFrom<&'a [T]> for [T; $N] where T: Copy {
142+
impl<T> TryFrom<&[T]> for [T; $N] where T: Copy {
143143
type Error = TryFromSliceError;
144144

145145
fn try_from(slice: &[T]) -> Result<[T; $N], TryFromSliceError> {

src/libcore/cmp.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -1004,26 +1004,26 @@ mod impls {
10041004
// & pointers
10051005

10061006
#[stable(feature = "rust1", since = "1.0.0")]
1007-
impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b B> for &'a A where A: PartialEq<B> {
1007+
impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &A where A: PartialEq<B> {
10081008
#[inline]
1009-
fn eq(&self, other: & &'b B) -> bool { PartialEq::eq(*self, *other) }
1009+
fn eq(&self, other: & &B) -> bool { PartialEq::eq(*self, *other) }
10101010
#[inline]
1011-
fn ne(&self, other: & &'b B) -> bool { PartialEq::ne(*self, *other) }
1011+
fn ne(&self, other: & &B) -> bool { PartialEq::ne(*self, *other) }
10121012
}
10131013
#[stable(feature = "rust1", since = "1.0.0")]
1014-
impl<'a, 'b, A: ?Sized, B: ?Sized> PartialOrd<&'b B> for &'a A where A: PartialOrd<B> {
1014+
impl<A: ?Sized, B: ?Sized> PartialOrd<&B> for &A where A: PartialOrd<B> {
10151015
#[inline]
1016-
fn partial_cmp(&self, other: &&'b B) -> Option<Ordering> {
1016+
fn partial_cmp(&self, other: &&B) -> Option<Ordering> {
10171017
PartialOrd::partial_cmp(*self, *other)
10181018
}
10191019
#[inline]
1020-
fn lt(&self, other: & &'b B) -> bool { PartialOrd::lt(*self, *other) }
1020+
fn lt(&self, other: & &B) -> bool { PartialOrd::lt(*self, *other) }
10211021
#[inline]
1022-
fn le(&self, other: & &'b B) -> bool { PartialOrd::le(*self, *other) }
1022+
fn le(&self, other: & &B) -> bool { PartialOrd::le(*self, *other) }
10231023
#[inline]
1024-
fn ge(&self, other: & &'b B) -> bool { PartialOrd::ge(*self, *other) }
1024+
fn ge(&self, other: & &B) -> bool { PartialOrd::ge(*self, *other) }
10251025
#[inline]
1026-
fn gt(&self, other: & &'b B) -> bool { PartialOrd::gt(*self, *other) }
1026+
fn gt(&self, other: & &B) -> bool { PartialOrd::gt(*self, *other) }
10271027
}
10281028
#[stable(feature = "rust1", since = "1.0.0")]
10291029
impl<A: ?Sized> Ord for &A where A: Ord {
@@ -1036,26 +1036,26 @@ mod impls {
10361036
// &mut pointers
10371037

10381038
#[stable(feature = "rust1", since = "1.0.0")]
1039-
impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b mut B> for &'a mut A where A: PartialEq<B> {
1039+
impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &mut A where A: PartialEq<B> {
10401040
#[inline]
1041-
fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) }
1041+
fn eq(&self, other: &&mut B) -> bool { PartialEq::eq(*self, *other) }
10421042
#[inline]
1043-
fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) }
1043+
fn ne(&self, other: &&mut B) -> bool { PartialEq::ne(*self, *other) }
10441044
}
10451045
#[stable(feature = "rust1", since = "1.0.0")]
1046-
impl<'a, 'b, A: ?Sized, B: ?Sized> PartialOrd<&'b mut B> for &'a mut A where A: PartialOrd<B> {
1046+
impl<A: ?Sized, B: ?Sized> PartialOrd<&mut B> for &mut A where A: PartialOrd<B> {
10471047
#[inline]
1048-
fn partial_cmp(&self, other: &&'b mut B) -> Option<Ordering> {
1048+
fn partial_cmp(&self, other: &&mut B) -> Option<Ordering> {
10491049
PartialOrd::partial_cmp(*self, *other)
10501050
}
10511051
#[inline]
1052-
fn lt(&self, other: &&'b mut B) -> bool { PartialOrd::lt(*self, *other) }
1052+
fn lt(&self, other: &&mut B) -> bool { PartialOrd::lt(*self, *other) }
10531053
#[inline]
1054-
fn le(&self, other: &&'b mut B) -> bool { PartialOrd::le(*self, *other) }
1054+
fn le(&self, other: &&mut B) -> bool { PartialOrd::le(*self, *other) }
10551055
#[inline]
1056-
fn ge(&self, other: &&'b mut B) -> bool { PartialOrd::ge(*self, *other) }
1056+
fn ge(&self, other: &&mut B) -> bool { PartialOrd::ge(*self, *other) }
10571057
#[inline]
1058-
fn gt(&self, other: &&'b mut B) -> bool { PartialOrd::gt(*self, *other) }
1058+
fn gt(&self, other: &&mut B) -> bool { PartialOrd::gt(*self, *other) }
10591059
}
10601060
#[stable(feature = "rust1", since = "1.0.0")]
10611061
impl<A: ?Sized> Ord for &mut A where A: Ord {
@@ -1066,18 +1066,18 @@ mod impls {
10661066
impl<A: ?Sized> Eq for &mut A where A: Eq {}
10671067

10681068
#[stable(feature = "rust1", since = "1.0.0")]
1069-
impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b mut B> for &'a A where A: PartialEq<B> {
1069+
impl<A: ?Sized, B: ?Sized> PartialEq<&mut B> for &A where A: PartialEq<B> {
10701070
#[inline]
1071-
fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) }
1071+
fn eq(&self, other: &&mut B) -> bool { PartialEq::eq(*self, *other) }
10721072
#[inline]
1073-
fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) }
1073+
fn ne(&self, other: &&mut B) -> bool { PartialEq::ne(*self, *other) }
10741074
}
10751075

10761076
#[stable(feature = "rust1", since = "1.0.0")]
1077-
impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b B> for &'a mut A where A: PartialEq<B> {
1077+
impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &mut A where A: PartialEq<B> {
10781078
#[inline]
1079-
fn eq(&self, other: &&'b B) -> bool { PartialEq::eq(*self, *other) }
1079+
fn eq(&self, other: &&B) -> bool { PartialEq::eq(*self, *other) }
10801080
#[inline]
1081-
fn ne(&self, other: &&'b B) -> bool { PartialEq::ne(*self, *other) }
1081+
fn ne(&self, other: &&B) -> bool { PartialEq::ne(*self, *other) }
10821082
}
10831083
}

src/libcore/internal_macros.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ macro_rules! forward_ref_binop {
3737
}
3838

3939
#[$attr]
40-
impl<'a> $imp<&'a $u> for $t {
40+
impl $imp<&$u> for $t {
4141
type Output = <$t as $imp<$u>>::Output;
4242

4343
#[inline]
44-
fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
44+
fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
4545
$imp::$method(self, *other)
4646
}
4747
}
4848

4949
#[$attr]
50-
impl<'a, 'b> $imp<&'a $u> for &'b $t {
50+
impl $imp<&$u> for &$t {
5151
type Output = <$t as $imp<$u>>::Output;
5252

5353
#[inline]
54-
fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
54+
fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
5555
$imp::$method(*self, *other)
5656
}
5757
}
@@ -67,9 +67,9 @@ macro_rules! forward_ref_op_assign {
6767
};
6868
(impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
6969
#[$attr]
70-
impl<'a> $imp<&'a $u> for $t {
70+
impl $imp<&$u> for $t {
7171
#[inline]
72-
fn $method(&mut self, other: &'a $u) {
72+
fn $method(&mut self, other: &$u) {
7373
$imp::$method(self, *other);
7474
}
7575
}

src/libcore/ptr.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2837,15 +2837,15 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
28372837
}
28382838

28392839
#[unstable(feature = "ptr_internals", issue = "0")]
2840-
impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> {
2841-
fn from(reference: &'a mut T) -> Self {
2840+
impl<T: ?Sized> From<&mut T> for Unique<T> {
2841+
fn from(reference: &mut T) -> Self {
28422842
unsafe { Unique { pointer: reference as *mut T, _marker: PhantomData } }
28432843
}
28442844
}
28452845

28462846
#[unstable(feature = "ptr_internals", issue = "0")]
2847-
impl<'a, T: ?Sized> From<&'a T> for Unique<T> {
2848-
fn from(reference: &'a T) -> Self {
2847+
impl<T: ?Sized> From<&T> for Unique<T> {
2848+
fn from(reference: &T) -> Self {
28492849
unsafe { Unique { pointer: reference as *const T, _marker: PhantomData } }
28502850
}
28512851
}
@@ -3049,17 +3049,17 @@ impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
30493049
}
30503050

30513051
#[stable(feature = "nonnull", since = "1.25.0")]
3052-
impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
3052+
impl<T: ?Sized> From<&mut T> for NonNull<T> {
30533053
#[inline]
3054-
fn from(reference: &'a mut T) -> Self {
3054+
fn from(reference: &mut T) -> Self {
30553055
unsafe { NonNull { pointer: reference as *mut T } }
30563056
}
30573057
}
30583058

30593059
#[stable(feature = "nonnull", since = "1.25.0")]
3060-
impl<'a, T: ?Sized> From<&'a T> for NonNull<T> {
3060+
impl<T: ?Sized> From<&T> for NonNull<T> {
30613061
#[inline]
3062-
fn from(reference: &'a T) -> Self {
3062+
fn from(reference: &T) -> Self {
30633063
unsafe { NonNull { pointer: reference as *const T } }
30643064
}
30653065
}

src/libstd/collections/hash/set.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ impl<T, S> Default for HashSet<T, S>
850850
}
851851

852852
#[stable(feature = "rust1", since = "1.0.0")]
853-
impl<'a, 'b, T, S> BitOr<&'b HashSet<T, S>> for &'a HashSet<T, S>
853+
impl<T, S> BitOr<&HashSet<T, S>> for &HashSet<T, S>
854854
where T: Eq + Hash + Clone,
855855
S: BuildHasher + Default
856856
{
@@ -882,7 +882,7 @@ impl<'a, 'b, T, S> BitOr<&'b HashSet<T, S>> for &'a HashSet<T, S>
882882
}
883883

884884
#[stable(feature = "rust1", since = "1.0.0")]
885-
impl<'a, 'b, T, S> BitAnd<&'b HashSet<T, S>> for &'a HashSet<T, S>
885+
impl<T, S> BitAnd<&HashSet<T, S>> for &HashSet<T, S>
886886
where T: Eq + Hash + Clone,
887887
S: BuildHasher + Default
888888
{
@@ -914,7 +914,7 @@ impl<'a, 'b, T, S> BitAnd<&'b HashSet<T, S>> for &'a HashSet<T, S>
914914
}
915915

916916
#[stable(feature = "rust1", since = "1.0.0")]
917-
impl<'a, 'b, T, S> BitXor<&'b HashSet<T, S>> for &'a HashSet<T, S>
917+
impl<T, S> BitXor<&HashSet<T, S>> for &HashSet<T, S>
918918
where T: Eq + Hash + Clone,
919919
S: BuildHasher + Default
920920
{
@@ -946,7 +946,7 @@ impl<'a, 'b, T, S> BitXor<&'b HashSet<T, S>> for &'a HashSet<T, S>
946946
}
947947

948948
#[stable(feature = "rust1", since = "1.0.0")]
949-
impl<'a, 'b, T, S> Sub<&'b HashSet<T, S>> for &'a HashSet<T, S>
949+
impl<T, S> Sub<&HashSet<T, S>> for &HashSet<T, S>
950950
where T: Eq + Hash + Clone,
951951
S: BuildHasher + Default
952952
{

src/libstd/error.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ impl From<String> for Box<dyn Error> {
337337
}
338338

339339
#[stable(feature = "rust1", since = "1.0.0")]
340-
impl<'a, 'b> From<&'b str> for Box<dyn Error + Send + Sync + 'a> {
340+
impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a> {
341341
/// Converts a [`str`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
342342
///
343343
/// # Examples
@@ -351,13 +351,13 @@ impl<'a, 'b> From<&'b str> for Box<dyn Error + Send + Sync + 'a> {
351351
/// assert!(
352352
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
353353
/// ```
354-
fn from(err: &'b str) -> Box<dyn Error + Send + Sync + 'a> {
354+
fn from(err: &str) -> Box<dyn Error + Send + Sync + 'a> {
355355
From::from(String::from(err))
356356
}
357357
}
358358

359359
#[stable(feature = "string_box_error", since = "1.6.0")]
360-
impl<'a> From<&'a str> for Box<dyn Error> {
360+
impl From<&str> for Box<dyn Error> {
361361
/// Converts a [`str`] into a box of dyn [`Error`].
362362
///
363363
/// # Examples
@@ -370,7 +370,7 @@ impl<'a> From<&'a str> for Box<dyn Error> {
370370
/// let a_boxed_error = Box::<Error>::from(a_str_error);
371371
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
372372
/// ```
373-
fn from(err: &'a str) -> Box<dyn Error> {
373+
fn from(err: &str) -> Box<dyn Error> {
374374
From::from(String::from(err))
375375
}
376376
}

0 commit comments

Comments
 (0)