Skip to content

Commit 2a9e763

Browse files
committed
std: implement Container for CString
1 parent 0bdc99d commit 2a9e763

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/libstd/c_str.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ impl CString {
152152
pub fn as_bytes<'a>(&'a self) -> &'a [u8] {
153153
if self.buf.is_null() { fail!("CString is null!"); }
154154
unsafe {
155-
let len = ptr::position(self.buf, |c| *c == 0);
156-
cast::transmute((self.buf, len + 1))
155+
cast::transmute((self.buf, self.len() + 1))
157156
}
158157
}
159158

@@ -187,6 +186,15 @@ impl Drop for CString {
187186
}
188187
}
189188

189+
impl Container for CString {
190+
#[inline]
191+
fn len(&self) -> uint {
192+
unsafe {
193+
ptr::position(self.buf, |c| *c == 0)
194+
}
195+
}
196+
}
197+
190198
/// A generic trait for converting a value to a CString.
191199
pub trait ToCStr {
192200
/// Copy the receiver into a CString.

0 commit comments

Comments
 (0)