Implement Show for CString

We use use `from_utf8_lossy` to convert it to a MaybeOwned string, to
avoid failing in case the CString contains invalid UTF-8
This commit is contained in:
Adolfo Ochagavía 2014-07-21 18:06:24 +02:00 committed by Alex Crichton
parent 8836048c76
commit 6988bcd74c

View File

@ -73,6 +73,7 @@ use collections::hash;
use core::kinds::marker;
use core::mem;
use core::ptr;
use core::fmt;
use core::raw::Slice;
use core::slice;
use core::str;
@ -344,6 +345,12 @@ impl Collection for CString {
}
}
impl fmt::Show for CString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
String::from_utf8_lossy(self.as_bytes_no_nul()).fmt(f)
}
}
/// A generic trait for converting a value to a CString.
pub trait ToCStr {
/// Copy the receiver into a CString.