Skip to main content

behavior_actors/atomic/worker/
roster.rs

1use crate::atomic::OrderedRoles;
2
3use super::{PreparedWorker, WorkerSubmission};
4
5/// Complete custody when an initial worker is unavailable.
6pub struct InitialWorkerRejection<Factory, Role, Worker, Plan, Rejection> {
7    /// Original construction-only worker function.
8    pub factory: Factory,
9    /// Roles and worker submissions prepared before rejection.
10    pub prepared: Vec<PreparedWorker<Role, Worker, Plan>>,
11    /// Exact role whose worker was unavailable.
12    pub role: Role,
13    /// Typed reason returned by the worker function.
14    pub reason: Rejection,
15    /// Untouched declaration-order suffix.
16    pub remaining: Vec<Role>,
17}
18
19pub(in crate::atomic) fn prepare_initial_workers<Factory, Role, Worker, Plan, Rejection>(
20    mut factory: Factory,
21    roles: OrderedRoles<Role>,
22) -> Result<
23    Vec<PreparedWorker<Role, Worker, Plan>>,
24    InitialWorkerRejection<Factory, Role, Worker, Plan, Rejection>,
25>
26where
27    Factory: FnMut(&Role) -> Result<WorkerSubmission<Worker, Plan>, Rejection>,
28{
29    let mut remaining_roles = roles.into_roles().into_iter();
30    let mut prepared = Vec::new();
31    for role in remaining_roles.by_ref() {
32        match factory(&role) {
33            Ok(submission) => prepared.push(PreparedWorker { role, submission }),
34            Err(rejection) => {
35                return Err(InitialWorkerRejection {
36                    factory,
37                    prepared,
38                    role,
39                    reason: rejection,
40                    remaining: remaining_roles.collect(),
41                });
42            }
43        }
44    }
45    Ok(prepared)
46}