use anyhow::Result;
use std::sync::Arc;
use surreal_sync_core::InPlaceTransform;
use surreal_sync_core::{Change, Relation, Row};
#[derive(Debug, Clone)]
pub struct CowBatch<T> {
pub items: Vec<Arc<T>>,
}
impl<T> CowBatch<T> {
pub fn new(items: Vec<Arc<T>>) -> Self {
Self { items }
}
pub fn from_owned(items: Vec<T>) -> Self {
Self {
items: items.into_iter().map(Arc::new).collect(),
}
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub fn into_items(self) -> Vec<Arc<T>> {
self.items
}
}
impl CowBatch<Row> {
pub fn apply_inplace(&mut self, t: &impl InPlaceTransform) -> Result<()> {
for item in &mut self.items {
t.transform_row(Arc::make_mut(item))?;
}
Ok(())
}
}
impl CowBatch<Change> {
pub fn apply_inplace(&mut self, t: &impl InPlaceTransform) -> Result<()> {
for item in &mut self.items {
t.transform_change(Arc::make_mut(item))?;
}
Ok(())
}
}
impl CowBatch<Relation> {
pub fn apply_inplace(&mut self, t: &impl InPlaceTransform) -> Result<()> {
for item in &mut self.items {
t.transform_relation(Arc::make_mut(item))?;
}
Ok(())
}
}