diff --git a/compiler/rustc_data_structures/src/small_c_str.rs b/compiler/rustc_data_structures/src/small_c_str.rs index 349fd7f9769d9..809ce3d44832e 100644 --- a/compiler/rustc_data_structures/src/small_c_str.rs +++ b/compiler/rustc_data_structures/src/small_c_str.rs @@ -82,6 +82,6 @@ impl<'a> FromIterator<&'a str> for SmallCStr { impl From<&ffi::CStr> for SmallCStr { fn from(s: &ffi::CStr) -> Self { - Self { data: SmallVec::from_slice(s.to_bytes()) } + Self { data: SmallVec::from_slice(s.to_bytes_with_nul()) } } } diff --git a/compiler/rustc_data_structures/src/small_c_str/tests.rs b/compiler/rustc_data_structures/src/small_c_str/tests.rs index 47277604b2b71..7b975dadcb767 100644 --- a/compiler/rustc_data_structures/src/small_c_str/tests.rs +++ b/compiler/rustc_data_structures/src/small_c_str/tests.rs @@ -43,3 +43,11 @@ fn long() { fn internal_nul() { let _ = SmallCStr::new("abcd\0def"); } + +#[test] +fn from_cstr() { + let c = c"foo"; + let s: SmallCStr = c.into(); + assert_eq!(s.len_with_nul(), 4); + assert_eq!(s.as_c_str(), c"foo"); +}