4 use smallvec
::SmallVec
;
9 const SIZE
: usize = 36;
11 /// Like SmallVec but for C strings.
13 pub struct SmallCStr
{
14 data
: SmallVec
<[u8; SIZE
]>,
19 pub fn new(s
: &str) -> SmallCStr
{
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
)
27 let mut data
= Vec
::with_capacity(len1
);
28 data
.extend_from_slice(s
.as_bytes());
30 SmallVec
::from_vec(data
)
32 if let Err(e
) = ffi
::CStr
::from_bytes_with_nul(&data
) {
33 panic
!("The string \"{}\" cannot be converted into a CStr: {}", s
, e
);
39 pub fn new_with_nul(s
: &str) -> SmallCStr
{
41 if let Err(e
) = ffi
::CStr
::from_bytes_with_nul(b
) {
42 panic
!("The string \"{}\" cannot be converted into a CStr: {}", s
, e
);
44 SmallCStr { data: SmallVec::from_slice(s.as_bytes()) }
48 pub fn as_c_str(&self) -> &ffi
::CStr
{
49 unsafe { ffi::CStr::from_bytes_with_nul_unchecked(&self.data[..]) }
53 pub fn len_with_nul(&self) -> usize {
57 pub fn spilled(&self) -> bool
{
62 impl Deref
for SmallCStr
{
63 type Target
= ffi
::CStr
;
65 fn deref(&self) -> &ffi
::CStr
{