@eefriedman: The primary purpose of CStr is to be able to write efficient safe wrapper functions around FFI functions.
mod ffi {
extern {
fn strlen(s:*mut libc::c_char) -> libc::size_t;
}
}
pub fn strlen(s: &CStr) -> libc::size_t {
unsafe { ffi::strlen(s.as_ptr()) }
}
Being able to directly use &CStr in FFI declarations would be nice. The idea was that CStr should be an unsized type:
struct CStr { data: libc::c_char }
impl !Sized for CStr {}
Unlike the current DST CStr, with an unsized type, &CStr would be a thin pointer that can be used for FFI.
However, Rust currently doesn’t support unsized types – it only has statically sized types (Sized) and dynamically sized types (DST). Since removing Sized from a type is a breaking change, CStr was made dynamically sized so that it can be turned unsized in the future.
Using a ...Ref<'a> instead of a real reference avoids the need for unsized types in the language, but it shouldn’t be added to the standard library as long as there’s a chance we’ll get real unsized types.