Skip to content

Commit 6988bcd

Browse files
aochagaviaalexcrichton
authored andcommitted
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
1 parent 8836048 commit 6988bcd

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/librustrt/c_str.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ use collections::hash;
7373
use core::kinds::marker;
7474
use core::mem;
7575
use core::ptr;
76+
use core::fmt;
7677
use core::raw::Slice;
7778
use core::slice;
7879
use core::str;
@@ -344,6 +345,12 @@ impl Collection for CString {
344345
}
345346
}
346347

348+
impl fmt::Show for CString {
349+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
350+
String::from_utf8_lossy(self.as_bytes_no_nul()).fmt(f)
351+
}
352+
}
353+
347354
/// A generic trait for converting a value to a CString.
348355
pub trait ToCStr {
349356
/// Copy the receiver into a CString.

0 commit comments

Comments
 (0)