Skip to content

Commit e9dbbcb

Browse files
committed
ffi: adapt to protocol v1
1 parent 6cc960c commit e9dbbcb

File tree

11 files changed

+451
-144
lines changed

11 files changed

+451
-144
lines changed

negentropy-ffi/Cargo.lock

Lines changed: 331 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

negentropy-ffi/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ path = "uniffi-bindgen.rs"
2020
[dependencies]
2121
negentropy = { path = "../negentropy" }
2222
parking_lot = "0.12"
23-
uniffi = { version = "0.24", features = ["cli"] }
23+
uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "d380d164cfdba9e091c461baa6855f0a2294ac5b", features = ["cli"] }
2424

2525
[build-dependencies]
26-
uniffi = { version = "0.24", features = ["build"] }
26+
uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "d380d164cfdba9e091c461baa6855f0a2294ac5b", features = ["build"] }
2727

2828
[profile.release]
2929
lto = true

negentropy-ffi/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ python:
88
rm -rf bindings-python/dist
99
pip install -r bindings-python/requirements.txt --break-system-packages
1010
cargo build --release
11-
cargo run --features=uniffi/cli --bin uniffi-bindgen generate src/negentropy.udl --language python --no-format -o bindings-python/src/negentropy/
12-
cp ../target/release/libnegentropy_ffi.so bindings-python/src/negentropy/ | true
13-
cp ../target/release/libnegentropy_ffi.dylib bindings-python/src/negentropy/ | true
11+
cargo run --features=uniffi/cli --bin uniffi-bindgen generate --library ./target/release/libnegentropy_ffi.so --language python --no-format -o bindings-python/src/negentropy/
12+
cp ./target/release/libnegentropy_ffi.so bindings-python/src/negentropy/ | true
13+
cp ./target/release/libnegentropy_ffi.dylib bindings-python/src/negentropy/ | true
1414
cd bindings-python && python setup.py --verbose bdist_wheel
1515
pip install ./bindings-python/dist/negentropy*.whl --force-reinstall --break-system-packages

negentropy-ffi/bindings-python/examples/client.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
from negentropy import Negentropy, Bytes
1+
from negentropy import Negentropy, NegentropyStorageVector, Bytes
22

33
# Client init
4-
client = Negentropy(16, None)
5-
client.add_item(0, Bytes.from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
6-
client.add_item(1, Bytes.from_hex("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"))
7-
client.seal()
4+
storage = NegentropyStorageVector()
5+
storage.insert(0, Bytes.from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
6+
storage.insert(1, Bytes.from_hex("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"))
7+
storage.seal()
8+
client = Negentropy(storage, None)
89
init_output = client.initiate()
910
print(f"Initiator Output: {init_output.as_hex()}")
1011

1112
# Relay
12-
relay = Negentropy(16, None)
13-
relay.add_item(0, Bytes.from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
14-
relay.add_item(2, Bytes.from_hex("cccccccccccccccccccccccccccccccc"))
15-
relay.add_item(3, Bytes.from_hex("11111111111111111111111111111111"))
16-
relay.add_item(5, Bytes.from_hex("22222222222222222222222222222222"))
17-
relay.add_item(10, Bytes.from_hex("33333333333333333333333333333333"))
18-
relay.seal()
13+
storage = NegentropyStorageVector()
14+
storage.insert(0, Bytes.from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
15+
storage.insert(2, Bytes.from_hex("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"))
16+
storage.insert(3, Bytes.from_hex("1111111111111111111111111111111111111111111111111111111111111111"))
17+
storage.insert(5, Bytes.from_hex("2222222222222222222222222222222222222222222222222222222222222222"))
18+
storage.insert(10, Bytes.from_hex("3333333333333333333333333333333333333333333333333333333333333333"))
19+
storage.seal()
20+
relay = Negentropy(storage, None)
1921
reconcile_output = relay.reconcile(init_output)
2022
print(f"Reconcile Output: {reconcile_output.as_hex()}")
2123

negentropy-ffi/build.rs

Lines changed: 0 additions & 6 deletions
This file was deleted.

negentropy-ffi/src/bytes.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
// Distributed under the MIT software license
33

44
use core::ops::Deref;
5+
use std::sync::Arc;
6+
7+
use uniffi::Object;
58

69
use crate::error::Result;
710

11+
#[derive(Object)]
812
pub struct Bytes {
913
inner: negentropy::Bytes,
1014
}
@@ -22,17 +26,20 @@ impl From<negentropy::Bytes> for Bytes {
2226
}
2327
}
2428

29+
#[uniffi::export]
2530
impl Bytes {
26-
pub fn new(bytes: Vec<u8>) -> Self {
27-
Self {
31+
#[uniffi::constructor]
32+
pub fn new(bytes: Vec<u8>) -> Arc<Self> {
33+
Arc::new(Self {
2834
inner: negentropy::Bytes::new(bytes),
29-
}
35+
})
3036
}
3137

32-
pub fn from_hex(data: String) -> Result<Self> {
33-
Ok(Self {
38+
#[uniffi::constructor]
39+
pub fn from_hex(data: String) -> Result<Arc<Self>> {
40+
Ok(Arc::new(Self {
3441
inner: negentropy::Bytes::from_hex(data)?,
35-
})
42+
}))
3643
}
3744

3845
pub fn as_hex(&self) -> String {

negentropy-ffi/src/error.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33

44
use core::fmt;
55

6+
use uniffi::Error;
7+
68
pub type Result<T, E = NegentropyError> = std::result::Result<T, E>;
79

8-
#[derive(Debug)]
10+
#[derive(Debug, Error)]
911
pub enum NegentropyError {
1012
Generic { err: String },
1113
}
1214

15+
impl std::error::Error for NegentropyError {}
16+
1317
impl fmt::Display for NegentropyError {
1418
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1519
match self {

negentropy-ffi/src/lib.rs

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,61 @@ use std::ops::Deref;
55
use std::sync::Arc;
66

77
use parking_lot::RwLock;
8+
use uniffi::{Object, Record};
89

910
mod bytes;
1011
mod error;
12+
mod storage;
1113

1214
pub use self::bytes::Bytes;
1315
pub use self::error::NegentropyError;
1416
use self::error::Result;
17+
pub use self::storage::NegentropyStorageVector;
1518

19+
#[derive(Record)]
1620
pub struct ReconcileWithIds {
1721
pub have_ids: Vec<Arc<Bytes>>,
1822
pub need_ids: Vec<Arc<Bytes>>,
1923
pub output: Option<Arc<Bytes>>,
2024
}
2125

26+
#[derive(Object)]
2227
pub struct Negentropy {
23-
inner: RwLock<negentropy::Negentropy>,
28+
inner: RwLock<negentropy::Negentropy<negentropy::NegentropyStorageVector>>,
2429
}
2530

31+
#[uniffi::export]
2632
impl Negentropy {
27-
pub fn new(id_size: u8, frame_size_limit: Option<u64>) -> Result<Self> {
28-
Ok(Self {
33+
/// Create new negentropy instance
34+
///
35+
/// Frame size limit must be `equal to 0` or `greater than 4096`
36+
#[uniffi::constructor]
37+
pub fn new(
38+
storage: Arc<NegentropyStorageVector>,
39+
frame_size_limit: Option<u64>,
40+
) -> Result<Arc<Self>> {
41+
Ok(Arc::new(Self {
2942
inner: RwLock::new(negentropy::Negentropy::new(
30-
id_size as usize,
31-
frame_size_limit,
43+
storage.as_ref().to_inner(),
44+
frame_size_limit.unwrap_or_default(),
3245
)?),
33-
})
46+
}))
3447
}
3548

36-
pub fn id_size(&self) -> u64 {
37-
self.inner.read().id_size() as u64
49+
/// Initiate reconciliation set
50+
pub fn initiate(&self) -> Result<Arc<Bytes>> {
51+
let mut negentropy = self.inner.write();
52+
Ok(Arc::new(negentropy.initiate()?.into()))
3853
}
3954

40-
/// Check if current instance it's an initiator
4155
pub fn is_initiator(&self) -> bool {
4256
self.inner.read().is_initiator()
4357
}
4458

45-
/// Check if sealed
46-
pub fn is_sealed(&self) -> bool {
47-
self.inner.read().is_sealed()
48-
}
49-
50-
/// Check if need to continue
51-
pub fn continuation_needed(&self) -> bool {
52-
self.inner.read().continuation_needed()
53-
}
54-
55-
pub fn add_item(&self, created_at: u64, id: Arc<Bytes>) -> Result<()> {
59+
/// Set Initiator: for resuming initiation flow with a new instance
60+
pub fn set_initiator(&self) {
5661
let mut negentropy = self.inner.write();
57-
Ok(negentropy.add_item(created_at, id.as_ref().deref().clone())?)
58-
}
59-
60-
pub fn seal(&self) -> Result<()> {
61-
let mut negentropy = self.inner.write();
62-
Ok(negentropy.seal()?)
63-
}
64-
65-
/// Initiate reconciliation set
66-
pub fn initiate(&self) -> Result<Arc<Bytes>> {
67-
let mut negentropy = self.inner.write();
68-
Ok(Arc::new(negentropy.initiate()?.into()))
62+
negentropy.set_initiator();
6963
}
7064

7165
pub fn reconcile(&self, query: Arc<Bytes>) -> Result<Arc<Bytes>> {
@@ -89,5 +83,4 @@ impl Negentropy {
8983
}
9084
}
9185

92-
// UDL
93-
uniffi::include_scaffolding!("negentropy");
86+
uniffi::setup_scaffolding!("negentropy");

negentropy-ffi/src/negentropy.udl

Lines changed: 0 additions & 42 deletions
This file was deleted.

negentropy-ffi/src/storage.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2023 Yuki Kishimoto
2+
// Distributed under the MIT software license
3+
4+
use std::ops::Deref;
5+
use std::sync::Arc;
6+
7+
use negentropy::NegentropyStorageBase;
8+
use parking_lot::RwLock;
9+
use uniffi::Object;
10+
11+
use crate::error::Result;
12+
use crate::Bytes;
13+
14+
#[derive(Object)]
15+
pub struct NegentropyStorageVector {
16+
inner: Arc<RwLock<negentropy::NegentropyStorageVector>>,
17+
}
18+
19+
#[uniffi::export]
20+
impl NegentropyStorageVector {
21+
#[uniffi::constructor]
22+
pub fn new() -> Arc<Self> {
23+
Arc::new(Self {
24+
inner: Arc::new(RwLock::new(negentropy::NegentropyStorageVector::new())),
25+
})
26+
}
27+
28+
/// Insert item
29+
pub fn insert(&self, created_at: u64, id: Arc<Bytes>) -> Result<()> {
30+
let mut storage = self.inner.write();
31+
Ok(storage.insert(created_at, id.as_ref().deref().clone())?)
32+
}
33+
34+
/// Seal
35+
pub fn seal(&self) -> Result<()> {
36+
let mut storage = self.inner.write();
37+
Ok(storage.seal()?)
38+
}
39+
40+
/// Unseal
41+
pub fn unseal(&self) -> Result<()> {
42+
let mut storage = self.inner.write();
43+
Ok(storage.unseal()?)
44+
}
45+
46+
fn size(&self) -> Result<u64> {
47+
Ok(self.inner.read().size()? as u64)
48+
}
49+
}
50+
51+
impl NegentropyStorageVector {
52+
pub(crate) fn to_inner(&self) -> negentropy::NegentropyStorageVector {
53+
self.inner.read().to_owned()
54+
}
55+
}

0 commit comments

Comments
 (0)