use std::ops::{Deref, DerefMut};
#[derive(Debug, Default)]
pub struct Fragment<T> {
pub(crate) data: Vec<T>,
}
impl<T> Fragment<T> {
pub fn new_with_first_value(capacity: usize, first_value: T) -> Self {
let mut data = Vec::with_capacity(capacity);
data.push(first_value);
Self { data }
}
pub fn new(capacity: usize) -> Self {
Self {
data: Vec::with_capacity(capacity),
}
}
pub fn has_capacity_for_one(&self) -> bool {
self.data.len() < self.data.capacity()
}
pub fn room(&self) -> usize {
self.data.capacity() - self.data.len()
}
}
impl<T> Deref for Fragment<T> {
type Target = Vec<T>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<T> DerefMut for Fragment<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
impl<T> From<Vec<T>> for Fragment<T> {
fn from(value: Vec<T>) -> Self {
Self { data: value }
}
}
impl<T> From<Fragment<T>> for Vec<T> {
fn from(value: Fragment<T>) -> Self {
value.data
}
}