Skip to content

Commit 40d0b44

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 ae793ab + 22251a8 commit 40d0b44

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

src/libcore/pin.rs

+47-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,59 @@ 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+
133+
fn eq(&self, other: &Pin<Q>) -> bool {
134+
self.pointer == other.pointer
135+
}
136+
137+
fn ne(&self, other: &Pin<Q>) -> bool {
138+
self.pointer != other.pointer
139+
}
140+
}
141+
142+
#[stable(feature = "pin_partialeq_partialord_impl_applicability", since = "1.34.0")]
143+
impl<P, Q> PartialOrd<Pin<Q>> for Pin<P>
144+
where
145+
P: PartialOrd<Q>,
146+
{
147+
148+
fn partial_cmp(&self, other: &Pin<Q>) -> Option<cmp::Ordering> {
149+
self.pointer.partial_cmp(&other.pointer)
150+
}
151+
152+
fn lt(&self, other: &Pin<Q>) -> bool {
153+
self.pointer < other.pointer
154+
}
155+
156+
fn le(&self, other: &Pin<Q>) -> bool {
157+
self.pointer <= other.pointer
158+
}
159+
160+
fn gt(&self, other: &Pin<Q>) -> bool {
161+
self.pointer > other.pointer
162+
}
163+
164+
fn ge(&self, other: &Pin<Q>) -> bool {
165+
self.pointer >= other.pointer
166+
}
167+
}
168+
125169
impl<P: Deref> Pin<P>
126170
where
127171
P::Target: Unpin,

0 commit comments

Comments
 (0)