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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Provides the ability to store `Systems`, `Bundles`, `Barriers`, in a normal vector for deferred dispatcher construction.

use std::marker::PhantomData;

use derivative::Derivative;

use amethyst_error::Error;

use crate::{
    ecs::prelude::{DispatcherBuilder, RunNow, System, World},
    RunNowDesc, SystemBundle, SystemDesc,
};

/// Trait to capture deferred dispatcher builder operations.
pub trait DispatcherOperation<'a, 'b> {
    /// Executes the dispatcher builder instruction.
    fn exec(
        self: Box<Self>,
        world: &mut World,
        dispatcher_builder: &mut DispatcherBuilder<'a, 'b>,
    ) -> Result<(), Error>;
}

/// Deferred operation Add Barrier
#[derive(Debug)]
pub struct AddBarrier;

impl<'a, 'b> DispatcherOperation<'a, 'b> for AddBarrier {
    fn exec(
        self: Box<Self>,
        _world: &mut World,
        dispatcher_builder: &mut DispatcherBuilder<'a, 'b>,
    ) -> Result<(), Error> {
        dispatcher_builder.add_barrier();
        Ok(())
    }
}

/// Deferred operation Add System
#[derive(Derivative)]
#[derivative(Debug)]
pub struct AddSystem<S> {
    /// System object
    #[derivative(Debug = "ignore")]
    pub system: S,
    /// System name
    pub name: String,
    /// System dependencies list
    pub dependencies: Vec<String>,
}

impl<'a, 'b, S> DispatcherOperation<'a, 'b> for AddSystem<S>
where
    S: for<'s> System<'s> + Send + 'a,
{
    fn exec(
        self: Box<Self>,
        _world: &mut World,
        dispatcher_builder: &mut DispatcherBuilder<'a, 'b>,
    ) -> Result<(), Error> {
        let dependencies = self
            .dependencies
            .iter()
            .map(String::as_str)
            .collect::<Vec<&str>>();
        dispatcher_builder.add(self.system, &self.name, &dependencies);
        Ok(())
    }
}

/// Deferred operation Add System Desc
#[derive(Derivative)]
#[derivative(Debug)]
pub struct AddSystemDesc<SD, S> {
    /// System description
    #[derivative(Debug = "ignore")]
    pub system_desc: SD,
    /// System name
    pub name: String,
    /// System dependencies
    pub dependencies: Vec<String>,
    /// Generic type holder
    pub marker: PhantomData<S>,
}

impl<'a, 'b, SD, S> DispatcherOperation<'a, 'b> for AddSystemDesc<SD, S>
where
    SD: SystemDesc<'a, 'b, S>,
    S: for<'s> System<'s> + Send + 'a,
{
    fn exec(
        self: Box<Self>,
        world: &mut World,
        dispatcher_builder: &mut DispatcherBuilder<'a, 'b>,
    ) -> Result<(), Error> {
        let system = self.system_desc.build(world);
        let dependencies = self
            .dependencies
            .iter()
            .map(String::as_str)
            .collect::<Vec<&str>>();
        dispatcher_builder.add(system, &self.name, &dependencies);
        Ok(())
    }
}

/// Deferred operation Add Thread Local
#[derive(Derivative)]
#[derivative(Debug)]
pub struct AddThreadLocal<S> {
    /// System object
    #[derivative(Debug = "ignore")]
    pub system: S,
}

impl<'a, 'b, S> DispatcherOperation<'a, 'b> for AddThreadLocal<S>
where
    S: for<'c> RunNow<'c> + 'b,
{
    fn exec(
        self: Box<Self>,
        _world: &mut World,
        dispatcher_builder: &mut DispatcherBuilder<'a, 'b>,
    ) -> Result<(), Error> {
        dispatcher_builder.add_thread_local(self.system);
        Ok(())
    }
}

/// Deferred operation Add Thread Local Desc
#[derive(Derivative)]
#[derivative(Debug)]
pub struct AddThreadLocalDesc<SD, S> {
    /// System description
    #[derivative(Debug = "ignore")]
    pub system_desc: SD,
    /// Generic type holder
    pub marker: PhantomData<S>,
}

impl<'a, 'b, SD, S> DispatcherOperation<'a, 'b> for AddThreadLocalDesc<SD, S>
where
    SD: RunNowDesc<'a, 'b, S>,
    S: for<'c> RunNow<'c> + 'b,
{
    fn exec(
        self: Box<Self>,
        world: &mut World,
        dispatcher_builder: &mut DispatcherBuilder<'a, 'b>,
    ) -> Result<(), Error> {
        let system = self.system_desc.build(world);
        dispatcher_builder.add_thread_local(system);
        Ok(())
    }
}

/// Deferred operation Add Bundle
#[derive(Derivative)]
#[derivative(Debug)]
pub struct AddBundle<B> {
    /// Bundle object
    #[derivative(Debug = "ignore")]
    pub bundle: B,
}

impl<'a, 'b, B> DispatcherOperation<'a, 'b> for AddBundle<B>
where
    B: SystemBundle<'a, 'b>,
{
    fn exec(
        self: Box<Self>,
        world: &mut World,
        dispatcher_builder: &mut DispatcherBuilder<'a, 'b>,
    ) -> Result<(), Error> {
        self.bundle.build(world, dispatcher_builder)?;
        Ok(())
    }
}