about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-06-30 04:03:14 +0000
committerbors <bors@rust-lang.org>2018-06-30 04:03:14 +0000
commit87ecf5442ced38a6253e670dd6d87c0c334b21fb (patch)
tree9e43199505bf86e98bdb2dcd0acc0abd4def8352
parent1029775ad5f15d2200e087ebb4a867cf30dbbb25 (diff)
parentfbd3c92a885b02a5397eaf65d36beae8e1ed999c (diff)
downloadrust-87ecf5442ced38a6253e670dd6d87c0c334b21fb.tar.gz
rust-87ecf5442ced38a6253e670dd6d87c0c334b21fb.zip
Auto merge of #51178 - GabrielMajeri:os-str-compare, r=SimonSapin
Implement PartialEq between &str and OsString

This fixes #49854.

It allows equality comparison between `OsString` values and `str` references, such as `os_string == "something"`.
-rw-r--r--src/libstd/ffi/os_str.rs14
-rw-r--r--src/test/run-pass/issue-49854.rs18
2 files changed, 32 insertions, 0 deletions
diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs
index 0a3148029d0..4ada6a77a8e 100644
--- a/src/libstd/ffi/os_str.rs
+++ b/src/libstd/ffi/os_str.rs
@@ -417,6 +417,20 @@ impl PartialEq<OsString> for str {
     }
 }
 
+#[stable(feature = "os_str_str_ref_eq", since = "1.28.0")]
+impl<'a> PartialEq<&'a str> for OsString {
+    fn eq(&self, other: &&'a str) -> bool {
+        **self == **other
+    }
+}
+
+#[stable(feature = "os_str_str_ref_eq", since = "1.28.0")]
+impl<'a> PartialEq<OsString> for &'a str {
+    fn eq(&self, other: &OsString) -> bool {
+        **other == **self
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl Eq for OsString {}
 
diff --git a/src/test/run-pass/issue-49854.rs b/src/test/run-pass/issue-49854.rs
new file mode 100644
index 00000000000..5bb80b5e2c6
--- /dev/null
+++ b/src/test/run-pass/issue-49854.rs
@@ -0,0 +1,18 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use std::ffi::OsString;
+
+fn main() {
+    let os_str = OsString::from("Hello Rust!");
+
+    assert_eq!(os_str, "Hello Rust!");
+    assert_eq!("Hello Rust!", os_str);
+}