Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
sgdxbc committed Apr 8, 2024
1 parent 292845a commit 94c19f0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
20 changes: 9 additions & 11 deletions src/event/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ use derive_where::derive_where;

use super::TimerId;

#[derive(derive_more::Deref, derive_more::DerefMut)]
#[derive_where(Debug, Clone, PartialEq, Eq, Hash; T, D)]
#[derive(derive_more::Deref, derive_more::DerefMut, Debug, Clone, PartialEq, Eq, Hash)]
#[derive_where(Default; T)]
pub struct Timer<T, F, D> {
pub struct Timer<T, D> {
#[deref]
#[deref_mut]
inner: T,
attach: BTreeMap<u32, D>,
#[derive_where(skip)]
_m: std::marker::PhantomData<F>,
}

pub trait TryIntoTimerData<D> {
fn try_into<M: 'static>(event: M) -> anyhow::Result<D>;
pub trait DowncastEvent {
fn try_from<M: 'static>(event: M) -> anyhow::Result<Self>
where
Self: Sized;
}

impl<T: super::Timer, F: TryIntoTimerData<D>, D, S> super::erased::RichTimer<S> for Timer<T, F, D> {
impl<T: super::Timer, D: DowncastEvent, S> super::erased::RichTimer<S> for Timer<T, D> {
fn set<M: Clone + Send + Sync + 'static>(
&mut self,
period: std::time::Duration,
Expand All @@ -29,7 +28,7 @@ impl<T: super::Timer, F: TryIntoTimerData<D>, D, S> super::erased::RichTimer<S>
where
S: super::erased::OnEventRichTimer<M>,
{
let data = F::try_into(event)?;
let data = D::try_from(event)?;
let TimerId(timer_id) = self.inner.set(period)?;
self.attach.insert(timer_id, data);
Ok(TimerId(timer_id))
Expand All @@ -41,12 +40,11 @@ impl<T: super::Timer, F: TryIntoTimerData<D>, D, S> super::erased::RichTimer<S>
}
}

impl<T, F, D> Timer<T, F, D> {
impl<T, D> Timer<T, D> {
pub fn new(inner: T) -> Self {
Self {
inner,
attach: Default::default(),
_m: Default::default(),
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/pbft/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
Verifiable,
},
event::{
any::{self, TryIntoTimerData},
any::{self, DowncastEvent},
erased::{events::Init, OnEvent as _, OnEventRichTimer as _},
linear, SendEvent, TimerId, Transient, UnreachableTimer,
},
Expand Down Expand Up @@ -88,12 +88,10 @@ enum TimerData {
// replica timers
}

struct IntoTimerData;

impl TryIntoTimerData<TimerData> for IntoTimerData {
fn try_into<M: 'static>(event: M) -> anyhow::Result<TimerData> {
impl DowncastEvent for TimerData {
fn try_from<M: 'static>(event: M) -> anyhow::Result<Self> {
if (&event as &dyn Any).is::<Resend>() {
Ok(TimerData::Resend)
Ok(Self::Resend)
} else {
Err(anyhow::anyhow!("unknown timer data"))
}
Expand Down Expand Up @@ -125,7 +123,7 @@ struct State<W: Workload, const CHECK: bool = false> {
pub clients: Vec<ClientState<W>>,
pub replicas: Vec<Replica>,
message_events: BTreeSet<MessageEvent>,
timers: BTreeMap<Addr, any::Timer<linear::Timer, IntoTimerData, TimerData>>,
timers: BTreeMap<Addr, any::Timer<linear::Timer, TimerData>>,
}

#[derive_where(Debug, Clone; W, W::Attach)]
Expand Down

0 comments on commit 94c19f0

Please sign in to comment.