]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/small_c_str.rs
librustc_data_structures: Unconfigure tests during normal build
[rust.git] / src / librustc_data_structures / small_c_str.rs
1 use std::ffi;
2 use std::ops::Deref;
3
4 use smallvec::SmallVec;
5
6 #[cfg(test)]
7 mod tests;
8
9 const SIZE: usize = 36;
10
11 /// Like SmallVec but for C strings.
12 #[derive(Clone)]
13 pub struct SmallCStr {
14     data: SmallVec<[u8; SIZE]>,
15 }
16
17 impl SmallCStr {
18     #[inline]
19     pub fn new(s: &str) -> SmallCStr {
20         let len = s.len();
21         let len1 = len + 1;
22         let data = if len < SIZE {
23             let mut buf = [0; SIZE];
24             buf[..len].copy_from_slice(s.as_bytes());
25             SmallVec::from_buf_and_len(buf, len1)
26         } else {
27             let mut data = Vec::with_capacity(len1);
28             data.extend_from_slice(s.as_bytes());
29             data.push(0);
30             SmallVec::from_vec(data)
31         };
32         if let Err(e) = ffi::CStr::from_bytes_with_nul(&data) {
33             panic!("The string \"{}\" cannot be converted into a CStr: {}", s, e);
34         }
35         SmallCStr { data }
36     }
37
38     #[inline]
39     pub fn new_with_nul(s: &str) -> SmallCStr {
40         let b = s.as_bytes();
41         if let Err(e) = ffi::CStr::from_bytes_with_nul(b) {
42             panic!("The string \"{}\" cannot be converted into a CStr: {}", s, e);
43         }
44         SmallCStr { data: SmallVec::from_slice(s.as_bytes()) }
45     }
46
47
48     #[inline]
49     pub fn as_c_str(&self) -> &ffi::CStr {
50         unsafe {
51             ffi::CStr::from_bytes_with_nul_unchecked(&self.data[..])
52         }
53     }
54
55     #[inline]
56     pub fn len_with_nul(&self) -> usize {
57         self.data.len()
58     }
59
60     pub fn spilled(&self) -> bool {
61         self.data.spilled()
62     }
63 }
64
65 impl Deref for SmallCStr {
66     type Target = ffi::CStr;
67
68     fn deref(&self) -> &ffi::CStr {
69         self.as_c_str()
70     }
71 }