Skip to content

Commit

Permalink
renamed Return & Retain
Browse files Browse the repository at this point in the history
  • Loading branch information
mkatychev committed Sep 3, 2024
1 parent b834c68 commit efb8b50
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 48 deletions.
10 changes: 5 additions & 5 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
ingress::{BoxedStateRouter, Ingress, IngressAdapter, PacketRouter},
manager::{BoxedStateMachine, EmptyContext},
notification::NotificationQueue,
timeout::{self, Retain, Return, TimeoutManager},
timeout::{self, Timeout, TimeoutManager, TimeoutMessage},
NotificationManager, NotificationProcessor, Rex, RexMessage, SignalQueue, StateMachine,
StateMachineManager,
};
Expand Down Expand Up @@ -46,8 +46,8 @@ impl<K: Rex> RexBuilder<K, (), ()> {

impl<K, In, Out> RexBuilder<K, In, Out>
where
K: Rex + Return,
K::Message: Retain<K>,
K: Rex + Timeout,
K::Message: TimeoutMessage<K>,
In: Send + Sync + std::fmt::Debug,
Out: Send + Sync + std::fmt::Debug,
TimeoutManager<K>: NotificationProcessor<K::Message>,
Expand Down Expand Up @@ -159,9 +159,9 @@ where

impl<K> RexBuilder<K, K::In, K::Out>
where
K: Rex + Return + Ingress,
K: Rex + Timeout + Ingress,

K::Message: Retain<K> + TryInto<K::Out, Error = Report<ConversionError>>,
K::Message: TimeoutMessage<K> + TryInto<K::Out, Error = Report<ConversionError>>,
K::Input: TryFrom<K::In, Error = Report<ConversionError>>,
TimeoutManager<K>: NotificationProcessor<K::Message>,
{
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub use notification::{
GetTopic, Notification, NotificationManager, NotificationProcessor, NotificationQueue,
Operation, Request, RequestInner, RexMessage, RexTopic, Subscriber, UnaryRequest,
};
pub use timeout::Timeout;

/// A trait for types representing state machine lifecycles. These types should be field-less
/// enumerations or enumerations whose variants only contain field-less enumerations; note that
Expand Down
47 changes: 20 additions & 27 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use crate::{
notification::{Notification, NotificationQueue},
queue::StreamableDeque,
storage::{StateStore, Tree},
timeout::{Retain, RetainItem, TimeoutInput},
timeout::{RetainItem, TimeoutInput, TimeoutMessage},
Kind, Rex, State, StateId,
};

Expand Down Expand Up @@ -126,20 +126,14 @@ where

/// Store the injectable dependencies provided by the [`StateMachineManager`]
/// to a given state machine processor.
pub struct SmContext<K>
where
K: Rex,
{
pub struct SmContext<K: Rex> {
pub signal_queue: SignalQueue<K>,
pub notification_queue: NotificationQueue<K::Message>,
pub state_store: Arc<StateStore<StateId<K>, K::State>>,
pub id: StateId<K>,
}

impl<K> SmContext<K>
where
K: Rex,
{
impl<K: Rex> SmContext<K> {
pub fn notify(&self, notification: Notification<K::Message>) {
self.notification_queue.send(notification);
}
Expand All @@ -150,17 +144,14 @@ where
guard.get_state(self.id).copied()
}

fn get_tree(&self) -> Option<Tree<K>> {
pub fn get_tree(&self) -> Option<Tree<K>> {
self.state_store.get_tree(self.id)
}
pub fn has_state(&self) -> bool {
self.state_store.get_tree(self.id).is_some()
}
}
impl<K> Clone for SmContext<K>
where
K: Rex,
{
impl<K: Rex> Clone for SmContext<K> {
fn clone(&self) -> Self {
Self {
signal_queue: self.signal_queue.clone(),
Expand All @@ -171,12 +162,17 @@ where
}
}

impl<K: Rex> std::ops::Deref for SmContext<K> {
type Target = StateId<K>;

fn deref(&self) -> &Self::Target {
&self.id
}
}

/// Manages the [`Signal`] scope of various [`State`]s and [`StateMachine`]s bounded by
/// a [`Kind`] enumerable
pub struct StateMachineManager<K>
where
K: Rex,
{
pub struct StateMachineManager<K: Rex> {
signal_queue: SignalQueue<K>,
notification_queue: NotificationQueue<K::Message>,
state_machines: Arc<HashMap<K, BoxedStateMachine<K>>>,
Expand All @@ -199,10 +195,7 @@ impl<K: Rex> EmptyContext<K> {
}
}

impl<K> StateMachineManager<K>
where
K: Rex,
{
impl<K: Rex> StateMachineManager<K> {
#[must_use]
pub fn ctx_builder(&self) -> EmptyContext<K> {
EmptyContext {
Expand Down Expand Up @@ -299,7 +292,7 @@ where
pub trait StateMachineExt<K>: StateMachine<K>
where
K: Rex,
K::Message: Retain<K>,
K::Message: TimeoutMessage<K>,
{
/// NOTE [`StateMachineExt::new`] is created without a hierarchy
fn create_tree(&self, ctx: &SmContext<K>) {
Expand Down Expand Up @@ -379,7 +372,7 @@ impl<K, T> StateMachineExt<K> for T
where
T: StateMachine<K>,
K: Rex,
K::Message: Retain<K>,
K::Message: TimeoutMessage<K>,
{
}

Expand All @@ -398,7 +391,7 @@ mod tests {
notification::GetTopic,
storage::StateStore,
test_support::Hold,
timeout::{Retain, Return, TimeoutTopic, TEST_TICK_RATE, TEST_TIMEOUT},
timeout::{Timeout, TimeoutMessage, TimeoutTopic, TEST_TICK_RATE, TEST_TIMEOUT},
Rex, RexBuilder, RexMessage,
};

Expand Down Expand Up @@ -427,7 +420,7 @@ mod tests {
}
}

impl Retain<Game> for GameMsg {
impl TimeoutMessage<Game> for GameMsg {
type Item = Hold<Packet>;
}

Expand Down Expand Up @@ -553,7 +546,7 @@ mod tests {
}
}

impl Return for Game {
impl Timeout for Game {
fn return_item(&self, item: RetainItem<Self>) -> Option<Self::Input> {
match self {
Game::Ping => Some(Input::Ping(item.into())),
Expand Down
6 changes: 3 additions & 3 deletions src/test_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{Kind, Rex, State};
use crate::{
ingress::{Ingress, StateRouter},
notification::{GetTopic, RexMessage},
timeout::{NoRetain, Retain, Return, TimeoutInput},
timeout::{NoRetain, Timeout, TimeoutInput, TimeoutMessage},
RexError, StateId,
};

Expand Down Expand Up @@ -91,7 +91,7 @@ impl RexMessage for TestMsg {

#[derive(Copy, Clone, Debug, derive_more::Display)]
pub struct Hold<T>(pub(crate) T);
impl Retain<TestKind> for TestMsg {
impl TimeoutMessage<TestKind> for TestMsg {
type Item = NoRetain;
}

Expand Down Expand Up @@ -151,7 +151,7 @@ impl TryFrom<InPacket> for TestInput {
Ok(Self::Packet(packet))
}
}
impl Return for TestKind {}
impl Timeout for TestKind {}

#[derive(Clone, Debug, PartialEq)]
pub enum TestInput {
Expand Down
26 changes: 13 additions & 13 deletions src/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn hms_string(duration: Duration) -> String {
struct TimeoutLedger<K>
where
K: Kind + Rex,
K::Message: Retain<K>,
K::Message: TimeoutMessage<K>,
{
timers: BTreeMap<Instant, HashSet<StateId<K>>>,
ids: HashMap<StateId<K>, Instant>,
Expand All @@ -73,7 +73,7 @@ type RetainPair<K> = (StateId<K>, RetainItem<K>);
impl<K> TimeoutLedger<K>
where
K: Rex + HashKind + Copy,
K::Message: Retain<K>,
K::Message: TimeoutMessage<K>,
{
fn new() -> Self {
Self {
Expand Down Expand Up @@ -147,7 +147,7 @@ where
}
}

pub trait Retain<K: Rex>:
pub trait TimeoutMessage<K: Rex>:
std::fmt::Debug
+ RexMessage
+ From<UnaryRequest<K, Operation<Self::Item>>>
Expand All @@ -156,9 +156,9 @@ pub trait Retain<K: Rex>:
type Item: Copy + Send + std::fmt::Debug;
}

pub trait Return: Rex
pub trait Timeout: Rex
where
Self::Message: Retain<Self>,
Self::Message: TimeoutMessage<Self>,
{
fn return_item(&self, _item: RetainItem<Self>) -> Option<Self::Input> {
None
Expand Down Expand Up @@ -199,13 +199,13 @@ impl<T> Operation<T> {
}

pub type TimeoutInput<K> = UnaryRequest<K, TimeoutOp<K>>;
pub type TimeoutOp<K> = Operation<<<K as Rex>::Message as Retain<K>>::Item>;
pub type RetainItem<K> = <<K as Rex>::Message as Retain<K>>::Item;
pub type TimeoutOp<K> = Operation<<<K as Rex>::Message as TimeoutMessage<K>>::Item>;
pub type RetainItem<K> = <<K as Rex>::Message as TimeoutMessage<K>>::Item;

impl<K> UnaryRequest<K, TimeoutOp<K>>
where
K: Rex,
K::Message: Retain<K>,
K::Message: TimeoutMessage<K>,
{
#[cfg(test)]
pub(crate) fn set_timeout_millis(id: StateId<K>, millis: u64) -> Self {
Expand Down Expand Up @@ -251,7 +251,7 @@ where
pub struct TimeoutManager<K>
where
K: Rex,
K::Message: Retain<K>,
K::Message: TimeoutMessage<K>,
{
// the interval at which the TimeoutLedger checks for timeouts
tick_rate: Duration,
Expand All @@ -263,8 +263,8 @@ where

impl<K> TimeoutManager<K>
where
K: Rex + Return,
K::Message: Retain<K>,
K: Rex + Timeout,
K::Message: TimeoutMessage<K>,
{
#[must_use]
pub fn new(
Expand Down Expand Up @@ -385,8 +385,8 @@ where

impl<K> NotificationProcessor<K::Message> for TimeoutManager<K>
where
K: Rex + Return,
K::Message: Retain<K>,
K: Rex + Timeout,
K::Message: TimeoutMessage<K>,
{
fn init(&mut self, join_set: &mut JoinSet<()>) -> UnboundedSender<Notification<K::Message>> {
self.init_inner_with_handle(join_set)
Expand Down

0 comments on commit efb8b50

Please sign in to comment.