diff options
Diffstat (limited to 'compiler/rustc_data_structures')
| -rw-r--r-- | compiler/rustc_data_structures/src/small_c_str.rs | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/small_c_str.rs b/compiler/rustc_data_structures/src/small_c_str.rs index 33e72914e19..cd902524562 100644 --- a/compiler/rustc_data_structures/src/small_c_str.rs +++ b/compiler/rustc_data_structures/src/small_c_str.rs @@ -66,3 +66,15 @@ impl Deref for SmallCStr { self.as_c_str() } } + +impl<'a> FromIterator<&'a str> for SmallCStr { + fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self { + let mut data = + iter.into_iter().flat_map(|s| s.as_bytes()).copied().collect::<SmallVec<_>>(); + data.push(0); + if let Err(e) = ffi::CStr::from_bytes_with_nul(&data) { + panic!("The iterator {:?} cannot be converted into a CStr: {}", data, e); + } + Self { data } + } +} |
