Skip to content

Commit f356917

Browse files
committed
Convert outbound message type to associated type
1 parent fe14683 commit f356917

8 files changed

Lines changed: 36 additions & 26 deletions

File tree

crates/hotfix/src/application.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
use crate::message::OutboundMessage;
12
use hotfix_message::message::Message;
23

34
#[async_trait::async_trait]
45
/// The application users of HotFIX can implement to hook into the engine.
5-
pub trait Application<Outbound>: Send + Sync + 'static {
6+
pub trait Application: Send + Sync + 'static {
7+
type Outbound: OutboundMessage;
8+
69
/// Called when a message is sent to the engine to be sent to the counterparty.
710
///
811
/// This is invoked before the raw message is persisted in the message store.
9-
async fn on_outbound_message(&self, msg: &Outbound) -> OutboundDecision;
12+
async fn on_outbound_message(&self, msg: &Self::Outbound) -> OutboundDecision;
1013
/// Called when a message is received from the counterparty.
1114
///
1215
/// This is invoked after the message is verified by the session layer.

crates/hotfix/src/initiator.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct Initiator<Outbound> {
2929
impl<Outbound: OutboundMessage> Initiator<Outbound> {
3030
pub async fn start(
3131
config: SessionConfig,
32-
application: impl Application<Outbound>,
32+
application: impl Application<Outbound = Outbound>,
3333
store: impl MessageStore + 'static,
3434
) -> Result<Self, SessionCreationError> {
3535
let session_ref = InternalSessionRef::new(config.clone(), application, store)?;
@@ -184,7 +184,9 @@ mod tests {
184184
struct NoOpApp;
185185

186186
#[async_trait::async_trait]
187-
impl Application<DummyMessage> for NoOpApp {
187+
impl Application for NoOpApp {
188+
type Outbound = DummyMessage;
189+
188190
async fn on_outbound_message(&self, _msg: &DummyMessage) -> OutboundDecision {
189191
OutboundDecision::Send
190192
}

crates/hotfix/src/session.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use hotfix_message::session_fields::{
5757

5858
const SCHEDULE_CHECK_INTERVAL: u64 = 1;
5959

60-
struct Session<A, O, S> {
60+
struct Session<A, S> {
6161
message_config: MessageConfig,
6262
config: SessionConfig,
6363
schedule: SessionSchedule,
@@ -67,20 +67,18 @@ struct Session<A, O, S> {
6767
store: S,
6868
schedule_check_timer: Pin<Box<Sleep>>,
6969
reset_on_next_logon: bool,
70-
_phantom: std::marker::PhantomData<fn() -> O>,
7170
}
7271

73-
impl<App, Outbound, Store> Session<App, Outbound, Store>
72+
impl<App, Store> Session<App, Store>
7473
where
75-
App: Application<Outbound>,
76-
Outbound: OutboundMessage,
74+
App: Application,
7775
Store: MessageStore,
7876
{
7977
fn new(
8078
config: SessionConfig,
8179
application: App,
8280
store: Store,
83-
) -> Result<Session<App, Outbound, Store>, SessionCreationError> {
81+
) -> Result<Session<App, Store>, SessionCreationError> {
8482
let schedule_check_timer = sleep(Duration::from_secs(SCHEDULE_CHECK_INTERVAL));
8583

8684
let dictionary = Self::get_data_dictionary(&config)?;
@@ -98,7 +96,6 @@ where
9896
store,
9997
schedule_check_timer: Box::pin(schedule_check_timer),
10098
reset_on_next_logon: false,
101-
_phantom: std::marker::PhantomData,
10299
};
103100

104101
Ok(session)
@@ -794,7 +791,7 @@ where
794791
.reset_peer_timer(self.config.heartbeat_interval, test_request_id);
795792
}
796793

797-
async fn send_app_message(&mut self, message: Outbound) -> Result<SendOutcome, SendError> {
794+
async fn send_app_message(&mut self, message: App::Outbound) -> Result<SendOutcome, SendError> {
798795
if !self.state.is_connected() {
799796
return Err(SendError::Disconnected);
800797
}
@@ -981,7 +978,7 @@ where
981978
}
982979
}
983980

984-
async fn handle_outbound_message(&mut self, request: OutboundRequest<Outbound>) {
981+
async fn handle_outbound_message(&mut self, request: OutboundRequest<App::Outbound>) {
985982
let OutboundRequest { message, confirm } = request;
986983
let result = self.send_app_message(message).await;
987984
match confirm {
@@ -1129,14 +1126,13 @@ fn get_msg_seq_num(message: &Message) -> u64 {
11291126
.expect("MsgSeqNum missing from validated message - parser bug")
11301127
}
11311128

1132-
async fn run_session<App, Outbound, Store>(
1133-
mut session: Session<App, Outbound, Store>,
1129+
async fn run_session<App, Store>(
1130+
mut session: Session<App, Store>,
11341131
mut event_receiver: mpsc::Receiver<SessionEvent>,
1135-
mut outbound_message_receiver: mpsc::Receiver<OutboundRequest<Outbound>>,
1132+
mut outbound_message_receiver: mpsc::Receiver<OutboundRequest<App::Outbound>>,
11361133
mut admin_request_receiver: mpsc::Receiver<AdminRequest>,
11371134
) where
1138-
App: Application<Outbound>,
1139-
Outbound: OutboundMessage,
1135+
App: Application,
11401136
Store: MessageStore + Send + 'static,
11411137
{
11421138
loop {
@@ -1294,7 +1290,9 @@ mod tests {
12941290
struct NoOpApp;
12951291

12961292
#[async_trait::async_trait]
1297-
impl Application<DummyMessage> for NoOpApp {
1293+
impl Application for NoOpApp {
1294+
type Outbound = DummyMessage;
1295+
12981296
async fn on_outbound_message(&self, _: &DummyMessage) -> OutboundDecision {
12991297
OutboundDecision::Send
13001298
}
@@ -1332,7 +1330,7 @@ mod tests {
13321330
schedule: SessionSchedule,
13331331
state: SessionState,
13341332
store: TestStore,
1335-
) -> Session<NoOpApp, DummyMessage, TestStore> {
1333+
) -> Session<NoOpApp, TestStore> {
13361334
let config = create_test_config();
13371335
let message_config = MessageConfig::default();
13381336
let dictionary = Dictionary::fix44();
@@ -1348,7 +1346,6 @@ mod tests {
13481346
store,
13491347
schedule_check_timer: Box::pin(sleep(Duration::from_secs(1))),
13501348
reset_on_next_logon: false,
1351-
_phantom: std::marker::PhantomData,
13521349
}
13531350
}
13541351

crates/hotfix/src/session/session_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct InternalSessionRef<Outbound> {
2828
impl<Outbound: OutboundMessage> InternalSessionRef<Outbound> {
2929
pub fn new(
3030
config: SessionConfig,
31-
application: impl Application<Outbound>,
31+
application: impl Application<Outbound = Outbound>,
3232
store: impl MessageStore + 'static,
3333
) -> Result<Self, SessionCreationError> {
3434
let (event_sender, event_receiver) = mpsc::channel::<SessionEvent>(100);

crates/hotfix/tests/connection_test_cases/helpers.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ impl OutboundMessage for MinimalMessage {
341341
pub struct MinimalApplication;
342342

343343
#[async_trait::async_trait]
344-
impl Application<MinimalMessage> for MinimalApplication {
344+
impl Application for MinimalApplication {
345+
type Outbound = MinimalMessage;
346+
345347
async fn on_outbound_message(&self, _msg: &MinimalMessage) -> OutboundDecision {
346348
OutboundDecision::Send
347349
}

crates/hotfix/tests/session_test_cases/common/fakes/fake_application.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ impl FakeApplication {
2828
}
2929

3030
#[async_trait::async_trait]
31-
impl Application<TestMessage> for FakeApplication {
31+
impl Application for FakeApplication {
32+
type Outbound = TestMessage;
33+
3234
async fn on_outbound_message(&self, _msg: &TestMessage) -> OutboundDecision {
3335
self.outbound_decision
3436
}

examples/load-testing/src/application.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ impl LoadTestingApplication {
1616
}
1717

1818
#[async_trait::async_trait]
19-
impl Application<OutboundMsg> for LoadTestingApplication {
19+
impl Application for LoadTestingApplication {
20+
type Outbound = OutboundMsg;
21+
2022
async fn on_outbound_message(&self, _msg: &OutboundMsg) -> OutboundDecision {
2123
OutboundDecision::Send
2224
}

examples/simple-new-order/src/application.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use tracing::info;
88
pub struct TestApplication {}
99

1010
#[async_trait::async_trait]
11-
impl Application<OutboundMsg> for TestApplication {
11+
impl Application for TestApplication {
12+
type Outbound = OutboundMsg;
13+
1214
async fn on_outbound_message(&self, _msg: &OutboundMsg) -> OutboundDecision {
1315
OutboundDecision::Send
1416
}

0 commit comments

Comments
 (0)