From 9c12bf2e8815dc9b42be62f1e1fd2932dd02938b Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 31 Oct 2026 19:53:27 +0100 Subject: use Automerge as Document instead of AutoCommit This allows a lot of operations to drop mut. --- src/document/changes.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'src/document/changes.rs') diff --git a/src/document/changes.rs b/src/document/changes.rs index 33029fc..92b7744 100644 --- a/src/document/changes.rs +++ b/src/document/changes.rs @@ -6,11 +6,10 @@ use std::collections::{HashMap, HashSet}; use anyhow::{Context, Result, anyhow, bail}; -use super::{Change, ChangeHash, Document}; +use super::{Change, ChangeHash, Document, ReadDoc}; -/// Those of `heads` that `doc` doesn't have. (Looking a change up needs the document -/// mutably.) -pub(crate) fn missing(doc: &mut Document, heads: &[ChangeHash]) -> Vec { +/// Those of `heads` that `doc` doesn't have. +pub(crate) fn missing(doc: &impl ReadDoc, heads: &[ChangeHash]) -> Vec { heads .iter() .copied() @@ -19,8 +18,8 @@ pub(crate) fn missing(doc: &mut Document, heads: &[ChangeHash]) -> Vec Vec { +/// writers' clocks (`Replica::transact`); changes without a time (snapshots) left out. +pub(crate) fn commit_times(doc: &Document, since: &[ChangeHash]) -> Vec { doc.get_changes(since) .iter() .map(|change| change.timestamp()) @@ -29,7 +28,7 @@ pub(crate) fn commit_times(doc: &mut Document, since: &[ChangeHash]) -> Vec } /// The change `hash` as the document encodes it; None if `doc` doesn't have it. -pub(crate) fn change_bytes(doc: &mut Document, hash: &ChangeHash) -> Option> { +pub(crate) fn change_bytes(doc: &Document, hash: &ChangeHash) -> Option> { doc.get_change_by_hash(hash) .map(|change| change.raw_bytes().to_vec()) } @@ -49,7 +48,7 @@ pub(crate) fn change_from_bytes(bytes: Vec, hash: ChangeHash) -> Result Option> { @@ -147,8 +146,8 @@ mod tests { // Beyond `to`: must stay out of the delta. random_write(rng, &mut two); - let bytes = delta_bytes(&mut one, &from, &to).unwrap(); - assert_eq!(delta_bytes(&mut two, &from, &to).unwrap(), bytes); + let bytes = delta_bytes(&one, &from, &to).unwrap(); + assert_eq!(delta_bytes(&two, &from, &to).unwrap(), bytes); let mut behind = base.fork(); load_delta(&mut behind, &bytes).unwrap(); @@ -160,7 +159,7 @@ mod tests { fn no_delta_without_its_heads() { let mut doc = Document::new(); change(&mut doc, |doc| doc.put(ROOT, "k", 1_i64).unwrap()); - assert!(delta_bytes(&mut doc, &[], &[ChangeHash([7; 32])]).is_none()); + assert!(delta_bytes(&doc, &[], &[ChangeHash([7; 32])]).is_none()); } /// A change loads from its bytes only as the change it was asked for. @@ -169,9 +168,9 @@ mod tests { let mut doc = Document::new(); change(&mut doc, |doc| doc.put(ROOT, "k", 1_i64).unwrap()); let hash = doc.get_heads()[0]; - let bytes = change_bytes(&mut doc, &hash).unwrap(); + let bytes = change_bytes(&doc, &hash).unwrap(); assert_eq!(change_from_bytes(bytes.clone(), hash).unwrap().hash(), hash); assert!(change_from_bytes(bytes, ChangeHash([7; 32])).is_err()); - assert!(change_bytes(&mut doc, &ChangeHash([7; 32])).is_none()); + assert!(change_bytes(&doc, &ChangeHash([7; 32])).is_none()); } } -- cgit v1.3.1