Skip to content

Commit 87ecf54

Browse files
committed
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"`.
2 parents 1029775 + fbd3c92 commit 87ecf54

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Diff for: src/libstd/ffi/os_str.rs

+14
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,20 @@ impl PartialEq<OsString> for str {
417417
}
418418
}
419419

420+
#[stable(feature = "os_str_str_ref_eq", since = "1.28.0")]
421+
impl<'a> PartialEq<&'a str> for OsString {
422+
fn eq(&self, other: &&'a str) -> bool {
423+
**self == **other
424+
}
425+
}
426+
427+
#[stable(feature = "os_str_str_ref_eq", since = "1.28.0")]
428+
impl<'a> PartialEq<OsString> for &'a str {
429+
fn eq(&self, other: &OsString) -> bool {
430+
**other == **self
431+
}
432+
}
433+
420434
#[stable(feature = "rust1", since = "1.0.0")]
421435
impl Eq for OsString {}
422436

Diff for: src/test/run-pass/issue-49854.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::ffi::OsString;
12+
13+
fn main() {
14+
let os_str = OsString::from("Hello Rust!");
15+
16+
assert_eq!(os_str, "Hello Rust!");
17+
assert_eq!("Hello Rust!", os_str);
18+
}

0 commit comments

Comments
 (0)