std: implement Container for CString

This commit is contained in:
Erick Tryzelaar 2013-09-18 12:21:30 -07:00
parent 0bdc99d81f
commit 2a9e763304

View File

@ -152,8 +152,7 @@ impl CString {
pub fn as_bytes<'a>(&'a self) -> &'a [u8] {
if self.buf.is_null() { fail!("CString is null!"); }
unsafe {
let len = ptr::position(self.buf, |c| *c == 0);
cast::transmute((self.buf, len + 1))
cast::transmute((self.buf, self.len() + 1))
}
}
@ -187,6 +186,15 @@ impl Drop for CString {
}
}
impl Container for CString {
#[inline]
fn len(&self) -> uint {
unsafe {
ptr::position(self.buf, |c| *c == 0)
}
}
}
/// A generic trait for converting a value to a CString.
pub trait ToCStr {
/// Copy the receiver into a CString.