use super::{Change, ChangeSet, IChangeSet};
use std::fmt;
use std::ops::Deref;
pub struct ChangeSetWithContext<T, K, X> {
changes: ChangeSet<T, K>,
context: X,
}
impl<T, K, X: Clone> Clone for ChangeSetWithContext<T, K, X> {
fn clone(&self) -> Self {
Self { changes: self.changes.clone(), context: self.context.clone() }
}
}
impl<T, K, X> ChangeSetWithContext<T, K, X> {
pub fn new(collection: ChangeSet<T, K>, context: X) -> Self {
Self { changes: collection, context }
}
pub fn empty(context: X) -> Self {
Self::new(ChangeSet::empty(), context)
}
pub fn context(&self) -> &X {
&self.context
}
pub fn changes(&self) -> &ChangeSet<T, K> {
&self.changes
}
}
impl<T, K, X> Deref for ChangeSetWithContext<T, K, X> {
type Target = ChangeSet<T, K>;
fn deref(&self) -> &Self::Target {
&self.changes
}
}
impl<'a, T, K, X> IntoIterator for &'a ChangeSetWithContext<T, K, X> {
type Item = &'a Change<T, K>;
type IntoIter = std::slice::Iter<'a, Change<T, K>>;
fn into_iter(self) -> Self::IntoIter {
self.changes.changes().iter()
}
}
impl<T: 'static, K: 'static, X: Clone + 'static> IChangeSet<T, K> for ChangeSetWithContext<T, K, X> {
fn change_set(&self) -> &ChangeSet<T, K> {
&self.changes
}
}
impl<T: fmt::Debug, K: fmt::Debug, X: fmt::Debug> fmt::Debug for ChangeSetWithContext<T, K, X> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ChangeSetWithContext").field("changes", &self.changes).field("context", &self.context).finish()
}
}