some_executor 0.6.3

A trait for libraries that abstract over any executor
Documentation
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Dynamic executor implementations with type erasure support.
//!
//! This module provides implementations that enable trait objects (`Box<dyn SomeExecutor>`)
//! to function as executors themselves. This is crucial for storing executors in collections
//! or passing them around without knowing their concrete types at compile time.
//!
//! # Overview
//!
//! The type erasure pattern implemented here allows you to use `Box<dyn SomeExecutor>`
//! as if it were a concrete executor type. This is particularly useful when:
//!
//! - You need to store multiple executors of different types
//! - You want to pass executors across API boundaries without generic parameters
//! - You're building runtime-configurable executor selection
//!
//! # How It Works
//!
//! The module implements the executor traits for boxed trait objects, enabling them to:
//! 1. Accept typed futures and convert them to type-erased versions
//! 2. Spawn these type-erased futures onto the underlying executor
//! 3. Return observers that automatically downcast results back to the original type
//!
//! # Type Safety
//!
//! Despite the type erasure, the implementation maintains type safety through:
//! - Compile-time type checking when spawning tasks
//! - Runtime downcasting that panics on type mismatches (programming errors)
//! - Phantom types to track the original output type
//!
//! # Examples
//!
//! ## Using a boxed executor
//!
//! ```
//! use some_executor::{SomeExecutor, task::{Task, Configuration}};
//! use std::future::Future;
//! use std::convert::Infallible;
//!
//! # fn example() {
//! // Store any executor as a trait object
//! let mut boxed_executor: Box<dyn SomeExecutor<ExecutorNotifier = Infallible>> =
//!     todo!(); // Would come from a real executor implementation
//!
//! // Use it just like a concrete executor
//! let task = Task::without_notifications(
//!     "example".to_string(),
//!     Configuration::default(),
//!     async { 42 }
//! );
//! # // Can't actually spawn without a real executor
//! # // let observer = boxed_executor.spawn(task);
//! # }
//! ```
//!
//! ## Storing multiple executor types
//!
//! ```
//! use some_executor::SomeExecutor;
//! use std::convert::Infallible;
//!
//! struct ExecutorPool {
//!     executors: Vec<Box<dyn SomeExecutor<ExecutorNotifier = Infallible>>>,
//! }
//!
//! impl ExecutorPool {
//!     fn add_executor(&mut self, executor: Box<dyn SomeExecutor<ExecutorNotifier = Infallible>>) {
//!         self.executors.push(executor);
//!     }
//!     
//!     fn get_executor(&mut self) -> Option<&mut Box<dyn SomeExecutor<ExecutorNotifier = Infallible>>> {
//!         self.executors.first_mut()
//!     }
//! }
//! ```

use crate::dyn_observer::DowncastObserver;
use crate::observer::{
    ExecutorNotified, FinishedObservation, Observation, Observer, ObserverNotified,
};
use crate::task::{Task, TaskID};
use crate::{DynExecutor, DynStaticExecutor, SomeExecutor, SomeStaticExecutor};
use std::any::Any;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};

/// Observer that downcasts type-erased results back to their original type for static executors.
///
/// This is the non-Send version of [`DowncastObserver`], used with [`SomeStaticExecutor`]
/// implementations that work with `!Send` futures. It performs runtime downcasting of
/// `Box<dyn Any + 'static>` values back to the concrete type `V`.
///
/// # Type Parameters
///
/// - `O`: The underlying observer type that produces `Box<dyn Any + 'static>` values
/// - `V`: The concrete type to downcast to
///
/// # Panics
///
/// Methods will panic if downcasting fails, which indicates a type mismatch bug in the
/// executor implementation.
pub struct StaticDowncastObserver<O, V>(O, PhantomData<V>);

impl<O, V> Observer for StaticDowncastObserver<O, V>
where
    O: Observer<Value = Box<dyn Any + 'static>> + 'static,
    V: 'static,
{
    type Value = V;

    /// Observes the current state of the task and downcasts the result if ready.
    ///
    /// # Panics
    ///
    /// Panics if the task result is ready but cannot be downcast to type `V`.
    /// This indicates a type system violation in the executor implementation.
    fn observe(&self) -> Observation<Self::Value> {
        let inner = self.0.observe();
        match inner {
            Observation::Pending => Observation::Pending,
            Observation::Ready(e) => {
                let downcasted = e.downcast::<V>().expect("Downcast failed");
                Observation::Ready(*downcasted)
            }
            Observation::Done => Observation::Done,
            Observation::Cancelled => Observation::Cancelled,
        }
    }

    /// Returns the task ID of the underlying observer.
    fn task_id(&self) -> &TaskID {
        self.0.task_id()
    }
}

impl<O, V> StaticDowncastObserver<O, V> {
    /// Creates a new `StaticDowncastObserver` wrapping the given observer.
    ///
    /// # Arguments
    ///
    /// * `observer` - The underlying observer that produces type-erased values
    ///
    /// # Examples
    ///
    /// ```compile_fail
    /// // ALLOW_COMPILE_FAIL_DOCTEST: This shows the API but requires internal types
    /// // The StaticDowncastObserver is not exported publicly
    /// use some_executor::dyn_executor::StaticDowncastObserver;
    /// use some_executor::observer::{Observer, Observation};
    /// use std::any::Any;
    ///
    /// struct MockObserver;
    /// // Observer requires Future trait which MockObserver doesn't implement
    /// // This is just to show the API usage
    /// let mock_observer = /* ... */;
    /// let typed_observer: StaticDowncastObserver<_, i32> =
    ///     StaticDowncastObserver::new(mock_observer);
    /// ```
    pub fn new(observer: O) -> Self {
        Self(observer, PhantomData)
    }
}

impl<O, V> Future for StaticDowncastObserver<O, V>
where
    O: Future<Output = FinishedObservation<Box<dyn Any + 'static>>> + 'static,
    V: 'static,
{
    type Output = FinishedObservation<V>;

    /// Polls the underlying future and downcasts the result if ready.
    ///
    /// # Panics
    ///
    /// Panics if the future completes with a value that cannot be downcast to type `V`.
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let f = unsafe { self.map_unchecked_mut(|s| &mut s.0) }.poll(cx);
        match f {
            Poll::Pending => Poll::Pending,
            Poll::Ready(e) => match e {
                FinishedObservation::Cancelled => Poll::Ready(FinishedObservation::Cancelled),
                FinishedObservation::Ready(e) => {
                    let downcasted = e.downcast::<V>().expect("Downcast failed");
                    Poll::Ready(FinishedObservation::Ready(*downcasted))
                }
            },
        }
    }
}

/// Implementation of `SomeExecutor` for boxed executor trait objects.
///
/// This implementation enables `Box<dyn SomeExecutor>` to act as an executor itself,
/// providing the crucial type erasure pattern that allows executors to be stored and
/// used polymorphically.
///
/// # How It Works
///
/// When you spawn a task on a boxed executor:
/// 1. The concrete future type is converted to a type-erased version using `into_objsafe()`
/// 2. The type-erased task is spawned on the underlying executor
/// 3. A `DowncastObserver` is returned that will convert results back to the original type
///
/// # Examples
///
/// ```
/// # use some_executor::{SomeExecutor, task::{Task, Configuration}};
/// # use std::convert::Infallible;
/// # fn example() {
/// let mut executor: Box<dyn SomeExecutor<ExecutorNotifier = Infallible>> = todo!();
///
/// // Spawn a typed future
/// let task = Task::without_notifications(
///     "hello-task".to_string(),
///     Configuration::default(),
///     async { "hello".to_string() }
/// );
/// # // Would work with a real executor:
/// # // let observer = executor.spawn(task);
/// # // assert_eq!(observer.await.unwrap(), "hello");
/// # }
/// ```
impl<UnderlyingNotifier: ExecutorNotified + Send> SomeExecutor
    for Box<dyn SomeExecutor<ExecutorNotifier = UnderlyingNotifier>>
{
    type ExecutorNotifier = Box<dyn ExecutorNotified + Send>;

    /// Spawns a future onto the underlying executor with automatic type erasure and recovery.
    ///
    /// This method handles the type erasure process transparently, converting the typed
    /// future into a type-erased version for the underlying executor, then wrapping the
    /// returned observer to restore type information.
    fn spawn<F: Future + Send + 'static, Notifier: ObserverNotified<F::Output> + Send>(
        &mut self,
        task: Task<F, Notifier>,
    ) -> impl Observer<Value = F::Output>
    where
        Self: Sized,
        F::Output: Send + Unpin,
    {
        let underlying = self.as_mut();
        let objsafe = task.into_objsafe();
        let observer = underlying.spawn_objsafe(objsafe);
        // Restore the concrete type through downcasting
        let downcasted: DowncastObserver<_, F::Output> = DowncastObserver::new(observer);
        downcasted
    }

    /// Asynchronously spawns a future onto the underlying executor.
    ///
    /// Similar to `spawn`, but allows the spawning process itself to be asynchronous.
    async fn spawn_async<F: Future + Send + 'static, Notifier: ObserverNotified<F::Output> + Send>(
        &mut self,
        task: Task<F, Notifier>,
    ) -> impl Observer<Value = F::Output>
    where
        Self: Sized,
        F::Output: Send + Unpin,
    {
        let underlying = self.as_mut();
        let objsafe = task.into_objsafe();
        let observer = underlying.spawn_objsafe(objsafe);
        // Restore the concrete type through downcasting
        let downcasted: DowncastObserver<_, F::Output> = DowncastObserver::new(observer);
        downcasted
    }

    /// Spawns an already type-erased task.
    ///
    /// This is the object-safe method that works with type-erased futures directly.
    /// User code typically doesn't call this; it's used internally by the typed spawn methods.
    fn spawn_objsafe(
        &mut self,
        task: Task<
            Pin<Box<dyn Future<Output = Box<dyn Any + 'static + Send>> + 'static + Send>>,
            Box<dyn ObserverNotified<dyn Any + Send> + Send>,
        >,
    ) -> Box<
        dyn Observer<Value = Box<dyn Any + Send>, Output = FinishedObservation<Box<dyn Any + Send>>>
            + Send,
    > {
        self.as_mut().spawn_objsafe(task)
    }

    /// Asynchronously spawns an already type-erased task.
    ///
    /// The async version of `spawn_objsafe` for executors that need async initialization.
    fn spawn_objsafe_async<'s>(
        &'s mut self,
        task: Task<
            Pin<Box<dyn Future<Output = Box<dyn Any + 'static + Send>> + 'static + Send>>,
            Box<dyn ObserverNotified<dyn Any + Send> + Send>,
        >,
    ) -> Box<
        dyn Future<
                Output = Box<
                    dyn Observer<
                            Value = Box<dyn Any + Send>,
                            Output = FinishedObservation<Box<dyn Any + Send>>,
                        > + Send,
                >,
            > + 's,
    > {
        self.as_mut().spawn_objsafe_async(task)
    }

    /// Creates a boxed clone of the executor.
    ///
    /// This allows the boxed executor to be cloned while preserving the type erasure.
    fn clone_box(&self) -> Box<DynExecutor> {
        self.as_ref().clone_box()
    }

    /// Gets a notifier for executor-level events, if supported.
    ///
    /// Returns a type-erased notifier that can be used to wake the executor.
    fn executor_notifier(&mut self) -> Option<Self::ExecutorNotifier> {
        let underlying = self.as_mut().executor_notifier();
        underlying.map(|u| Box::new(u) as Box<dyn ExecutorNotified + Send>)
    }
}

/// Implementation of `SomeStaticExecutor` for boxed static executor trait objects.
///
/// This implementation enables `Box<dyn SomeStaticExecutor>` to act as a static executor,
/// supporting `!Send` futures through type erasure. This is particularly useful for
/// single-threaded or local executors.
///
/// # Differences from `SomeExecutor`
///
/// Unlike the `SomeExecutor` implementation, this works with futures that are not `Send`,
/// making it suitable for JavaScript/WASM environments or thread-local execution contexts.
///
/// # Examples
///
/// ```
/// # use some_executor::{SomeStaticExecutor, task::{Task, Configuration}};
/// # use std::convert::Infallible;
/// # use std::rc::Rc;
/// # fn example() {
/// let mut executor: Box<dyn SomeStaticExecutor<ExecutorNotifier = Infallible>> = todo!();
///
/// // Can spawn !Send futures
/// let rc = Rc::new(42);
/// let task = Task::without_notifications(
///     "rc-task".to_string(),
///     Configuration::default(),
///     async move { *rc }
/// );
/// # // Would work with a real executor:
/// # // let observer = executor.spawn_static(task);
/// # }
/// ```
impl<UnderlyingNotifier: ExecutorNotified> SomeStaticExecutor
    for Box<dyn SomeStaticExecutor<ExecutorNotifier = UnderlyingNotifier>>
{
    type ExecutorNotifier = Box<dyn ExecutorNotified>;

    /// Spawns a static (potentially !Send) future onto the underlying executor.
    ///
    /// This method supports futures that don't implement `Send`, making it suitable
    /// for single-threaded environments.
    fn spawn_static<F, Notifier: ObserverNotified<F::Output>>(
        &mut self,
        task: Task<F, Notifier>,
    ) -> impl Observer<Value = F::Output>
    where
        Self: Sized,
        F: Future + 'static,
        F::Output: 'static + Unpin,
    {
        let underlying = self.as_mut();
        let objsafe = task.into_objsafe_static();
        let observer = underlying.spawn_static_objsafe(objsafe);
        // Use StaticDowncastObserver for non-Send type recovery
        StaticDowncastObserver::new(observer)
    }

    /// Asynchronously spawns a static future onto the underlying executor.
    ///
    /// The async version of `spawn_static` for executors that need async initialization.
    async fn spawn_static_async<F, Notifier: ObserverNotified<F::Output>>(
        &mut self,
        task: Task<F, Notifier>,
    ) -> impl Observer<Value = F::Output>
    where
        Self: Sized,
        F: Future + 'static,
        F::Output: 'static + Unpin,
    {
        let underlying = self.as_mut();
        let objsafe = task.into_objsafe_static();
        let observer = underlying.spawn_static_objsafe(objsafe);
        StaticDowncastObserver::new(observer)
    }

    /// Spawns an already type-erased static task.
    ///
    /// This is the object-safe method for static executors. User code typically
    /// doesn't call this directly.
    fn spawn_static_objsafe(
        &mut self,
        task: crate::ObjSafeStaticTask,
    ) -> crate::BoxedStaticObserver {
        self.as_mut().spawn_static_objsafe(task)
    }

    /// Asynchronously spawns an already type-erased static task.
    ///
    /// The async version of `spawn_static_objsafe`.
    fn spawn_static_objsafe_async<'s>(
        &'s mut self,
        task: crate::ObjSafeStaticTask,
    ) -> crate::BoxedStaticObserverFuture<'s> {
        self.as_mut().spawn_static_objsafe_async(task)
    }

    /// Creates a boxed clone of the static executor.
    fn clone_box(&self) -> Box<DynStaticExecutor> {
        self.as_ref().clone_box()
    }

    /// Gets a notifier for executor-level events, if supported.
    fn executor_notifier(&mut self) -> Option<Self::ExecutorNotifier> {
        let underlying = self.as_mut().executor_notifier();
        underlying.map(|u| Box::new(u) as Box<dyn ExecutorNotified>)
    }
}

#[cfg(test)]
mod tests {
    use crate::{Infallible, SomeExecutor};

    #[cfg(target_arch = "wasm32")]
    use wasm_bindgen_test::*;

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
    #[cfg_attr(not(target_arch = "wasm32"), test)]
    fn test_dyn_executor() {
        //mt2-697
        #[allow(dead_code)]
        fn just_compile() {
            let _ty: Box<dyn SomeExecutor<ExecutorNotifier = Infallible>> = todo!();
            #[allow(unreachable_code, unused_variables)]
            fn expect_executor<E: SomeExecutor>(e: E) {}
            #[allow(unreachable_code)]
            expect_executor(_ty);
        }
    }
}