Skip to content

Commit

Permalink
doc work etc
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb committed Aug 31, 2024
1 parent bafac2e commit d81b80f
Show file tree
Hide file tree
Showing 37 changed files with 2,147 additions and 898 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ implements `StateMachine` and has the following properties:
Here's an example `StateMachine` implementing a counter:

```rust
use aper::{StateMachine, NeverConflict};
use aper::{Aper, AperSync};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
#[derive(Serialize, Deserialize, Clone, Debug, Default, AperSync)]
struct Counter { value: i64 }

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
Expand All @@ -43,7 +43,7 @@ enum CounterTransition {
Decrement(i64),
}

impl StateMachine for Counter {
impl Aper for Counter {
type Transition = CounterTransition;
type Conflict = NeverConflict;

Expand Down
4 changes: 2 additions & 2 deletions aper-stateroom/src/state_program.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::IntentEvent;
use aper::{Aper, Attach, TreeMap, TreeMapRef};
use aper::{Aper, AperSync, TreeMap, TreeMapRef};
use serde::{de::DeserializeOwned, Serialize};

/// This trait can be added to a [StateMachine] which takes a [TransitionEvent] as
Expand Down Expand Up @@ -44,7 +44,7 @@ where
SM: Aper + Send + Sync + 'static,
<SM as Aper>::Intent: Send;

impl<SM> Attach for StateMachineContainerProgram<SM>
impl<SM> AperSync for StateMachineContainerProgram<SM>
where
SM: Aper + Send + Sync + 'static,
SM::Intent: Send,
Expand Down
22 changes: 11 additions & 11 deletions aper/aper-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use proc_macro2::TokenStream;
use quote::quote;
use std::collections::BTreeSet;

#[proc_macro_derive(Attach)]
#[proc_macro_derive(AperSync)]
pub fn attach_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let state = MacroState::from_tokens(input.into());
let result = state.generate_impl();
Expand Down Expand Up @@ -55,7 +55,7 @@ impl MacroState {
let field = syn::Ident::new(field, proc_macro2::Span::call_site());
let name = Literal::byte_string(field.to_string().as_bytes());
quote! {
#field: aper::Attach::attach(treemap.child(#name))
#field: aper::AperSync::attach(treemap.child(#name))
}
});
quote! {
Expand All @@ -68,7 +68,7 @@ impl MacroState {
let fields = (0..*fields).map(|i| {
let i = Literal::byte_string(i.to_be_bytes().as_slice());
quote! {
aper::Attach::attach(treemap.child(#i))
aper::AperSync::attach(treemap.child(#i))
}
});
quote! {
Expand All @@ -81,7 +81,7 @@ impl MacroState {
};

quote! {
impl aper::Attach for #name {
impl aper::AperSync for #name {
fn attach(treemap: aper::TreeMapRef) -> Self {
#fields
}
Expand All @@ -104,7 +104,7 @@ mod tests {
let result = state.generate_impl();

let expected = quote! {
impl aper::Attach for MyStruct {
impl aper::AperSync for MyStruct {
fn attach(treemap: aper::TreeMapRef) -> Self {
MyStruct
}
Expand All @@ -127,11 +127,11 @@ mod tests {
let result = state.generate_impl();

let expected = quote! {
impl aper::Attach for MyStruct {
impl aper::AperSync for MyStruct {
fn attach(treemap: aper::TreeMapRef) -> Self {
MyStruct {
field1: aper::Attach::attach(treemap.child(b"field1")),
field2: aper::Attach::attach(treemap.child(b"field2"))
field1: aper::AperSync::attach(treemap.child(b"field1")),
field2: aper::AperSync::attach(treemap.child(b"field2"))
}
}
}
Expand All @@ -150,11 +150,11 @@ mod tests {
let result = state.generate_impl();

let expected = quote! {
impl aper::Attach for MyStruct {
impl aper::AperSync for MyStruct {
fn attach(treemap: aper::TreeMapRef) -> Self {
MyStruct(
aper::Attach::attach(treemap.child(b"\0\0\0\0\0\0\0\0")),
aper::Attach::attach(treemap.child(b"\0\0\0\0\0\0\0\x01"))
aper::AperSync::attach(treemap.child(b"\0\0\0\0\0\0\0\0")),
aper::AperSync::attach(treemap.child(b"\0\0\0\0\0\0\0\x01"))
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions aper/src/aper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use std::{
fmt::Debug,
};

pub trait Attach {
pub trait AperSync {
fn attach(map: TreeMapRef) -> Self;

fn listen<F: Fn() -> bool + 'static + Send + Sync>(&self, listener: F) {
// Default implementation does nothing.
}
}

pub trait Aper: Attach {
pub trait Aper: AperSync {
type Intent: Clone + Serialize + for<'de> Deserialize<'de> + PartialEq;
type Error: Debug;

Expand Down
4 changes: 2 additions & 2 deletions aper/src/data_structures/atom.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{Attach, TreeMap, TreeMapRef};
use crate::{AperSync, TreeMap, TreeMapRef};
use serde::{de::DeserializeOwned, Serialize};

pub struct Atom<T: Serialize + DeserializeOwned + Default> {
map: TreeMapRef,
_phantom: std::marker::PhantomData<T>,
}

impl<T: Serialize + DeserializeOwned + Default> Attach for Atom<T> {
impl<T: Serialize + DeserializeOwned + Default> AperSync for Atom<T> {
fn attach(map: TreeMapRef) -> Self {
Self {
map,
Expand Down
39 changes: 39 additions & 0 deletions aper/src/data_structures/atom_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::{AperSync, TreeMapRef};
use serde::{de::DeserializeOwned, Serialize};

pub struct AtomMap<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> {
map: TreeMapRef,
_phantom: std::marker::PhantomData<(K, V)>,
}

impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> AperSync for AtomMap<K, V> {
fn attach(map: TreeMapRef) -> Self {
Self {
map,
_phantom: std::marker::PhantomData,
}
}

fn listen<F: Fn() -> bool + 'static + Send + Sync>(&self, listener: F) {
self.map.listen(listener)
}
}

impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> AtomMap<K, V> {
pub fn get(&self, key: &K) -> Option<V> {
self.map
.get(&bincode::serialize(key).unwrap())
.map(|bytes| bincode::deserialize(&bytes).unwrap())
}

pub fn set(&mut self, key: &K, value: &V) {
self.map.set(
bincode::serialize(key).unwrap(),
bincode::serialize(value).unwrap(),
);
}

pub fn delete(&mut self, key: &K) {
self.map.delete(bincode::serialize(key).unwrap());
}
}
4 changes: 2 additions & 2 deletions aper/src/data_structures/fixed_array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Attach, TreeMapRef};
use crate::{AperSync, TreeMapRef};
use serde::{de::DeserializeOwned, Serialize};

#[derive(Clone)]
Expand All @@ -7,7 +7,7 @@ pub struct FixedArray<const N: u32, T: Serialize + DeserializeOwned + Default> {
_phantom: std::marker::PhantomData<T>,
}

impl<const N: u32, T: Serialize + DeserializeOwned + Default> Attach for FixedArray<N, T> {
impl<const N: u32, T: Serialize + DeserializeOwned + Default> AperSync for FixedArray<N, T> {
fn attach(map: TreeMapRef) -> Self {
Self {
map,
Expand Down
25 changes: 9 additions & 16 deletions aper/src/data_structures/map.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{Attach, TreeMapRef};
use crate::{AperSync, TreeMapRef};
use serde::{de::DeserializeOwned, Serialize};

pub struct Map<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> {
pub struct Map<K: Serialize + DeserializeOwned, V: AperSync> {
map: TreeMapRef,
_phantom: std::marker::PhantomData<(K, V)>,
}

impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> Attach for Map<K, V> {
impl<K: Serialize + DeserializeOwned, V: AperSync> AperSync for Map<K, V> {
fn attach(map: TreeMapRef) -> Self {
Self {
map,
Expand All @@ -19,21 +19,14 @@ impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> Attach fo
}
}

impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> Map<K, V> {
impl<K: Serialize + DeserializeOwned, V: AperSync> Map<K, V> {
pub fn get(&self, key: &K) -> Option<V> {
self.map
.get(&bincode::serialize(key).unwrap())
.map(|bytes| bincode::deserialize(&bytes).unwrap())
let key = bincode::serialize(key).unwrap();
Some(V::attach(self.map.child(&key)))
}

pub fn set(&mut self, key: &K, value: &V) {
self.map.set(
bincode::serialize(key).unwrap(),
bincode::serialize(value).unwrap(),
);
}

pub fn delete(&mut self, key: &K) {
self.map.delete(bincode::serialize(key).unwrap());
pub fn get_or_create(&self, key: &K) -> V {
let key = bincode::serialize(key).unwrap();
V::attach(self.map.child(&key))
}
}
6 changes: 6 additions & 0 deletions aper/src/data_structures/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
pub mod atom;
pub mod atom_map;
pub mod fixed_array;
pub mod map;

pub use atom::Atom;
pub use atom_map::AtomMap;
pub use fixed_array::FixedArray;
pub use map::Map;
2 changes: 1 addition & 1 deletion aper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod listener;
mod treemap;

pub use aper::*;
pub use aper_derive::Attach;
pub use aper_derive::AperSync;
use serde::{Deserialize, Serialize};
pub use treemap::*;

Expand Down
4 changes: 2 additions & 2 deletions aper/tests/backtrack.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use aper::{data_structures::map::Map, Attach, TreeMap, TreeMapRef};
use aper::{data_structures::atom_map::AtomMap, AperSync, TreeMap, TreeMapRef};

#[test]
fn test_backtrack() {
let treemap = TreeMap::default();
let mut map = Map::<u8, u8>::attach(TreeMapRef::new_root(&treemap));
let mut map = AtomMap::<u8, u8>::attach(TreeMapRef::new_root(&treemap));

{
map.set(&1, &2);
Expand Down
6 changes: 3 additions & 3 deletions aper/tests/listener.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use aper::{
data_structures::{atom::Atom, fixed_array::FixedArray},
Aper, AperClient, Attach, Mutation,
Aper, AperClient, AperSync, Mutation,
};
use serde::{Deserialize, Serialize};
use std::sync::mpsc::channel;

#[derive(Attach)]
#[derive(AperSync)]
struct SimpleStruct {
atom_i32: Atom<i32>,
atom_string: Atom<String>,
Expand Down Expand Up @@ -107,7 +107,7 @@ fn test_mutate_listener_simple() {
assert!(fixed_array_recv.try_recv().is_err());
}

#[derive(Attach)]
#[derive(AperSync)]
struct LinkedFields {
lhs: Atom<i32>,
rhs: Atom<i32>,
Expand Down
4 changes: 2 additions & 2 deletions aper/tests/simple-client-server.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use aper::{data_structures::atom::Atom, Aper, AperClient, AperServer, Attach, TreeMapRef};
use aper::{data_structures::atom::Atom, Aper, AperClient, AperServer, AperSync, TreeMapRef};
use serde::{Deserialize, Serialize};

struct Counter(Atom<u64>);

impl Attach for Counter {
impl AperSync for Counter {
fn attach(map: TreeMapRef) -> Self {
Self(Atom::attach(map))
}
Expand Down
4 changes: 2 additions & 2 deletions aper/tests/tic-tac-toe.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use aper::{
data_structures::{atom::Atom, fixed_array::FixedArray},
Aper, Attach, TreeMap, TreeMapRef,
Aper, AperSync, TreeMap, TreeMapRef,
};
use serde::{Deserialize, Serialize};

#[derive(Attach)]
#[derive(AperSync)]
struct TicTacToe {
grid: FixedArray<9, Option<TicTacToePlayer>>,
player: Atom<TicTacToePlayer>,
Expand Down
2 changes: 2 additions & 0 deletions examples/counter-leptos/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
static-client
client-pkg
Loading

0 comments on commit d81b80f

Please sign in to comment.