#[allow(unused_imports)]
use super::*;
use crate::Result;
pub struct VecWriter {
buffer: alloc::vec::Vec<u8>,
position: u64,
}
impl VecWriter {
pub fn new() -> Self {
Self {
buffer: alloc::vec::Vec::new(),
position: 0,
}
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
buffer: alloc::vec::Vec::with_capacity(capacity),
position: 0,
}
}
pub fn into_inner(self) -> alloc::vec::Vec<u8> {
self.buffer
}
pub fn as_slice(&self) -> &[u8] {
&self.buffer
}
pub fn len(&self) -> usize {
self.buffer.len()
}
pub fn is_empty(&self) -> bool {
self.buffer.is_empty()
}
}
impl Default for VecWriter {
fn default() -> Self {
Self::new()
}
}
impl MdfWrite for VecWriter {
fn write_all(&mut self, bytes: &[u8]) -> Result<()> {
let pos = self.position as usize;
let end = pos + bytes.len();
if end > self.buffer.len() {
self.buffer.resize(end, 0);
}
self.buffer[pos..end].copy_from_slice(bytes);
self.position = end as u64;
Ok(())
}
fn seek(&mut self, pos: u64) -> Result<u64> {
self.position = pos;
Ok(self.position)
}
fn position(&self) -> u64 {
self.position
}
fn flush(&mut self) -> Result<()> {
Ok(())
}
}