Skip to content

Commit

Permalink
Implement Show for CString
Browse files Browse the repository at this point in the history
We use use `from_utf8_lossy` to convert it to a MaybeOwned string, to
avoid failing in case the CString contains invalid UTF-8
  • Loading branch information
aochagavia authored and alexcrichton committed Jul 24, 2014
1 parent 8836048 commit 6988bcd
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/librustrt/c_str.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6988bcd

Please sign in to comment.