1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#![cfg_attr(feature = "nightly", feature(ptr_metadata, strict_provenance))]
//! **Sh**ared **re**source **d**ispatcher
//!
//! This library allows to dispatch
//! systems, which can have interdependencies,
//! shared and exclusive resource access, in parallel.
//!
//! # Examples
//!
//! ```rust
//! extern crate shred;
//!
//! use shred::{DispatcherBuilder, Read, Resource, ResourceId, System, SystemData, World, Write};
//!
//! #[derive(Debug, Default)]
//! struct ResA;
//!
//! #[derive(Debug, Default)]
//! struct ResB;
//!
//! # #[cfg(feature = "shred-derive")]
//! #[derive(SystemData)] // Provided with `shred-derive` feature
//! struct Data<'a> {
//! a: Read<'a, ResA>,
//! b: Write<'a, ResB>,
//! }
//!
//! struct EmptySystem;
//!
//! impl<'a> System<'a> for EmptySystem {
//! type SystemData = Data<'a>;
//!
//! fn run(&mut self, bundle: Data<'a>) {
//! println!("{:?}", &*bundle.a);
//! println!("{:?}", &*bundle.b);
//! }
//! }
//!
//! let mut world = World::empty();
//! let mut dispatcher = DispatcherBuilder::new()
//! .with(EmptySystem, "empty", &[])
//! .build();
//! world.insert(ResA);
//! world.insert(ResB);
//!
//! dispatcher.dispatch(&mut world);
//! #
//! # // The following is required for the snippet to compile without the `shred-derive` feature.
//! #
//! # #[cfg(not(feature = "shred-derive"))]
//! # struct Data<'a> {
//! # a: Read<'a, ResA>,
//! # b: Write<'a, ResB>,
//! # }
//! #
//! # #[cfg(not(feature = "shred-derive"))]
//! # impl<'a> SystemData<'a> for Data<'a> {
//! # fn setup(world: &mut World) {
//! # Read::<'_, ResA>::setup(world);
//! # Write::<'_, ResB>::setup(world);
//! # }
//! #
//! # fn fetch(world: &'a World) -> Self {
//! # Self {
//! # a: Read::<'_, ResA>::fetch(world),
//! # b: Write::<'_, ResB>::fetch(world),
//! # }
//! # }
//! #
//! # fn reads() -> Vec<ResourceId> {
//! # Read::<'_, ResA>::reads()
//! # }
//! #
//! # fn writes() -> Vec<ResourceId> {
//! # Write::<'_, ResB>::writes()
//! # }
//! # }
//! ```
//!
//! Once you are more familiar with how system data and parallelization works,
//! you can take look at a more flexible and performant way to dispatch:
//! `ParSeq`. Using it is bit trickier, but it allows dispatching without any
//! virtual function calls.
#![deny(unused_must_use, clippy::disallowed_types)]
#![deny(unsafe_op_in_unsafe_fn)]
#![warn(missing_docs)]
/// Re-exports from [`atomic_refcell`]
///
/// Mainly for internals, most users don't need to interact with this.
pub mod cell {
pub use atomic_refcell::*;
}
mod dispatch;
mod meta;
mod system;
mod world;
/// A reexport of the `#[derive(SystemData]` macro provided by `shred-derive`.
/// This requires that the `shred-derive` feature is enabled.
#[cfg(feature = "shred-derive")]
pub use shred_derive::SystemData;
#[cfg(feature = "parallel")]
pub use crate::dispatch::AsyncDispatcher;
#[cfg(feature = "parallel")]
pub use crate::dispatch::{Par, ParSeq, RunWithPool, Seq};
pub use crate::{
dispatch::{
BatchAccessor, BatchController, BatchUncheckedWorld, Dispatcher, DispatcherBuilder,
MultiDispatchController, MultiDispatcher, SendDispatcher,
},
meta::{CastFrom, MetaIter, MetaIterMut, MetaTable},
system::{
Accessor, AccessorCow, DynamicSystemData, RunNow, RunningTime, StaticAccessor, System,
SystemData,
},
world::{
DefaultProvider, Entry, Fetch, FetchMut, PanicHandler, Read, ReadExpect, Resource,
ResourceId, SetupHandler, World, Write, WriteExpect,
},
};
/// Alias for `World` for easier migration to the new version. Will be removed
/// in the future.
#[deprecated(since = "0.8.0", note = "renamed to `World`")]
pub type Resources = World;