Skip to content

Implement From and PartialEq for Path, PathBuf and Cow<Path> combinations #29532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,15 +963,15 @@ impl PartialEq for String {
macro_rules! impl_eq {
($lhs:ty, $rhs: ty) => {
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> PartialEq<$rhs> for $lhs {
impl<'a, 'b> PartialEq<$rhs> for $lhs {
#[inline]
fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&self[..], &other[..]) }
#[inline]
fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&self[..], &other[..]) }
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> PartialEq<$lhs> for $rhs {
impl<'a, 'b> PartialEq<$lhs> for $rhs {
#[inline]
fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&self[..], &other[..]) }
#[inline]
Expand All @@ -984,24 +984,9 @@ macro_rules! impl_eq {
impl_eq! { String, str }
impl_eq! { String, &'a str }
impl_eq! { Cow<'a, str>, str }
impl_eq! { Cow<'a, str>, &'b str }
impl_eq! { Cow<'a, str>, String }

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, 'b> PartialEq<&'b str> for Cow<'a, str> {
#[inline]
fn eq(&self, other: &&'b str) -> bool { PartialEq::eq(&self[..], &other[..]) }
#[inline]
fn ne(&self, other: &&'b str) -> bool { PartialEq::ne(&self[..], &other[..]) }
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, 'b> PartialEq<Cow<'a, str>> for &'b str {
#[inline]
fn eq(&self, other: &Cow<'a, str>) -> bool { PartialEq::eq(&self[..], &other[..]) }
#[inline]
fn ne(&self, other: &Cow<'a, str>) -> bool { PartialEq::ne(&self[..], &other[..]) }
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Default for String {
#[inline]
Expand Down
84 changes: 84 additions & 0 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,22 @@ impl<'a> IntoCow<'a, Path> for &'a Path {
}
}

#[stable(feature = "cow_from_path", since = "1.6.0")]
impl<'a> From<&'a Path> for Cow<'a, Path> {
#[inline]
fn from(s: &'a Path) -> Cow<'a, Path> {
Cow::Borrowed(s)
}
}

#[stable(feature = "cow_from_path", since = "1.6.0")]
impl<'a> From<PathBuf> for Cow<'a, Path> {
#[inline]
fn from(s: PathBuf) -> Cow<'a, Path> {
Cow::Owned(s)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ToOwned for Path {
type Owned = PathBuf;
Expand Down Expand Up @@ -1893,6 +1909,29 @@ impl<'a> IntoIterator for &'a Path {
fn into_iter(self) -> Iter<'a> { self.iter() }
}

macro_rules! impl_eq {
($lhs:ty, $rhs: ty) => {
#[stable(feature = "partialeq_path", since = "1.6.0")]
impl<'a, 'b> PartialEq<$rhs> for $lhs {
#[inline]
fn eq(&self, other: &$rhs) -> bool { <Path as PartialEq>::eq(self, other) }
}

#[stable(feature = "partialeq_path", since = "1.6.0")]
impl<'a, 'b> PartialEq<$lhs> for $rhs {
#[inline]
fn eq(&self, other: &$lhs) -> bool { <Path as PartialEq>::eq(self, other) }
}

}
}

impl_eq!(PathBuf, Path);
impl_eq!(PathBuf, &'a Path);
impl_eq!(Cow<'a, Path>, Path);
impl_eq!(Cow<'a, Path>, &'b Path);
impl_eq!(Cow<'a, Path>, PathBuf);

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -2002,6 +2041,26 @@ mod tests {
assert_eq!(static_cow_path, owned_cow_path);
}

#[test]
fn into() {
use borrow::Cow;

let static_path = Path::new("/home/foo");
let static_cow_path: Cow<'static, Path> = static_path.into();
let pathbuf = PathBuf::from("/home/foo");

{
let path: &Path = &pathbuf;
let borrowed_cow_path: Cow<Path> = path.into();

assert_eq!(static_cow_path, borrowed_cow_path);
}

let owned_cow_path: Cow<'static, Path> = pathbuf.into();

assert_eq!(static_cow_path, owned_cow_path);
}

#[test]
#[cfg(unix)]
pub fn test_decompositions_unix() {
Expand Down Expand Up @@ -3070,6 +3129,31 @@ mod tests {
tfe!("/", "foo", "/", false);
}

#[test]
fn test_eq_recievers() {
use borrow::Cow;

let borrowed: &Path = Path::new("foo/bar");
let mut owned: PathBuf = PathBuf::new();
owned.push("foo");
owned.push("bar");
let borrowed_cow: Cow<Path> = borrowed.into();
let owned_cow: Cow<Path> = owned.clone().into();

macro_rules! t {
($($current:expr),+) => {
$(
assert_eq!($current, borrowed);
assert_eq!($current, owned);
assert_eq!($current, borrowed_cow);
assert_eq!($current, owned_cow);
)+
}
}

t!(borrowed, owned, borrowed_cow, owned_cow);
}

#[test]
pub fn test_compare() {
use hash::{Hash, Hasher, SipHasher};
Expand Down