use crate::mm::Edit;
#[derive(Debug, Clone, Default)]
pub struct Transaction {
edits: Vec<Edit>,
}
impl Transaction {
#[must_use]
pub const fn new() -> Self {
Self { edits: Vec::new() }
}
pub fn iter(&self) -> std::slice::Iter<'_, Edit> {
self.edits.iter()
}
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
Self {
edits: Vec::with_capacity(capacity),
}
}
pub fn push(&mut self, edit: Edit) {
self.edits.push(edit);
}
#[must_use]
pub fn edits(&self) -> &[Edit] {
&self.edits
}
#[must_use]
pub fn into_edits(self) -> Vec<Edit> {
self.edits
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.edits.is_empty()
}
#[must_use]
pub const fn len(&self) -> usize {
self.edits.len()
}
#[must_use]
pub fn inverse(&self) -> Self {
Self {
edits: self.edits.iter().rev().map(Edit::inverse).collect(),
}
}
pub fn clear(&mut self) {
self.edits.clear();
}
}
impl From<Vec<Edit>> for Transaction {
fn from(edits: Vec<Edit>) -> Self {
Self { edits }
}
}
impl From<Edit> for Transaction {
fn from(edit: Edit) -> Self {
Self { edits: vec![edit] }
}
}
impl IntoIterator for Transaction {
type Item = Edit;
type IntoIter = std::vec::IntoIter<Edit>;
fn into_iter(self) -> Self::IntoIter {
self.edits.into_iter()
}
}
impl<'a> IntoIterator for &'a Transaction {
type Item = &'a Edit;
type IntoIter = std::slice::Iter<'a, Edit>;
fn into_iter(self) -> Self::IntoIter {
self.edits.iter()
}
}