Skip to content

Commit 6258aa5

Browse files
committed
Also move process into the trait
1 parent acc1a15 commit 6258aa5

2 files changed

Lines changed: 10 additions & 15 deletions

File tree

examples/repeat.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ impl ParticipantTrait for Repeat {
1616
.build()
1717
}
1818

19+
fn process(&self, input: Vec<u8>) -> Result<Vec<u8>, Vec<u8>> {
20+
println!("repeat process():");
21+
return Ok(input);
22+
}
23+
1924
}
2025

2126
unsafe impl Sync for Repeat {} // FIXME: figure out how to avoid
2227

23-
fn process(r: &ParticipantTrait, input: Vec<u8>) -> Result<Vec<u8>, Vec<u8>> {
24-
println!("repeat process():");
25-
return Ok(input);
26-
}
27-
2828
fn main() {
2929
static r: Repeat = Repeat { state: None };
30-
msgflo::participant::main(&r, process);
30+
msgflo::participant::main(&r);
3131
}

src/participant.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,11 @@ impl InfoBuilder {
6666

6767
pub trait ParticipantTrait : Sync {
6868
fn info(&self) -> Info;
69-
// fn process(&self, Vec<u8>) -> Result<Vec<u8>, Vec<u8>>;
69+
fn process(&self, Vec<u8>) -> Result<Vec<u8>, Vec<u8>>;
7070
}
71-
pub type ProcessFunction = fn(&ParticipantTrait, Vec<u8>) -> Result<Vec<u8>, Vec<u8>>;
7271

7372
struct Participant {
7473
info: Info,
75-
process: ProcessFunction,
7674
_trait: & 'static ParticipantTrait,
7775
}
7876

@@ -102,7 +100,6 @@ fn send_discovery(channel: &mut Channel, info: &Info) {
102100
}
103101

104102
struct PortConsumer {
105-
process: ProcessFunction,
106103
participant: & 'static ParticipantTrait,
107104
portname: String,
108105
outqueue: String, // FIXME: allow sending on any port, also multiple times
@@ -130,8 +127,7 @@ impl Consumer for PortConsumer {
130127
body: Vec<u8>) {
131128

132129
debug!("calling process()");
133-
let f = self.process;
134-
let res = f(self.participant, body);
130+
let res = self.participant.process(body);
135131
debug!("process() returned");
136132

137133
if res.is_ok() {
@@ -151,7 +147,6 @@ fn setup_inport(participant: &Participant, port: &Port, connection: &mut Connect
151147
debug!("setup inport: {}", port.queue.to_string());
152148

153149
let consumer = PortConsumer {
154-
process: participant.process,
155150
participant: participant._trait,
156151
portname: port.id.to_string(),
157152
outqueue: participant.info.outports[0].queue.to_string(),
@@ -275,15 +270,15 @@ fn parse(options: &mut Options) {
275270
// XXX: seems rust-amqp makes program hangs forever if error occurs / channel is borked?
276271
// TODO: pass port info in/out of process()
277272
// TODO: nicer way to declare ports? ideally they are enums not stringly typed?
278-
pub fn main(orig: & 'static ParticipantTrait, func: ProcessFunction) {
273+
pub fn main(orig: & 'static ParticipantTrait) {
279274

280275
let mut options = Options { .. Default::default() };
281276
parse(&mut options);
282277

283278
let i : Info = orig.info();
284279
let info = normalize_info(&i, &options);
285280

286-
let p = Participant { info: info, process: func, _trait: orig }; // XXX: hack
281+
let p = Participant { info: info, _trait: orig }; // XXX: hack
287282

288283
let mut c = start_participant(&p, &options);
289284
println!("{}({}) started", &p.info.role, &p.info.component);

0 commit comments

Comments
 (0)