Skip to content

Commit

Permalink
name cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb committed Sep 1, 2024
1 parent b1f7d7c commit 6accccd
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 42 deletions.
9 changes: 4 additions & 5 deletions aper-stateroom/src/state_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ where
SM: Aper + Send + Sync + 'static,
SM::Intent: Send,
{
fn attach(treemap: StoreHandle) -> Self {
StateMachineContainerProgram(SM::attach(treemap))
fn attach(store: StoreHandle) -> Self {
StateMachineContainerProgram(SM::attach(store))
}
}

Expand All @@ -76,8 +76,7 @@ where
type T = SM::Intent;

fn new() -> Self {
let treemap = Store::default();
let treemap_ref = StoreHandle::new_root(&treemap);
StateMachineContainerProgram(SM::attach(treemap_ref))
let store = Store::default();
StateMachineContainerProgram(SM::attach(store.handle()))
}
}
20 changes: 10 additions & 10 deletions aper/aper-derive/src/lib.rs
Original file line number Diff line number Diff line change
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::AperSync::attach(treemap.child(#name))
#field: aper::AperSync::attach(store.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::AperSync::attach(treemap.child(#i))
aper::AperSync::attach(store.child(#i))
}
});
quote! {
Expand All @@ -82,7 +82,7 @@ impl MacroState {

quote! {
impl aper::AperSync for #name {
fn attach(treemap: aper::StoreHandle) -> Self {
fn attach(store: aper::StoreHandle) -> Self {
#fields
}
}
Expand All @@ -105,7 +105,7 @@ mod tests {

let expected = quote! {
impl aper::AperSync for MyStruct {
fn attach(treemap: aper::StoreHandle) -> Self {
fn attach(store: aper::StoreHandle) -> Self {
MyStruct
}
}
Expand All @@ -128,10 +128,10 @@ mod tests {

let expected = quote! {
impl aper::AperSync for MyStruct {
fn attach(treemap: aper::StoreHandle) -> Self {
fn attach(store: aper::StoreHandle) -> Self {
MyStruct {
field1: aper::AperSync::attach(treemap.child(b"field1")),
field2: aper::AperSync::attach(treemap.child(b"field2"))
field1: aper::AperSync::attach(store.child(b"field1")),
field2: aper::AperSync::attach(store.child(b"field2"))
}
}
}
Expand All @@ -151,10 +151,10 @@ mod tests {

let expected = quote! {
impl aper::AperSync for MyStruct {
fn attach(treemap: aper::StoreHandle) -> Self {
fn attach(store: aper::StoreHandle) -> Self {
MyStruct(
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"))
aper::AperSync::attach(store.child(b"\0\0\0\0\0\0\0\0")),
aper::AperSync::attach(store.child(b"\0\0\0\0\0\0\0\x01"))
)
}
}
Expand Down
12 changes: 5 additions & 7 deletions aper/src/aper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<A: Aper> AperClient<A> {
}

pub fn state(&self) -> A {
A::attach(StoreHandle::new_root(&self.map))
A::attach(self.map.handle())
}

pub fn verified_client_version(&self) -> u64 {
Expand All @@ -96,13 +96,11 @@ impl<A: Aper> AperClient<A> {
self.map.push_overlay();

{
let mut sm = A::attach(StoreHandle::new_root(&self.map));
let mut sm = A::attach(self.map.handle());

if let Err(e) = sm.apply(intent) {
// reverse changes.
tracing::info!("here3");
self.map.pop_overlay();
tracing::info!("here4");
return Err(e);
}
}
Expand Down Expand Up @@ -161,7 +159,7 @@ impl<A: Aper> AperClient<A> {
for speculative_intent in self.intent_stack.iter() {
// push a working overlay
self.map.push_overlay();
let mut sm = A::attach(StoreHandle::new_root(&self.map));
let mut sm = A::attach(self.map.handle());

if sm.apply(&speculative_intent.intent).is_err() {
// reverse changes.
Expand Down Expand Up @@ -211,7 +209,7 @@ impl<A: Aper> AperServer<A> {
pub fn apply(&mut self, intent: &A::Intent) -> Result<Vec<Mutation>, A::Error> {
self.map.push_overlay();

let mut sm = A::attach(StoreHandle::new_root(&self.map));
let mut sm = A::attach(self.map.handle());

if let Err(e) = sm.apply(intent) {
// reverse changes.
Expand All @@ -228,6 +226,6 @@ impl<A: Aper> AperServer<A> {
}

pub fn state(&self) -> A {
A::attach(StoreHandle::new_root(&self.map))
A::attach(self.map.handle())
}
}
8 changes: 1 addition & 7 deletions aper/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl Store {
}
}

#[derive(Clone, Default)]
#[derive(Clone)]
pub struct StoreHandle {
map: Store,
prefix: Vec<Bytes>,
Expand All @@ -177,12 +177,6 @@ impl StoreHandle {
inner.listeners.listen(self.prefix.clone(), listener);
}

pub fn new_root(map: &Store) -> Self {
let prefix = vec![];
let map = map.clone();
Self { map, prefix }
}

pub fn get(&self, key: &Bytes) -> Option<Bytes> {
self.map.get(&self.prefix, key)
}
Expand Down
10 changes: 5 additions & 5 deletions aper/tests/backtrack.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use aper::{data_structures::atom_map::AtomMap, AperSync, Store, StoreHandle};
use aper::{data_structures::atom_map::AtomMap, AperSync, Store};

#[test]
fn test_backtrack() {
let treemap = Store::default();
let mut map = AtomMap::<u8, u8>::attach(StoreHandle::new_root(&treemap));
let store = Store::default();
let mut map = AtomMap::<u8, u8>::attach(store.handle());

{
map.set(&1, &2);
Expand All @@ -16,7 +16,7 @@ fn test_backtrack() {
// add an overlay to the map

{
treemap.push_overlay();
store.push_overlay();

// existing values are still there

Expand Down Expand Up @@ -51,7 +51,7 @@ fn test_backtrack() {
// when we pop the overlay, the original values are restored

{
treemap.pop_overlay();
store.pop_overlay();

assert_eq!(map.get(&1), Some(2));
assert_eq!(map.get(&3), Some(4));
Expand Down
2 changes: 2 additions & 0 deletions aper/tests/store-cleanup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
use aper::{data_structures::atom::Atom, Aper, AperClient, AperServer, AperSync, StoreHandle};
use serde::{Deserialize, Serialize};
4 changes: 2 additions & 2 deletions aper/tests/tic-tac-toe.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use aper::{
data_structures::{atom::Atom, fixed_array::FixedArray},
Aper, AperSync, Store, StoreHandle,
Aper, AperSync, Store,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -87,7 +87,7 @@ impl Aper for TicTacToe {
#[test]
fn test_tic_tac_toe() {
let map = Store::default();
let mut game = TicTacToe::attach(StoreHandle::new_root(&map));
let mut game = TicTacToe::attach(map.handle());

game.apply(&TicTacToePlay::Play(0)).unwrap(); // X
game.apply(&TicTacToePlay::Play(1)).unwrap(); // O
Expand Down
4 changes: 2 additions & 2 deletions examples/drop-four/common/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ impl StateProgram for DropFourGame {
type T = GameTransition;

fn new() -> Self {
let treemapref = StoreHandle::default();
Self::attach(treemapref)
let storeref = StoreHandle::default();
Self::attach(storeref)
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/timer/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ impl StateProgram for Timer {
type T = TimerIntent;

fn new() -> Self {
let treemap = StoreHandle::default();
Timer::attach(treemap)
let store = StoreHandle::default();
Timer::attach(store)
}

fn suspended_event(&self) -> Option<IntentEvent<Self::T>> {
Expand Down
4 changes: 2 additions & 2 deletions site/book/src/01-introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ use aper::{AperSync, StoreHandle};
# }

fn main() {
let treemap = StoreHandle::default();
let todos = ToDoList::attach(treemap);
let store = StoreHandle::default();
let todos = ToDoList::attach(store);

let mut todo1 = todos.items.get_or_create(&"todo1".to_string());
todo1.name.set("Do laundry".to_string());
Expand Down

0 comments on commit 6accccd

Please sign in to comment.