aboutsummaryrefslogtreecommitdiffstats
path: root/src/document/changes.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-10-31 19:53:27 +0100
committertv <tv@krebsco.de>2026-10-31 19:53:27 +0100
commit6105507200231e67ba544c12aa10b988e1c23826 (patch)
tree880530b7401cb5d43f52e06a68b4d704326aa272 /src/document/changes.rs
parent8d7adfbdb7798cca9d9783761f290e3bd85d9a3a (diff)
use Automerge as Document instead of AutoCommit
This allows a lot of operations to drop mut.
Diffstat (limited to 'src/document/changes.rs')
-rw-r--r--src/document/changes.rs25
1 files changed, 12 insertions, 13 deletions
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<ChangeHash> {
+/// Those of `heads` that `doc` doesn't have.
+pub(crate) fn missing(doc: &impl ReadDoc, heads: &[ChangeHash]) -> Vec<ChangeHash> {
heads
.iter()
.copied()
@@ -19,8 +18,8 @@ pub(crate) fn missing(doc: &mut Document, heads: &[ChangeHash]) -> Vec<ChangeHas
}
/// When the changes after `since` were committed, in Unix milliseconds by their
-/// writers' clocks (`Replica::commit`); changes without a time (snapshots) left out.
-pub(crate) fn commit_times(doc: &mut Document, since: &[ChangeHash]) -> Vec<i64> {
+/// writers' clocks (`Replica::transact`); changes without a time (snapshots) left out.
+pub(crate) fn commit_times(doc: &Document, since: &[ChangeHash]) -> Vec<i64> {
doc.get_changes(since)
.iter()
.map(|change| change.timestamp())
@@ -29,7 +28,7 @@ pub(crate) fn commit_times(doc: &mut Document, since: &[ChangeHash]) -> Vec<i64>
}
/// 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<Vec<u8>> {
+pub(crate) fn change_bytes(doc: &Document, hash: &ChangeHash) -> Option<Vec<u8>> {
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<u8>, hash: ChangeHash) -> Result<Chan
/// (`load_delta` reads that). The same on every node holding `to`, whatever else it
/// has. None if `doc` lacks part of `to`.
pub(crate) fn delta_bytes(
- doc: &mut Document,
+ doc: &Document,
from: &[ChangeHash],
to: &[ChangeHash],
) -> Option<Vec<u8>> {
@@ -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());
}
}