rust-sen/sehn-serde/src/ser/string_writer.rs

29 lines
497 B
Rust

use std::io;
use std::string::FromUtf8Error;
pub struct StringWriter {
buf: Vec<u8>
}
impl StringWriter {
pub fn new() -> Self {
Self {
buf: Vec::new()
}
}
pub fn to_string(self) -> Result<String, FromUtf8Error> {
String::from_utf8(self.buf)
}
}
impl io::Write for StringWriter {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
self.buf.extend_from_slice(bytes);
Ok(bytes.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}