Skip to content

Commit 2de428e

Browse files
committed
Rollup merge of rust-lang#32404 - WiSaGaN:feature/osstring-implement-default, r=aturon
Implement Default trait for OsString/OsStr Fixes rust-lang#32385
2 parents 26cfc26 + b5b1d06 commit 2de428e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/libstd/ffi/os_str.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ impl ops::Deref for OsString {
173173
}
174174
}
175175

176+
#[stable(feature = "osstring_default", since = "1.9.0")]
177+
impl Default for OsString {
178+
#[inline]
179+
fn default() -> OsString {
180+
OsString::new()
181+
}
182+
}
183+
176184
#[stable(feature = "rust1", since = "1.0.0")]
177185
impl Debug for OsString {
178186
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
@@ -302,6 +310,14 @@ impl OsStr {
302310
}
303311
}
304312

313+
#[stable(feature = "osstring_default", since = "1.9.0")]
314+
impl<'a> Default for &'a OsStr {
315+
#[inline]
316+
fn default() -> &'a OsStr {
317+
OsStr::new("")
318+
}
319+
}
320+
305321
#[stable(feature = "rust1", since = "1.0.0")]
306322
impl PartialEq for OsStr {
307323
fn eq(&self, other: &OsStr) -> bool {
@@ -554,6 +570,12 @@ mod tests {
554570
assert!(os_string.capacity() >= 33)
555571
}
556572

573+
#[test]
574+
fn test_os_string_default() {
575+
let os_string: OsString = Default::default();
576+
assert_eq!("", &os_string);
577+
}
578+
557579
#[test]
558580
fn test_os_str_is_empty() {
559581
let mut os_string = OsString::new();
@@ -577,4 +599,10 @@ mod tests {
577599
os_string.clear();
578600
assert_eq!(0, os_string.len());
579601
}
602+
603+
#[test]
604+
fn test_os_str_default() {
605+
let os_str: &OsStr = Default::default();
606+
assert_eq!("", os_str);
607+
}
580608
}

0 commit comments

Comments
 (0)