Skip to content

Commit 9175367

Browse files
authored
Message registry (#50)
Signed-off-by: Teo Koon Peng <teokoonpeng@gmail.com>
1 parent 013dc62 commit 9175367

File tree

12 files changed

+1537
-1133
lines changed

12 files changed

+1537
-1133
lines changed

examples/diagram/calculator/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::{error::Error, fs::File, str::FromStr};
22

33
use bevy_impulse::{
4-
Diagram, DiagramError, ImpulsePlugin, NodeBuilderOptions, NodeRegistry, Promise, RequestExt,
5-
RunCommandsOnWorldExt,
4+
Diagram, DiagramElementRegistry, DiagramError, ImpulsePlugin, NodeBuilderOptions, Promise,
5+
RequestExt, RunCommandsOnWorldExt,
66
};
77
use clap::Parser;
88

@@ -21,7 +21,7 @@ fn main() -> Result<(), Box<dyn Error>> {
2121

2222
tracing_subscriber::fmt::init();
2323

24-
let mut registry = NodeRegistry::default();
24+
let mut registry = DiagramElementRegistry::new();
2525
registry.register_node_builder(
2626
NodeBuilderOptions::new("add").with_name("Add"),
2727
|builder, config: f64| builder.create_map_block(move |req: f64| req + config),

src/diagram.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod fork_clone;
22
mod fork_result;
33
mod impls;
44
mod join;
5-
mod node_registry;
5+
mod registration;
66
mod serialization;
77
mod split_serialized;
88
mod transform;
@@ -14,7 +14,7 @@ use fork_clone::ForkCloneOp;
1414
use fork_result::ForkResultOp;
1515
use join::JoinOp;
1616
pub use join::JoinOutput;
17-
pub use node_registry::*;
17+
pub use registration::*;
1818
pub use serialization::*;
1919
pub use split_serialized::*;
2020
use tracing::debug;
@@ -380,10 +380,10 @@ impl Diagram {
380380
/// # Examples
381381
///
382382
/// ```
383-
/// use bevy_impulse::{Diagram, DiagramError, NodeBuilderOptions, NodeRegistry, RunCommandsOnWorldExt};
383+
/// use bevy_impulse::{Diagram, DiagramError, NodeBuilderOptions, DiagramElementRegistry, RunCommandsOnWorldExt};
384384
///
385385
/// let mut app = bevy_app::App::new();
386-
/// let mut registry = NodeRegistry::default();
386+
/// let mut registry = DiagramElementRegistry::new();
387387
/// registry.register_node_builder(NodeBuilderOptions::new("echo".to_string()), |builder, _config: ()| {
388388
/// builder.create_map_block(|msg: String| msg)
389389
/// });
@@ -411,7 +411,7 @@ impl Diagram {
411411
fn spawn_workflow<Streams>(
412412
&self,
413413
cmds: &mut Commands,
414-
registry: &NodeRegistry,
414+
registry: &DiagramElementRegistry,
415415
) -> Result<Service<DiagramStart, DiagramTerminate, Streams>, DiagramError>
416416
where
417417
Streams: StreamPack,
@@ -451,7 +451,7 @@ impl Diagram {
451451
pub fn spawn_io_workflow(
452452
&self,
453453
cmds: &mut Commands,
454-
registry: &NodeRegistry,
454+
registry: &DiagramElementRegistry,
455455
) -> Result<Service<DiagramStart, DiagramTerminate, ()>, DiagramError> {
456456
self.spawn_workflow::<()>(cmds, registry)
457457
}
@@ -506,6 +506,9 @@ pub enum DiagramError {
506506
#[error("response cannot be split")]
507507
NotSplittable,
508508

509+
#[error("responses cannot be joined")]
510+
NotJoinable,
511+
509512
#[error("empty join is not allowed")]
510513
EmptyJoin,
511514

@@ -557,7 +560,7 @@ mod tests {
557560
"ops": {
558561
"op1": {
559562
"type": "node",
560-
"builder": "multiply3_uncloneable",
563+
"builder": "multiply3",
561564
"next": { "builtin": "dispose" },
562565
},
563566
},
@@ -625,7 +628,7 @@ mod tests {
625628
"ops": {
626629
"op1": {
627630
"type": "node",
628-
"builder": "multiply3_uncloneable",
631+
"builder": "multiply3",
629632
"next": "op2",
630633
},
631634
"op2": {
@@ -651,12 +654,12 @@ mod tests {
651654
"ops": {
652655
"op1": {
653656
"type": "node",
654-
"builder": "multiply3_uncloneable",
657+
"builder": "multiply3",
655658
"next": "op2",
656659
},
657660
"op2": {
658661
"type": "node",
659-
"builder": "multiply3_uncloneable",
662+
"builder": "multiply3",
660663
"next": "op1",
661664
},
662665
},
@@ -691,7 +694,7 @@ mod tests {
691694
},
692695
"op2": {
693696
"type": "node",
694-
"builder": "multiply3_uncloneable",
697+
"builder": "multiply3",
695698
"next": { "builtin": "terminate" },
696699
},
697700
},
@@ -728,11 +731,11 @@ mod tests {
728731
let json_str = r#"
729732
{
730733
"version": "0.1.0",
731-
"start": "multiply3_uncloneable",
734+
"start": "multiply3",
732735
"ops": {
733-
"multiply3_uncloneable": {
736+
"multiply3": {
734737
"type": "node",
735-
"builder": "multiplyBy",
738+
"builder": "multiply_by",
736739
"config": 7,
737740
"next": { "builtin": "terminate" }
738741
}

src/diagram/impls.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1+
use std::marker::PhantomData;
2+
13
/// A struct to provide the default implementation for various operations.
24
pub struct DefaultImpl;
35

6+
/// A struct to provide the default implementation for various operations.
7+
pub struct DefaultImplMarker<T> {
8+
_unused: PhantomData<T>,
9+
}
10+
11+
impl<T> DefaultImplMarker<T> {
12+
pub(super) fn new() -> Self {
13+
Self {
14+
_unused: Default::default(),
15+
}
16+
}
17+
}
18+
419
/// A struct to provide "not supported" implementations for various operations.
520
pub struct NotSupported;
21+
22+
/// A struct to provide "not supported" implementations for various operations.
23+
pub struct NotSupportedMarker<T> {
24+
_unused: PhantomData<T>,
25+
}

src/diagram/join.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::any::TypeId;
2-
31
use schemars::JsonSchema;
42
use serde::{Deserialize, Serialize};
53
use smallvec::SmallVec;
@@ -30,13 +28,7 @@ where
3028
T: Send + Sync + 'static,
3129
Serializer: SerializeMessage<Vec<T>>,
3230
{
33-
if registry.join_impls.contains_key(&TypeId::of::<T>()) {
34-
return;
35-
}
36-
37-
registry
38-
.join_impls
39-
.insert(TypeId::of::<T>(), Box::new(join_impl::<T>));
31+
registry.register_join::<T>(Box::new(join_impl::<T>));
4032
}
4133

4234
/// Serialize the outputs before joining them, and convert the resulting joined output into a
@@ -55,14 +47,7 @@ pub(super) fn serialize_and_join(
5547

5648
let outputs = outputs
5749
.into_iter()
58-
.map(|o| {
59-
let serialize_impl = registry
60-
.serialize_impls
61-
.get(&o.type_id)
62-
.ok_or(DiagramError::NotSerializable)?;
63-
let serialized_output = serialize_impl(builder, o)?;
64-
Ok(serialized_output)
65-
})
50+
.map(|o| registry.serialize(builder, o))
6651
.collect::<Result<Vec<_>, DiagramError>>()?;
6752

6853
// we need to convert the joined output to [`serde_json::Value`] in order for it to be
@@ -163,7 +148,7 @@ mod tests {
163148
},
164149
"op1": {
165150
"type": "node",
166-
"builder": "multiply3_uncloneable",
151+
"builder": "multiply3",
167152
"next": "join",
168153
},
169154
"get_split_value2": {
@@ -173,7 +158,7 @@ mod tests {
173158
},
174159
"op2": {
175160
"type": "node",
176-
"builder": "multiply3_uncloneable",
161+
"builder": "multiply3",
177162
"next": "join",
178163
},
179164
"join": {
@@ -235,7 +220,7 @@ mod tests {
235220
},
236221
"op1": {
237222
"type": "node",
238-
"builder": "multiply3_uncloneable",
223+
"builder": "multiply3",
239224
"next": { "builtin": "terminate" },
240225
},
241226
"get_split_value2": {
@@ -245,7 +230,7 @@ mod tests {
245230
},
246231
"op2": {
247232
"type": "node",
248-
"builder": "multiply3_uncloneable",
233+
"builder": "multiply3",
249234
"next": { "builtin": "terminate" },
250235
},
251236
"join": {

0 commit comments

Comments
 (0)