Skip to content

Commit 10fdc36

Browse files
committed
Reorganizing delivery instructions
Signed-off-by: Michael X. Grey <grey@openrobotics.org>
1 parent 2565920 commit 10fdc36

File tree

9 files changed

+392
-297
lines changed

9 files changed

+392
-297
lines changed

src/channel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ mod tests {
168168
assert!(promise.peek().is_available());
169169
}
170170

171-
let count = context.app.world.get::<RunCount>(hello.get()).unwrap().0;
171+
let count = context.app.world.get::<RunCount>(hello.provider()).unwrap().0;
172172
assert_eq!(count, 25);
173173

174-
let count = context.app.world.get::<RunCount>(repeat.get()).unwrap().0;
174+
let count = context.app.world.get::<RunCount>(repeat.provider()).unwrap().0;
175175
assert_eq!(count, 5);
176176
}
177177
}

src/operation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
use crate::{
19-
RequestLabelId, Cancel, ManageInput, InspectInput, UnhandledErrors,
19+
DeliveryLabelId, Cancel, ManageInput, InspectInput, UnhandledErrors,
2020
CancelFailure, Broken, ManageCancellation, ManageDisposal,
2121
};
2222

@@ -260,7 +260,7 @@ pub(crate) struct Blocker {
260260
/// The session that is doing the blocking
261261
pub(crate) session: Entity,
262262
/// The label of the queue that is being blocked
263-
pub(crate) label: Option<RequestLabelId>,
263+
pub(crate) label: Option<DeliveryLabelId>,
264264
/// Function pointer to call when this is no longer blocking
265265
pub(crate) serve_next: fn(Blocker, &mut World, &mut OperationRoster),
266266
}

src/operation/operate_service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<Request: 'static + Send + Sync> OperateService<Request> {
4545
target: Entity,
4646
) -> Self {
4747
Self {
48-
provider: provider.get(),
48+
provider: provider.provider(),
4949
target,
5050
_ignore: Default::default(),
5151
}

src/request.rs

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,13 @@ use crate::{
2020
ModifiersUnset, AddOperation, Noop, IntoAsyncMap,
2121
};
2222

23-
use bevy::{
24-
prelude::Commands,
25-
utils::define_label,
26-
};
23+
use bevy::prelude::Commands;
2724

2825
use std::future::Future;
2926

3027
mod internal;
3128
pub use internal::{ApplyLabel, BuildLabel};
3229

33-
define_label!(
34-
/// A strongly-typed class of labels used to identify requests that have been
35-
/// issued to a service.
36-
RequestLabel,
37-
/// Strongly-typed identifier for a [`RequestLabel`].
38-
RequestLabelId,
39-
);
40-
4130
pub trait RequestExt<'w, 's> {
4231
/// Call this on [`Commands`] to begin building a impulse chain by submitting
4332
/// a request to a provider.
@@ -130,97 +119,6 @@ async fn async_server<T: Future>(value: T) -> T::Output {
130119
value.await
131120
}
132121

133-
/// By default when a service provider receives a new request with the same
134-
/// label as an earlier request, the earlier request will be cancelled,
135-
/// whether it is already being executed or whether it is sitting in a
136-
/// queue. If the earlier request was already delivered then the labeling
137-
/// has no effect.
138-
///
139-
/// To change the default behavior there are two modifiers you can apply to
140-
/// this label:
141-
/// - `.queue()` asks for the request to be queued up to run after all
142-
/// other requests with this same label have been fulfilled and not cancel
143-
/// any of them.
144-
/// - `.ensure()` asks for this request to not be cancelled even if another
145-
/// request comes in with the same label. The new request will instead be
146-
/// queued after this one.
147-
///
148-
/// You can choose to use either, both, or neither of these modifiers in
149-
/// whatever way fits your use case. No matter what modifiers you choose
150-
/// (or don't choose) the same service provider will never simultaneously
151-
/// execute its service for two requests with the same label value. To that
152-
/// extent, applying a label always guarantees mutual exclusivity between
153-
/// requests.
154-
///
155-
/// This mutual exclusivity can be useful if the service involves making
156-
/// modifications to the world which would conflict with each other when two
157-
/// related requests are being delivered at the same time.
158-
pub struct LabelBuilder<Q, E> {
159-
label: RequestLabelId,
160-
queue: bool,
161-
ensure: bool,
162-
_ignore: std::marker::PhantomData<(Q, E)>,
163-
}
164-
165-
pub struct Chosen;
166-
167-
impl LabelBuilder<(), ()> {
168-
/// Begin building a label for a request. You do not need to call this
169-
/// function explicitly. You can instead use `.queue()` or `.ensure()`
170-
/// directly on a `RequestLabel` instance.
171-
pub fn new(label: impl RequestLabel) -> LabelBuilder<(), ()> {
172-
LabelBuilder {
173-
label: label.as_label(),
174-
queue: false,
175-
ensure: false,
176-
_ignore: Default::default()
177-
}
178-
}
179-
}
180-
181-
impl<E> LabelBuilder<(), E> {
182-
/// Queue this labeled request to be handled **after** all other requests
183-
/// with the same label have been fulfilled. Do not automatically cancel
184-
/// pending requests that have the same label.
185-
///
186-
/// The default behavior, if you do **not** trigger this method, is for this
187-
/// new labeled request to supplant all prior requests that share the same
188-
/// label, sending them to the cancelled state (unless the prior request was
189-
/// marked with [`ensure()`]).
190-
///
191-
/// This modifer can only be applied to a labeled request because it does
192-
/// not make sense for unlabeled requests.
193-
pub fn queue(self) -> LabelBuilder<Chosen, E> {
194-
LabelBuilder {
195-
label: self.label,
196-
queue: true,
197-
ensure: self.ensure,
198-
_ignore: Default::default(),
199-
}
200-
}
201-
}
202-
203-
impl<Q> LabelBuilder<Q, ()> {
204-
/// Ensure that this request is resolved even if another request with the
205-
/// same label arrives.
206-
///
207-
/// Ordinarily a new labeled request would supplant all earlier requests
208-
/// with the same label, sending them into the cancelled state. But any
209-
/// of those requests that are "ensured" will remain queued and finish
210-
/// executing, one at a time.
211-
///
212-
/// This modifier can only be applied to labeled requests because it does
213-
/// not make sense for unlabeled requests.
214-
pub fn ensure(self) -> LabelBuilder<Q, Chosen> {
215-
LabelBuilder {
216-
label: self.label,
217-
queue: self.queue,
218-
ensure: true,
219-
_ignore: Default::default(),
220-
}
221-
}
222-
}
223-
224122
#[cfg(test)]
225123
mod tests {
226124
use crate::{*, testing::*};

src/request/internal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
use crate::{
19-
LabelBuilder, DeliveryInstructions, RequestLabel, Chosen,
19+
LabelBuilder, DeliveryInstructions, DeliveryLabel, Chosen,
2020
};
2121

2222
use bevy::ecs::system::EntityCommands;
@@ -38,7 +38,7 @@ pub trait ApplyLabel {
3838
fn apply<'w, 's, 'a>(self, commands: &mut EntityCommands<'w, 's, 'a>);
3939
}
4040

41-
impl<T: RequestLabel> ApplyLabel for T {
41+
impl<T: DeliveryLabel> ApplyLabel for T {
4242
fn apply<'w, 's, 'a>(self, commands: &mut EntityCommands<'w, 's, 'a>) {
4343
LabelBuilder::new(self).apply(commands)
4444
}
@@ -48,7 +48,7 @@ impl<Q, E> ApplyLabel for LabelBuilder<Q, E> {
4848
fn apply<'w, 's, 'a>(self, commands: &mut EntityCommands<'w, 's, 'a>) {
4949
commands.insert(DeliveryInstructions {
5050
label: self.label,
51-
queue: self.queue,
51+
preempt: self.queue,
5252
ensure: self.ensure,
5353
});
5454
}
@@ -61,7 +61,7 @@ pub trait BuildLabel {
6161
fn ensure(self) -> LabelBuilder<(), Chosen>;
6262
}
6363

64-
impl<T: RequestLabel> BuildLabel for T {
64+
impl<T: DeliveryLabel> BuildLabel for T {
6565
/// Specify that the labeled request should queue itself instead of
6666
/// cancelling prior requests with the same label.
6767
fn queue(self) -> LabelBuilder<Chosen, ()> {

0 commit comments

Comments
 (0)