Skip to content

Commit

Permalink
Rollup merge of rust-lang#57685 - pthariensflame:enhancement/pin-impl…
Browse files Browse the repository at this point in the history
…-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`.
  • Loading branch information
Centril committed Jan 18, 2019
2 parents 3135ddb + fefe1da commit 06786bb
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/libcore/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@

use fmt;
use marker::{Sized, Unpin};
use cmp::{self, PartialEq, PartialOrd};
use ops::{Deref, DerefMut, Receiver, CoerceUnsized, DispatchFromDyn};

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

#[stable(feature = "pin_partialeq_partialord_impl_applicability", since = "1.34.0")]
impl<P, Q> PartialEq<Pin<Q>> for Pin<P>
where
P: PartialEq<Q>,
{
fn eq(&self, other: &Pin<Q>) -> bool {
self.pointer == other.pointer
}

fn ne(&self, other: &Pin<Q>) -> bool {
self.pointer != other.pointer
}
}

#[stable(feature = "pin_partialeq_partialord_impl_applicability", since = "1.34.0")]
impl<P, Q> PartialOrd<Pin<Q>> for Pin<P>
where
P: PartialOrd<Q>,
{
fn partial_cmp(&self, other: &Pin<Q>) -> Option<cmp::Ordering> {
self.pointer.partial_cmp(&other.pointer)
}

fn lt(&self, other: &Pin<Q>) -> bool {
self.pointer < other.pointer
}

fn le(&self, other: &Pin<Q>) -> bool {
self.pointer <= other.pointer
}

fn gt(&self, other: &Pin<Q>) -> bool {
self.pointer > other.pointer
}

fn ge(&self, other: &Pin<Q>) -> bool {
self.pointer >= other.pointer
}
}

impl<P: Deref> Pin<P>
where
P::Target: Unpin,
Expand Down

0 comments on commit 06786bb

Please sign in to comment.