Expand description
Shared resource dispatcher
This library allows to dispatch systems, which can have interdependencies, shared and exclusive resource access, in parallel.
Examples
extern crate shred;
#[macro_use]
extern crate shred_derive;
use shred::{DispatcherBuilder, Read, Resource, Resources, System, Write};
#[derive(Debug, Default)]
struct ResA;
#[derive(Debug, Default)]
struct ResB;
#[derive(SystemData)]
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);
}
}
fn main() {
let mut resources = Resources::new();
let mut dispatcher = DispatcherBuilder::new()
.with(EmptySystem, "empty", &[])
.build();
resources.insert(ResA);
resources.insert(ResB);
dispatcher.dispatch(&mut resources);
}
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.
Modules
Helper module for some internals, most users don’t need to interact with it.
Macros
The
par!
macro may be used to easily create a structure
which runs things in parallel.The
seq!
macro may be used to easily create a structure
which runs things sequentially.Structs
Like,
Dispatcher
but works asynchronously.A
SetupHandler
that simply uses the default implementation.The dispatcher struct, allowing
systems to be executed in parallel.
Builder for the
Dispatcher
.An entry to a resource of the
Resources
struct.
This is similar to the Entry API found in the standard library.Allows to fetch a resource in a system immutably.
Allows to fetch a resource in a system mutably.
An iterator for the
MetaTable
.A mutable iterator for the
MetaTable
.The
MetaTable
which allows to store object-safe trait implementations for resources.A setup handler that simply does nothing and thus will cause a panic on fetching.
The panic will provide the type name if the
nightly
feature of shred is enabled.Runs two tasks in parallel.
These two tasks are called
head
and tail
in the following documentation.A dispatcher intended to be used with
Par
and Seq
structures.Allows to fetch a resource in a system immutably.
The id of a
Resource
,
which is a tuple struct with a type
id and an additional resource id (represented with a usize
).A resource container, which provides methods to access to
the contained resources.
Runs two tasks sequentially.
These two tasks are called
head
and tail
in the following documentation.The static accessor that is used for
SystemData
.Allows to fetch a resource in a system mutably.
Enums
Either an
Accessor
of the system T
or a reference to it.Traits
A trait for accessing read/write multiple resources from a system. This can be used
to create dynamic systems that don’t specify what they fetch at compile-time.
Helper trait for the
MetaTable
.
This trait is required to be implemented for a trait to be compatible with the meta table.A struct implementing system data indicates that it bundles some resources which are required
for the execution.
A resource defines a set of data
which can only be accessed according
to Rust’s typical borrowing model (one writer xor multiple readers).
Trait for fetching data and running systems. Automatically implemented for systems.
Similar to
RunNow
except additionally taking in a rayon::ThreadPool
for parallelism.A setup handler performing the fetching of
T
.A static system data that can specify its dependencies at statically (at compile-time).
Most system data is a
SystemData
, the DynamicSystemData
type is only needed for very special
setups.Type Definitions
Allows to fetch a resource in a system immutably.
This will panic if the resource does not exist.
Usage of
Read
or Option<Read>
is therefore recommended.Allows to fetch a resource in a system mutably.
This will panic if the resource does not exist.
Usage of
Write
or Option<Write>
is therefore recommended.