use std::iter;
use std::os::raw::c_char;
use std::str;
use std::str::Utf8Error;
use std::string::FromUtf8Error;
#[derive(Debug)]
pub struct StringBuffer {
bytes: Vec<u8>,
}
#[allow(dead_code)]
impl StringBuffer {
pub fn new(length: usize) -> StringBuffer {
StringBuffer {
bytes: iter::repeat(b'\0').take(length).collect(),
}
}
pub unsafe fn as_mut_ptr(&mut self) -> *mut c_char {
self.bytes.as_mut_ptr() as *mut c_char
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
&mut self.bytes
}
pub fn as_str(&self) -> Result<&str, Utf8Error> {
let chars_before_null = self.bytes.iter().take_while(|&&c| c != b'\0').count();
str::from_utf8(&self.bytes[..chars_before_null])
}
pub fn into_string(self) -> Result<String, FromUtf8Error> {
let chars_before_null = self.bytes.into_iter().take_while(|&c| c != b'\0');
String::from_utf8(chars_before_null.collect())
}
}