Skip to content

Commit

Permalink
Auto merge of #51178 - GabrielMajeri:os-str-compare, r=SimonSapin
Browse files Browse the repository at this point in the history
Implement PartialEq between &str and OsString

This fixes #49854.

It allows equality comparison between `OsString` values and `str` references, such as `os_string == "something"`.
  • Loading branch information
bors committed Jun 30, 2018
2 parents 1029775 + fbd3c92 commit 87ecf54
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down
18 changes: 18 additions & 0 deletions src/test/run-pass/issue-49854.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 87ecf54

Please sign in to comment.