Skip to content

Commit c7aa26e

Browse files
committed
Document additions.
1 parent 9aadae6 commit c7aa26e

1 file changed

Lines changed: 58 additions & 1 deletion

File tree

src/world.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl World {
205205
self.entities.contains(entity)
206206
}
207207

208-
/// Efficiently iterate over all entities that have certain components
208+
/// Efficiently iterate over all entities that have certain components.
209209
///
210210
/// Calling `iter` on the returned value yields `(Entity, Q)` tuples, where `Q` is some query
211211
/// type. A query type is `&T`, `&mut T`, a tuple of query types, or an `Option` wrapping a
@@ -229,6 +229,12 @@ impl World {
229229
/// its dynamic borrows from the world to be released. Similarly, lifetime rules ensure that
230230
/// references obtained from a query cannot outlive the `QueryBorrow`.
231231
///
232+
/// If you need change detection of some sort, you will likely want to use [`query_with_context`]
233+
/// instead; this method is actually shorthand for `.query_with_context(())`. See also [`SmartComponent`].
234+
///
235+
/// [`query_with_context`]: World::query_with_context
236+
/// [`SmartComponent`]: SmartComponent
237+
///
232238
/// # Example
233239
/// ```
234240
/// # use hecs::*;
@@ -248,6 +254,13 @@ impl World {
248254
self.query_with_context(())
249255
}
250256

257+
/// See [`query`]. This method augments `query` with a contex type, accessible by components
258+
/// through the [`SmartComponent`] trait (`query` is actually implemented on top of this
259+
/// method, using the empty context `()`.) If you are implementing an ECS on top of `hecs`
260+
/// which needs some sort of change detection, this is likely the interface you want to use.
261+
///
262+
/// [`query`]: World::query
263+
/// [`SmartComponent`]: SmartComponent
251264
pub fn query_with_context<'q, Q, C>(&'q self, context: C) -> QueryBorrow<'q, Q, C>
252265
where
253266
Q: Query<'q, C>,
@@ -264,6 +277,12 @@ impl World {
264277
///
265278
/// Handy for accessing multiple components simultaneously.
266279
///
280+
/// If you need change detection of some sort, you will likely want to use [`query_one_with_context`]
281+
/// instead; this method is actually shorthand for `.query_one_with_context(())`. See also [`SmartComponent`].
282+
///
283+
/// [`query_one_with_context`]: World::query_with_context
284+
/// [`SmartComponent`]: SmartComponent
285+
///
267286
/// # Example
268287
/// ```
269288
/// # use hecs::*;
@@ -282,6 +301,13 @@ impl World {
282301
self.query_one_with_context(entity, ())
283302
}
284303

304+
/// See [`query_one`]. This method augments `query_one` with a contex type, accessible by components
305+
/// through the [`SmartComponent`] trait (`query_one` is actually implemented on top of this
306+
/// method, using the empty context `()`.) If you are implementing an ECS on top of `hecs`
307+
/// which needs some sort of change detection, this is likely the interface you want to use.
308+
///
309+
/// [`query_one`]: World::query_one
310+
/// [`SmartComponent`]: SmartComponent
285311
pub fn query_one_with_context<'q, Q, C>(
286312
&'q self,
287313
entity: Entity,
@@ -324,6 +350,12 @@ impl World {
324350
self.entity_with_context(entity, ())
325351
}
326352

353+
/// See [`get`]. This method allows `get` to be augmented with a context type `C`, provided to
354+
/// components which implement `SmartComponent<C>`. This allows for detecting when the component
355+
/// is accessed immutably. See also [`SmartComponent`].
356+
///
357+
/// [`get`]: World::get
358+
/// [`SmartComponent`]: SmartComponent
327359
pub fn get_with_context<T: SmartComponent<C>, C: Clone>(
328360
&self,
329361
entity: Entity,
@@ -343,6 +375,12 @@ impl World {
343375
})
344376
}
345377

378+
/// See [`get_mut`]. This method allows `get_mut` to be augmented with a context type `C`, provided to
379+
/// components which implement `SmartComponent<C>`. This allows for detecting when the component
380+
/// is accessed mutably. See also [`SmartComponent`].
381+
///
382+
/// [`get_mut`]: World::get_mut
383+
/// [`SmartComponent`]: SmartComponent
346384
pub fn get_mut_with_context<T: SmartComponent<C>, C: Clone>(
347385
&self,
348386
entity: Entity,
@@ -362,6 +400,12 @@ impl World {
362400
})
363401
}
364402

403+
/// See [`entity`]. This method allows `entity` to be augmented with a context type `C`, provided to
404+
/// components which implement `SmartComponent<C>`. This allows for detecting when the component is
405+
/// accessed, and how. See also [`SmartComponent`].
406+
///
407+
/// [`entity`]: World::entity
408+
/// [`SmartComponent`]: SmartComponent
365409
pub fn entity_with_context<C: Clone>(
366410
&self,
367411
entity: Entity,
@@ -720,8 +764,21 @@ impl From<MissingComponent> for ComponentError {
720764
pub trait Component: Send + Sync + 'static {}
721765
impl<T: Send + Sync + 'static> Component for T {}
722766

767+
/// `SmartComponent` is an additional layer on top of `Component` which optionally allows components
768+
/// to be aware of when they are accessed. It is by default implemented for all components w.r.t. the
769+
/// "empty context" `()` (all types that implement `Component` implement `SmartComponent<()>`.)
770+
///
771+
/// The callbacks in this trait are called *when the reference returned from a query is dereferenced;*
772+
/// in other words, these will not be called until the component is derefenced to an `&` or `&mut`.
773+
///
774+
/// The type parameter `T` of `SmartComponent<T>` is the "context" type. It's generally expected that
775+
/// you'll use an immutable reference of some kind here, but anything clonable is fine.
723776
pub trait SmartComponent<T: Clone>: Component {
777+
/// Called when the component is immutably accessed. You probably won't ever need this one.
724778
fn on_borrow(&self, _id: Entity, _x: T) {}
779+
/// Called when the component is mutably accessed. Useful for flagging changes to components.
780+
/// For example, the context type `T` might be a `Mutex<BitSet>` type or similar (`AtomicBitSet`)
781+
/// for which you might set the relevant bit on a mutable access to this component on an entity.
725782
fn on_borrow_mut(&mut self, _id: Entity, _x: T) {}
726783
}
727784

0 commit comments

Comments
 (0)