Skip to content

Commit daa57fe

Browse files
Add try_* to add_slot_edge, add_node_edge (#6720)
# Objective `add_node_edge` and `add_slot_edge` are fallible methods, but are always used with `.unwrap()`. `input_node` is often unwrapped as well. This points to having an infallible behaviour as default, with an alternative fallible variant if needed. Improves readability and ergonomics. ## Solution - Change `add_node_edge` and `add_slot_edge` to panic on error. - Change `input_node` to panic on `None`. - Add `try_add_node_edge` and `try_add_slot_edge` in case fallible methods are needed. - Add `get_input_node` to still be able to get an `Option`. --- ## Changelog ### Added - `try_add_node_edge` - `try_add_slot_edge` - `get_input_node` ### Changed - `add_node_edge` is now infallible (panics on error) - `add_slot_edge` is now infallible (panics on error) - `input_node` now panics on `None` ## Migration Guide Remove `.unwrap()` from `add_node_edge` and `add_slot_edge`. For cases where the error was handled, use `try_add_node_edge` and `try_add_slot_edge` instead. Remove `.unwrap()` from `input_node`. For cases where the option was handled, use `get_input_node` instead. Co-authored-by: Torstein Grindvik <52322338+torsteingrindvik@users.noreply.github.com>
1 parent e2d1d9d commit daa57fe

File tree

10 files changed

+251
-254
lines changed

10 files changed

+251
-254
lines changed

crates/bevy_core_pipeline/src/bloom/mod.rs

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,21 @@ impl Plugin for BloomPlugin {
5959
.get_sub_graph_mut(crate::core_3d::graph::NAME)
6060
.unwrap();
6161
draw_3d_graph.add_node(core_3d::graph::node::BLOOM, bloom_node);
62-
draw_3d_graph
63-
.add_slot_edge(
64-
draw_3d_graph.input_node().unwrap().id,
65-
crate::core_3d::graph::input::VIEW_ENTITY,
66-
core_3d::graph::node::BLOOM,
67-
BloomNode::IN_VIEW,
68-
)
69-
.unwrap();
62+
draw_3d_graph.add_slot_edge(
63+
draw_3d_graph.input_node().id,
64+
crate::core_3d::graph::input::VIEW_ENTITY,
65+
core_3d::graph::node::BLOOM,
66+
BloomNode::IN_VIEW,
67+
);
7068
// MAIN_PASS -> BLOOM -> TONEMAPPING
71-
draw_3d_graph
72-
.add_node_edge(
73-
crate::core_3d::graph::node::MAIN_PASS,
74-
core_3d::graph::node::BLOOM,
75-
)
76-
.unwrap();
77-
draw_3d_graph
78-
.add_node_edge(
79-
core_3d::graph::node::BLOOM,
80-
crate::core_3d::graph::node::TONEMAPPING,
81-
)
82-
.unwrap();
69+
draw_3d_graph.add_node_edge(
70+
crate::core_3d::graph::node::MAIN_PASS,
71+
core_3d::graph::node::BLOOM,
72+
);
73+
draw_3d_graph.add_node_edge(
74+
core_3d::graph::node::BLOOM,
75+
crate::core_3d::graph::node::TONEMAPPING,
76+
);
8377
}
8478

8579
{
@@ -89,27 +83,21 @@ impl Plugin for BloomPlugin {
8983
.get_sub_graph_mut(crate::core_2d::graph::NAME)
9084
.unwrap();
9185
draw_2d_graph.add_node(core_2d::graph::node::BLOOM, bloom_node);
92-
draw_2d_graph
93-
.add_slot_edge(
94-
draw_2d_graph.input_node().unwrap().id,
95-
crate::core_2d::graph::input::VIEW_ENTITY,
96-
core_2d::graph::node::BLOOM,
97-
BloomNode::IN_VIEW,
98-
)
99-
.unwrap();
86+
draw_2d_graph.add_slot_edge(
87+
draw_2d_graph.input_node().id,
88+
crate::core_2d::graph::input::VIEW_ENTITY,
89+
core_2d::graph::node::BLOOM,
90+
BloomNode::IN_VIEW,
91+
);
10092
// MAIN_PASS -> BLOOM -> TONEMAPPING
101-
draw_2d_graph
102-
.add_node_edge(
103-
crate::core_2d::graph::node::MAIN_PASS,
104-
core_2d::graph::node::BLOOM,
105-
)
106-
.unwrap();
107-
draw_2d_graph
108-
.add_node_edge(
109-
core_2d::graph::node::BLOOM,
110-
crate::core_2d::graph::node::TONEMAPPING,
111-
)
112-
.unwrap();
93+
draw_2d_graph.add_node_edge(
94+
crate::core_2d::graph::node::MAIN_PASS,
95+
core_2d::graph::node::BLOOM,
96+
);
97+
draw_2d_graph.add_node_edge(
98+
core_2d::graph::node::BLOOM,
99+
crate::core_2d::graph::node::TONEMAPPING,
100+
);
113101
}
114102
}
115103
}

crates/bevy_core_pipeline/src/core_2d/mod.rs

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -72,45 +72,33 @@ impl Plugin for Core2dPlugin {
7272
graph::input::VIEW_ENTITY,
7373
SlotType::Entity,
7474
)]);
75-
draw_2d_graph
76-
.add_slot_edge(
77-
input_node_id,
78-
graph::input::VIEW_ENTITY,
79-
graph::node::MAIN_PASS,
80-
MainPass2dNode::IN_VIEW,
81-
)
82-
.unwrap();
83-
draw_2d_graph
84-
.add_slot_edge(
85-
input_node_id,
86-
graph::input::VIEW_ENTITY,
87-
graph::node::TONEMAPPING,
88-
TonemappingNode::IN_VIEW,
89-
)
90-
.unwrap();
91-
draw_2d_graph
92-
.add_slot_edge(
93-
input_node_id,
94-
graph::input::VIEW_ENTITY,
95-
graph::node::UPSCALING,
96-
UpscalingNode::IN_VIEW,
97-
)
98-
.unwrap();
99-
draw_2d_graph
100-
.add_node_edge(graph::node::MAIN_PASS, graph::node::TONEMAPPING)
101-
.unwrap();
102-
draw_2d_graph
103-
.add_node_edge(
104-
graph::node::TONEMAPPING,
105-
graph::node::END_MAIN_PASS_POST_PROCESSING,
106-
)
107-
.unwrap();
108-
draw_2d_graph
109-
.add_node_edge(
110-
graph::node::END_MAIN_PASS_POST_PROCESSING,
111-
graph::node::UPSCALING,
112-
)
113-
.unwrap();
75+
draw_2d_graph.add_slot_edge(
76+
input_node_id,
77+
graph::input::VIEW_ENTITY,
78+
graph::node::MAIN_PASS,
79+
MainPass2dNode::IN_VIEW,
80+
);
81+
draw_2d_graph.add_slot_edge(
82+
input_node_id,
83+
graph::input::VIEW_ENTITY,
84+
graph::node::TONEMAPPING,
85+
TonemappingNode::IN_VIEW,
86+
);
87+
draw_2d_graph.add_slot_edge(
88+
input_node_id,
89+
graph::input::VIEW_ENTITY,
90+
graph::node::UPSCALING,
91+
UpscalingNode::IN_VIEW,
92+
);
93+
draw_2d_graph.add_node_edge(graph::node::MAIN_PASS, graph::node::TONEMAPPING);
94+
draw_2d_graph.add_node_edge(
95+
graph::node::TONEMAPPING,
96+
graph::node::END_MAIN_PASS_POST_PROCESSING,
97+
);
98+
draw_2d_graph.add_node_edge(
99+
graph::node::END_MAIN_PASS_POST_PROCESSING,
100+
graph::node::UPSCALING,
101+
);
114102
graph.add_sub_graph(graph::NAME, draw_2d_graph);
115103
}
116104
}

crates/bevy_core_pipeline/src/core_3d/mod.rs

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -82,45 +82,33 @@ impl Plugin for Core3dPlugin {
8282
graph::input::VIEW_ENTITY,
8383
SlotType::Entity,
8484
)]);
85-
draw_3d_graph
86-
.add_slot_edge(
87-
input_node_id,
88-
graph::input::VIEW_ENTITY,
89-
graph::node::MAIN_PASS,
90-
MainPass3dNode::IN_VIEW,
91-
)
92-
.unwrap();
93-
draw_3d_graph
94-
.add_slot_edge(
95-
input_node_id,
96-
graph::input::VIEW_ENTITY,
97-
graph::node::TONEMAPPING,
98-
TonemappingNode::IN_VIEW,
99-
)
100-
.unwrap();
101-
draw_3d_graph
102-
.add_slot_edge(
103-
input_node_id,
104-
graph::input::VIEW_ENTITY,
105-
graph::node::UPSCALING,
106-
UpscalingNode::IN_VIEW,
107-
)
108-
.unwrap();
109-
draw_3d_graph
110-
.add_node_edge(graph::node::MAIN_PASS, graph::node::TONEMAPPING)
111-
.unwrap();
112-
draw_3d_graph
113-
.add_node_edge(
114-
graph::node::TONEMAPPING,
115-
graph::node::END_MAIN_PASS_POST_PROCESSING,
116-
)
117-
.unwrap();
118-
draw_3d_graph
119-
.add_node_edge(
120-
graph::node::END_MAIN_PASS_POST_PROCESSING,
121-
graph::node::UPSCALING,
122-
)
123-
.unwrap();
85+
draw_3d_graph.add_slot_edge(
86+
input_node_id,
87+
graph::input::VIEW_ENTITY,
88+
graph::node::MAIN_PASS,
89+
MainPass3dNode::IN_VIEW,
90+
);
91+
draw_3d_graph.add_slot_edge(
92+
input_node_id,
93+
graph::input::VIEW_ENTITY,
94+
graph::node::TONEMAPPING,
95+
TonemappingNode::IN_VIEW,
96+
);
97+
draw_3d_graph.add_slot_edge(
98+
input_node_id,
99+
graph::input::VIEW_ENTITY,
100+
graph::node::UPSCALING,
101+
UpscalingNode::IN_VIEW,
102+
);
103+
draw_3d_graph.add_node_edge(graph::node::MAIN_PASS, graph::node::TONEMAPPING);
104+
draw_3d_graph.add_node_edge(
105+
graph::node::TONEMAPPING,
106+
graph::node::END_MAIN_PASS_POST_PROCESSING,
107+
);
108+
draw_3d_graph.add_node_edge(
109+
graph::node::END_MAIN_PASS_POST_PROCESSING,
110+
graph::node::UPSCALING,
111+
);
124112
graph.add_sub_graph(graph::NAME, draw_3d_graph);
125113
}
126114
}

crates/bevy_core_pipeline/src/fxaa/mod.rs

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,21 @@ impl Plugin for FxaaPlugin {
103103

104104
graph.add_node(core_3d::graph::node::FXAA, fxaa_node);
105105

106-
graph
107-
.add_slot_edge(
108-
graph.input_node().unwrap().id,
109-
core_3d::graph::input::VIEW_ENTITY,
110-
core_3d::graph::node::FXAA,
111-
FxaaNode::IN_VIEW,
112-
)
113-
.unwrap();
114-
115-
graph
116-
.add_node_edge(
117-
core_3d::graph::node::TONEMAPPING,
118-
core_3d::graph::node::FXAA,
119-
)
120-
.unwrap();
121-
graph
122-
.add_node_edge(
123-
core_3d::graph::node::FXAA,
124-
core_3d::graph::node::END_MAIN_PASS_POST_PROCESSING,
125-
)
126-
.unwrap();
106+
graph.add_slot_edge(
107+
graph.input_node().id,
108+
core_3d::graph::input::VIEW_ENTITY,
109+
core_3d::graph::node::FXAA,
110+
FxaaNode::IN_VIEW,
111+
);
112+
113+
graph.add_node_edge(
114+
core_3d::graph::node::TONEMAPPING,
115+
core_3d::graph::node::FXAA,
116+
);
117+
graph.add_node_edge(
118+
core_3d::graph::node::FXAA,
119+
core_3d::graph::node::END_MAIN_PASS_POST_PROCESSING,
120+
);
127121
}
128122
{
129123
let fxaa_node = FxaaNode::new(&mut render_app.world);
@@ -132,27 +126,21 @@ impl Plugin for FxaaPlugin {
132126

133127
graph.add_node(core_2d::graph::node::FXAA, fxaa_node);
134128

135-
graph
136-
.add_slot_edge(
137-
graph.input_node().unwrap().id,
138-
core_2d::graph::input::VIEW_ENTITY,
139-
core_2d::graph::node::FXAA,
140-
FxaaNode::IN_VIEW,
141-
)
142-
.unwrap();
143-
144-
graph
145-
.add_node_edge(
146-
core_2d::graph::node::TONEMAPPING,
147-
core_2d::graph::node::FXAA,
148-
)
149-
.unwrap();
150-
graph
151-
.add_node_edge(
152-
core_2d::graph::node::FXAA,
153-
core_2d::graph::node::END_MAIN_PASS_POST_PROCESSING,
154-
)
155-
.unwrap();
129+
graph.add_slot_edge(
130+
graph.input_node().id,
131+
core_2d::graph::input::VIEW_ENTITY,
132+
core_2d::graph::node::FXAA,
133+
FxaaNode::IN_VIEW,
134+
);
135+
136+
graph.add_node_edge(
137+
core_2d::graph::node::TONEMAPPING,
138+
core_2d::graph::node::FXAA,
139+
);
140+
graph.add_node_edge(
141+
core_2d::graph::node::FXAA,
142+
core_2d::graph::node::END_MAIN_PASS_POST_PROCESSING,
143+
);
156144
}
157145
}
158146
}

crates/bevy_pbr/src/lib.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,19 +256,15 @@ impl Plugin for PbrPlugin {
256256
.get_sub_graph_mut(bevy_core_pipeline::core_3d::graph::NAME)
257257
.unwrap();
258258
draw_3d_graph.add_node(draw_3d_graph::node::SHADOW_PASS, shadow_pass_node);
259-
draw_3d_graph
260-
.add_node_edge(
261-
draw_3d_graph::node::SHADOW_PASS,
262-
bevy_core_pipeline::core_3d::graph::node::MAIN_PASS,
263-
)
264-
.unwrap();
265-
draw_3d_graph
266-
.add_slot_edge(
267-
draw_3d_graph.input_node().unwrap().id,
268-
bevy_core_pipeline::core_3d::graph::input::VIEW_ENTITY,
269-
draw_3d_graph::node::SHADOW_PASS,
270-
ShadowPassNode::IN_VIEW,
271-
)
272-
.unwrap();
259+
draw_3d_graph.add_node_edge(
260+
draw_3d_graph::node::SHADOW_PASS,
261+
bevy_core_pipeline::core_3d::graph::node::MAIN_PASS,
262+
);
263+
draw_3d_graph.add_slot_edge(
264+
draw_3d_graph.input_node().id,
265+
bevy_core_pipeline::core_3d::graph::input::VIEW_ENTITY,
266+
draw_3d_graph::node::SHADOW_PASS,
267+
ShadowPassNode::IN_VIEW,
268+
);
273269
}
274270
}

crates/bevy_render/src/render_graph/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl<'a> RenderGraphContext<'a> {
169169
.graph
170170
.get_sub_graph(&name)
171171
.ok_or_else(|| RunSubGraphError::MissingSubGraph(name.clone()))?;
172-
if let Some(input_node) = sub_graph.input_node() {
172+
if let Some(input_node) = sub_graph.get_input_node() {
173173
for (i, input_slot) in input_node.input_slots.iter().enumerate() {
174174
if let Some(input_value) = inputs.get(i) {
175175
if input_slot.slot_type != input_value.slot_type() {

0 commit comments

Comments
 (0)