Skip to content

Commit 25ea20d

Browse files
authored
Rollup merge of rust-lang#57685 - pthariensflame:enhancement/pin-impl-applicability, r=withoutboats
Enhance `Pin` impl applicability for `PartialEq` and `PartialOrd`. This allows for comparing for equality or ordering a `Pin<P>` and a `Pin<Q>` as long as `P` and `Q` are correspondingly comparable themselves *even when `P` and `Q` are different types*. An example might be comparing a `Pin<&mut OsString>` to a `Pin<&mut PathBuf>`, which might arise from pin projections from a pair of larger contexts that aren't `Unpin`.
2 parents b0563fd + fefe1da commit 25ea20d

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/libcore/pin.rs

+45-3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999

100100
use fmt;
101101
use marker::{Sized, Unpin};
102+
use cmp::{self, PartialEq, PartialOrd};
102103
use ops::{Deref, DerefMut, Receiver, CoerceUnsized, DispatchFromDyn};
103104

104105
/// A pinned pointer.
@@ -112,16 +113,57 @@ use ops::{Deref, DerefMut, Receiver, CoerceUnsized, DispatchFromDyn};
112113
/// [`Unpin`]: ../../std/marker/trait.Unpin.html
113114
/// [`pin` module]: ../../std/pin/index.html
114115
//
115-
// Note: the derives below are allowed because they all only use `&P`, so they
116-
// cannot move the value behind `pointer`.
116+
// Note: the derives below, and the explicit `PartialEq` and `PartialOrd`
117+
// implementations, are allowed because they all only use `&P`, so they cannot move
118+
// the value behind `pointer`.
117119
#[stable(feature = "pin", since = "1.33.0")]
118120
#[fundamental]
119121
#[repr(transparent)]
120-
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
122+
#[derive(Copy, Clone, Hash, Eq, Ord)]
121123
pub struct Pin<P> {
122124
pointer: P,
123125
}
124126

127+
#[stable(feature = "pin_partialeq_partialord_impl_applicability", since = "1.34.0")]
128+
impl<P, Q> PartialEq<Pin<Q>> for Pin<P>
129+
where
130+
P: PartialEq<Q>,
131+
{
132+
fn eq(&self, other: &Pin<Q>) -> bool {
133+
self.pointer == other.pointer
134+
}
135+
136+
fn ne(&self, other: &Pin<Q>) -> bool {
137+
self.pointer != other.pointer
138+
}
139+
}
140+
141+
#[stable(feature = "pin_partialeq_partialord_impl_applicability", since = "1.34.0")]
142+
impl<P, Q> PartialOrd<Pin<Q>> for Pin<P>
143+
where
144+
P: PartialOrd<Q>,
145+
{
146+
fn partial_cmp(&self, other: &Pin<Q>) -> Option<cmp::Ordering> {
147+
self.pointer.partial_cmp(&other.pointer)
148+
}
149+
150+
fn lt(&self, other: &Pin<Q>) -> bool {
151+
self.pointer < other.pointer
152+
}
153+
154+
fn le(&self, other: &Pin<Q>) -> bool {
155+
self.pointer <= other.pointer
156+
}
157+
158+
fn gt(&self, other: &Pin<Q>) -> bool {
159+
self.pointer > other.pointer
160+
}
161+
162+
fn ge(&self, other: &Pin<Q>) -> bool {
163+
self.pointer >= other.pointer
164+
}
165+
}
166+
125167
impl<P: Deref> Pin<P>
126168
where
127169
P::Target: Unpin,

0 commit comments

Comments
 (0)