Skip to content

Commit 018a756

Browse files
committed
Add some docs
Signed-off-by: Michael X. Grey <grey@openrobotics.org>
1 parent 80c46d1 commit 018a756

File tree

3 files changed

+54
-35
lines changed

3 files changed

+54
-35
lines changed

src/callback.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,33 @@ use std::{
4040
/// instance. If the Callback has any internal state (e.g. [`Local`](bevy::prelude::Local)
4141
/// parameters, change trackers, or mutable captured variables), that internal state will
4242
/// be shared among all its clones.
43-
//
44-
// TODO(@mxgrey): Explain the different ways to instantiate a Callback.
43+
///
44+
/// There are three ways to instantiate a callback:
45+
///
46+
/// ### [`.as_callback()`](AsCallback)
47+
///
48+
/// If you have a Bevy system with an input parameter of `In<`[`AsyncCallback`]`>`
49+
/// or `In<`[`BlockingCallback`]`>` then you can convert it into a callback
50+
/// object by applying `.as_callback()`.
51+
///
52+
/// ### [`.into_async_callback()`](IntoAsyncCallback)
53+
///
54+
/// If you have a Bevy system whose return type implements the [`Future`] trait,
55+
/// it can be converted into an async callback object by applying
56+
/// `.into_async_callback()` to it. The `Response` type of the callback will be
57+
/// `Future::Output` rather than the return type of the system. The return value
58+
/// will be polled in an async compute task pool.
59+
///
60+
/// ### [`.into_blocking_callback()`](IntoBlockingCallback)
61+
///
62+
/// Any Bevy system can be converted into a blocking callback by applying
63+
/// `.into_blocking_callback()` to it. The `Request` type of the callback will
64+
/// be whatever the input type of the system is (the `T` inside of `In<T>`). The
65+
/// `Response` type of the callback will be whatever the return value of the
66+
/// callback is.
67+
///
68+
/// A blocking callback is always an exclusive system, so it will block all
69+
/// other systems from running until it is finished.
4570
pub struct Callback<Request, Response, Streams = ()> {
4671
pub(crate) inner: Arc<Mutex<InnerCallback<Request, Response, Streams>>>,
4772
}
@@ -326,6 +351,7 @@ where
326351
}
327352
}
328353

354+
/// Use this to convert any Bevy system into a blocking callback.
329355
pub trait IntoBlockingCallback<M> {
330356
type Request;
331357
type Response;

src/chain.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ impl<'w, 's, 'a, 'b, T: 'static + Send + Sync> Chain<'w, 's, 'a, 'b, T> {
127127
}
128128
}
129129

130-
/// Apply a one-time map whose input is a [`BlockingMap`](crate::BlockingMap)
131-
/// or an [`AsyncMap`](crate::AsyncMap).
130+
/// Apply a function whose input is [`BlockingMap<T>`](crate::BlockingMap)
131+
/// or [`AsyncMap<T>`](crate::AsyncMap).
132132
#[must_use]
133133
pub fn map<M, F: AsMap<M>>(
134134
self,
@@ -156,7 +156,7 @@ impl<'w, 's, 'a, 'b, T: 'static + Send + Sync> Chain<'w, 's, 'a, 'b, T> {
156156
self.then_node(f.as_map())
157157
}
158158

159-
/// Apply a map whose input is the Response of the current Chain. The
159+
/// Apply a function whose input is the Response of the current Chain. The
160160
/// output of the map will be the Response of the returned Chain.
161161
///
162162
/// This takes in a regular blocking function rather than an async function,

src/lib.rs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,17 @@ pub mod testing;
8585

8686
use bevy::prelude::{Entity, In};
8787

88-
/// Use BlockingService to indicate that your system is a blocking service and
89-
/// to specify its input request type. For example:
88+
/// Use `BlockingService` to indicate that your system is a blocking [`Service`].
9089
///
91-
/// ```
92-
/// use bevy::prelude::*;
93-
/// use bevy_impulse::*;
90+
/// A blocking service will have exclusive world access while it runs, which
91+
/// means no other system will be able to run simultaneously. Each service is
92+
/// associated with its own unique entity which can be used to store state or
93+
/// configuration parameters.
9494
///
95-
/// #[derive(Component, Resource)]
96-
/// struct Precision(i32);
97-
///
98-
/// fn rounding_service(
99-
/// In(input): InBlockingService<f64>,
100-
/// global_precision: Res<Precision>,
101-
/// ) -> f64 {
102-
/// (input.request * 10_f64.powi(global_precision.0)).floor() * 10_f64.powi(-global_precision.0)
103-
/// }
104-
/// ```
105-
///
106-
/// The systems of more complex services might need to know what entity is
107-
/// providing the service, e.g. if the service provider is configured with
108-
/// additional components that need to be queried when a request comes in. For
109-
/// that you can check the `provider` field of `BlockingService`:
95+
/// Some services might need to know what entity is providing the service, e.g.
96+
/// if the service provider is configured with additional components that need
97+
/// to be queried when a request comes in. For that you can check the `provider`
98+
/// field of `BlockingService`:
11099
///
111100
/// ```
112101
/// use bevy::prelude::*;
@@ -141,9 +130,9 @@ pub struct BlockingService<Request, Streams: StreamPack = ()> {
141130
/// Use this to reduce bracket noise when you need `In<BlockingService<R>>`.
142131
pub type InBlockingService<Request, Streams = ()> = In<BlockingService<Request, Streams>>;
143132

144-
/// Use AsyncService to indicate that your system is an async service and to
145-
/// specify its input request type. Being async means it must return a
146-
/// `Future<Output=Response>` which will be processed by a task pool.
133+
/// Use AsyncService to indicate that your system is an async [`Service`]. Being
134+
/// async means it must return a [`Future<Output=Response>`](std::future::Future)
135+
/// which will be processed by a task pool.
147136
///
148137
/// This comes with a [`Channel`] that allows your Future to interact with Bevy's
149138
/// ECS asynchronously while it is polled from inside the task pool.
@@ -169,6 +158,10 @@ pub type InAsyncService<Request, Streams = ()> = In<AsyncService<Request, Stream
169158
/// Use BlockingCallback to indicate that your system is meant to define a
170159
/// blocking [`Callback`]. Callbacks are different from services because they are
171160
/// not associated with any entity.
161+
///
162+
/// Alternatively any Bevy system with an input of `In<Request>` can be converted
163+
/// into a blocking callback by applying
164+
/// [`.into_blocking_callback()`](crate::IntoBlockingCallback).
172165
#[non_exhaustive]
173166
pub struct BlockingCallback<Request, Streams: StreamPack = ()> {
174167
/// The input data of the request
@@ -199,9 +192,9 @@ pub struct AsyncCallback<Request, Streams: StreamPack = ()> {
199192
pub session: Entity,
200193
}
201194

202-
/// Use BlockingMap to indicate that your function is meant to define a blocking
203-
/// [`Map`]. A Map is not associated with any entity, and it cannot be a Bevy
204-
/// System. These limited traits allow them to be processed more efficiently.
195+
/// Use `BlockingMap`` to indicate that your function is a blocking map. A map
196+
/// is not associated with any entity, and it cannot be a Bevy System. These
197+
/// restrictions allow them to be processed more efficiently.
205198
#[non_exhaustive]
206199
pub struct BlockingMap<Request, Streams: StreamPack = ()> {
207200
/// The input data of the request
@@ -214,11 +207,11 @@ pub struct BlockingMap<Request, Streams: StreamPack = ()> {
214207
pub session: Entity,
215208
}
216209

217-
/// Use AsyncMap to indicate that your function is meant to define an async
218-
/// [`Map`]. A Map is not associated with any entity, and it cannot be a Bevy
219-
/// System. These limited traits allow them to be processed more efficiently.
210+
/// Use AsyncMap to indicate that your function is an async map. A Map is not
211+
/// associated with any entity, and it cannot be a Bevy System. These
212+
/// restrictions allow them to be processed more efficiently.
220213
///
221-
/// An async Map must return a [`Future<Output=Response>`](std::future::Future)
214+
/// An async map must return a [`Future<Output=Response>`](std::future::Future)
222215
/// that will be polled by the async task pool.
223216
#[non_exhaustive]
224217
pub struct AsyncMap<Request, Streams: StreamPack = ()> {

0 commit comments

Comments
 (0)