]> git.lizzy.rs Git - rust.git/commitdiff
Add `Default` implementation for `&CStr` and `CString`
authorTobias Bucher <tobiasbucher5991@gmail.com>
Fri, 15 Apr 2016 15:58:59 +0000 (17:58 +0200)
committerTobias Bucher <tobiasbucher5991@gmail.com>
Thu, 5 May 2016 08:57:06 +0000 (10:57 +0200)
src/libstd/ffi/c_str.rs

index 52d7bb128d5a21423af9fe26f080328521d67eb1..fbc8886ce8f8cd98589f2cd52dcf33421dc68d3c 100644 (file)
@@ -326,6 +326,22 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
     }
 }
 
+#[stable(feature = "cstr_default", since = "1.10.0")]
+impl<'a> Default for &'a CStr {
+    fn default() -> &'a CStr {
+        static SLICE: &'static [c_char] = &[0];
+        unsafe { CStr::from_ptr(SLICE.as_ptr()) }
+    }
+}
+
+#[stable(feature = "cstr_default", since = "1.10.0")]
+impl Default for CString {
+    fn default() -> CString {
+        let a: &CStr = Default::default();
+        a.to_owned()
+    }
+}
+
 #[stable(feature = "cstr_borrow", since = "1.3.0")]
 impl Borrow<CStr> for CString {
     fn borrow(&self) -> &CStr { self }