Skip to content

Commit

Permalink
Merge pull request automerge#380 from jkankiewicz/add_syncing_to_C_API
Browse files Browse the repository at this point in the history
Add syncing to C API
  • Loading branch information
orionz authored Jun 1, 2022
2 parents b1712cb + fbdb5da commit de25e8f
Show file tree
Hide file tree
Showing 25 changed files with 4,024 additions and 1,626 deletions.
58 changes: 29 additions & 29 deletions automerge-c/examples/quickstart.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,80 +22,80 @@ int main(int argc, char** argv) {
AMobjId const* const card1 = value.obj_id;
AMresult* result = AMmapPutStr(doc1, card1, "title", "Rewrite everything in Clojure");
test(result, AM_VALUE_VOID);
AMfreeResult(result);
AMresultFree(result);
result = AMmapPutBool(doc1, card1, "done", false);
test(result, AM_VALUE_VOID);
AMfreeResult(result);
AMresultFree(result);
AMresult* const card2_result = AMlistPutObject(doc1, cards, 0, true, AM_OBJ_TYPE_MAP);
value = test(card2_result, AM_VALUE_OBJ_ID);
AMobjId const* const card2 = value.obj_id;
result = AMmapPutStr(doc1, card2, "title", "Rewrite everything in Haskell");
test(result, AM_VALUE_VOID);
AMfreeResult(result);
AMresultFree(result);
result = AMmapPutBool(doc1, card2, "done", false);
test(result, AM_VALUE_VOID);
AMfreeResult(result);
AMfreeResult(card2_result);
AMresultFree(result);
AMresultFree(card2_result);
result = AMcommit(doc1, "Add card", NULL);
test(result, AM_VALUE_CHANGE_HASHES);
AMfreeResult(result);
AMresultFree(result);

AMdoc* doc2 = AMcreate();
if (doc2 == NULL) {
fprintf(stderr, "`AMcreate()` failure.");
AMfreeResult(card1_result);
AMfreeResult(cards_result);
AMfreeDoc(doc1);
AMresultFree(card1_result);
AMresultFree(cards_result);
AMfree(doc1);
exit(EXIT_FAILURE);
}
result = AMmerge(doc2, doc1);
test(result, AM_VALUE_CHANGE_HASHES);
AMfreeResult(result);
AMfreeDoc(doc2);
AMresultFree(result);
AMfree(doc2);

AMresult* const save_result = AMsave(doc1);
value = test(save_result, AM_VALUE_BYTES);
AMbyteSpan binary = value.bytes;
doc2 = AMload(binary.src, binary.count);
AMfreeResult(save_result);
AMresultFree(save_result);
if (doc2 == NULL) {
fprintf(stderr, "`AMload()` failure.");
AMfreeResult(card1_result);
AMfreeResult(cards_result);
AMfreeDoc(doc1);
AMresultFree(card1_result);
AMresultFree(cards_result);
AMfree(doc1);
exit(EXIT_FAILURE);
}

result = AMmapPutBool(doc1, card1, "done", true);
test(result, AM_VALUE_VOID);
AMfreeResult(result);
AMresultFree(result);
result = AMcommit(doc1, "Mark card as done", NULL);
test(result, AM_VALUE_CHANGE_HASHES);
AMfreeResult(result);
AMfreeResult(card1_result);
AMresultFree(result);
AMresultFree(card1_result);

result = AMlistDelete(doc2, cards, 0);
test(result, AM_VALUE_VOID);
AMfreeResult(result);
AMresultFree(result);
result = AMcommit(doc2, "Delete card", NULL);
test(result, AM_VALUE_CHANGE_HASHES);
AMfreeResult(result);
AMresultFree(result);

result = AMmerge(doc1, doc2);
test(result, AM_VALUE_CHANGE_HASHES);
AMfreeResult(result);
AMfreeDoc(doc2);
AMresultFree(result);
AMfree(doc2);

result = AMgetChanges(doc1, NULL);
value = test(result, AM_VALUE_CHANGES);
AMchange const* change = NULL;
while (value.changes.ptr && (change = AMnextChange(&value.changes, 1))) {
while (value.changes.ptr && (change = AMchangesNext(&value.changes, 1))) {
size_t const size = AMobjSizeAt(doc1, cards, change);
printf("%s %ld\n", AMgetMessage(change), size);
printf("%s %ld\n", AMchangeMessage(change), size);
}
AMfreeResult(result);
AMfreeResult(cards_result);
AMfreeDoc(doc1);
AMresultFree(result);
AMresultFree(cards_result);
AMfree(doc1);
}

/**
Expand Down Expand Up @@ -123,7 +123,7 @@ AMvalue test(AMresult* result, AMvalueVariant const value_tag) {
default: sprintf(prelude, "Unknown `AMstatus` tag %d", status);
}
fprintf(stderr, "%s; %s.", prelude, AMerrorMessage(result));
AMfreeResult(result);
AMresultFree(result);
exit(EXIT_FAILURE);
}
AMvalue const value = AMresultValue(result, 0);
Expand All @@ -147,7 +147,7 @@ AMvalue test(AMresult* result, AMvalueVariant const value_tag) {
default: label = "<unknown>";
}
fprintf(stderr, "Unexpected `AMvalueVariant` tag `%s` (%d).", label, value.tag);
AMfreeResult(result);
AMresultFree(result);
exit(EXIT_FAILURE);
}
return value;
Expand Down
10 changes: 10 additions & 0 deletions automerge-c/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,19 @@ add_custom_command(
DEPENDS
byte_span.rs
change_hashes.rs
change.rs
changes.rs
doc.rs
doc/list.rs
doc/map.rs
doc/utils.rs
obj.rs
result.rs
sync.rs
sync/have.rs
sync/haves.rs
sync/message.rs
sync/state.rs
${CMAKE_SOURCE_DIR}/build.rs
${CMAKE_SOURCE_DIR}/Cargo.toml
${CMAKE_SOURCE_DIR}/cbindgen.toml
Expand Down
27 changes: 14 additions & 13 deletions automerge-c/src/byte_span.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use automerge as am;

use crate::AMchange;

/// \struct AMbyteSpan
/// \brief A contiguous sequence of bytes.
///
#[repr(C)]
pub struct AMbyteSpan {
/// A pointer to an array of bytes.
/// \warning \p src is only valid until the `AMfreeResult()` function is called
/// on the `AMresult` struct hosting the array of bytes to which
/// it points.
/// \warning \p src is only valid until the `AMresultFree()` function is
/// called on the `AMresult` struct hosting the array of bytes to
/// which it points.
src: *const u8,
/// The number of bytes in the array.
count: usize,
Expand All @@ -25,10 +23,13 @@ impl Default for AMbyteSpan {
}
}

impl From<&AMchange> for AMbyteSpan {
fn from(change: &AMchange) -> Self {
let change_hash = &(change.as_ref()).hash;
change_hash.into()
impl From<&am::ActorId> for AMbyteSpan {
fn from(actor: &am::ActorId) -> Self {
let slice = actor.to_bytes();
Self {
src: slice.as_ptr(),
count: slice.len(),
}
}
}

Expand All @@ -51,11 +52,11 @@ impl From<&am::ChangeHash> for AMbyteSpan {
}
}

impl From<&Vec<u8>> for AMbyteSpan {
fn from(v: &Vec<u8>) -> Self {
impl From<&[u8]> for AMbyteSpan {
fn from(slice: &[u8]) -> Self {
Self {
src: (*v).as_ptr(),
count: (*v).len(),
src: slice.as_ptr(),
count: slice.len(),
}
}
}
Loading

0 comments on commit de25e8f

Please sign in to comment.