Skip to content

Commit 1a17a8a

Browse files
authored
CASLike no longer requires that state is total ordered (#22)
1 parent a142968 commit 1a17a8a

3 files changed

Lines changed: 8 additions & 11 deletions

File tree

hydro_optimize_examples/src/compare_and_swap/cas.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct CAS<'a, 'b> {
1616

1717
impl<'a, 'b, State, Sender> CASLike<'a, State, Sender> for CAS<'a, 'b>
1818
where
19-
State: Clone + Serialize + for<'de> Deserialize<'de> + Ord + 'a,
19+
State: Clone + Serialize + for<'de> Deserialize<'de> + PartialOrd + 'a,
2020
Sender: Clone + Serialize + for<'de> Deserialize<'de> + Eq + Hash + 'a,
2121
{
2222
fn build(

hydro_optimize_examples/src/compare_and_swap/cas_distributed.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<'a, 'b> DistributedCAS<'a, 'b> {
247247
/// 2. Each client subscribes at most once. (Doesn't break correctness but will duplicate messages).
248248
impl<'a, 'b, State, Sender> CASLike<'a, State, Sender> for DistributedCAS<'a, 'b>
249249
where
250-
State: Clone + Debug + Serialize + for<'de> Deserialize<'de> + Ord + 'a,
250+
State: Clone + Debug + Serialize + for<'de> Deserialize<'de> + PartialOrd + Eq + 'a,
251251
Sender: Clone + Debug + Serialize + for<'de> Deserialize<'de> + Eq + Hash + 'a,
252252
{
253253
fn build(
@@ -380,7 +380,12 @@ where
380380
.clone()
381381
.map(q!(|(response, _all_agree)| (response.max_ballot, response.state)))
382382
.chain(committed_state.into_stream())
383-
.max();
383+
.reduce(q!(|(ballot, state), (new_ballot, new_state)| {
384+
if new_ballot >= *ballot {
385+
*ballot = new_ballot;
386+
*state = new_state;
387+
}
388+
}, commutative = manual_proof!(/** Max is commutative. If the new ballot is the same, then state can be overwritten based on order and that's ok. */)));
384389
// Propose a valid client write if the election is succesful and all agree
385390
let proposed_client_write = write_queue
386391
.clone()

hydro_optimize_examples/src/compare_and_swap/cas_like.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ impl<S: PartialOrd> PartialOrd for CASState<S> {
4343
}
4444
}
4545

46-
impl<S: Ord> Ord for CASState<S> {
47-
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
48-
self.version
49-
.cmp(&other.version)
50-
.then_with(|| self.state.cmp(&other.state))
51-
}
52-
}
53-
5446
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
5547
pub struct UniqueRequestId {
5648
pub id: u64,

0 commit comments

Comments
 (0)