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
// This is free and unencumbered software released into the public domain.
//! The asynchronous execution interface shared by all program patterns.
//!
//! [`Execute`] separates running an already configured operation from selecting
//! its role, input, and options. Pattern traits in [`crate::programs`] extend
//! this interface without adding methods or prescribing an executor.
use Box;
use async_trait;
use Result;
/// An asynchronously executable operation yielding `T` or its associated [`Error`](Self::Error).
///
/// Inputs and configuration are supplied by the implementation, typically at
/// construction time. The mutable receiver permits execution to advance input
/// streams or update internal state; it does not promise that another call will
/// replay the same input. Implementations should document reuse, buffering,
/// cancellation, and the meaning of successful completion.
/// If `T` is a live stream, `Ok(T)` can mean successful startup rather than
/// completed execution; subsequent failures must be exposed by that result.
/// Each implementation chooses one error type for its result type `T`. Callers
/// can constrain it with `Execute<T, Error = E>` or use the corresponding
/// pattern trait's inherited associated type, such as `Emitter<T, Error = E>`.
///
/// This trait uses [`#[async_trait]`](macro@async_trait) with `Send` futures.
/// Implementors use the same attribute on their `impl`; each call returns a
/// boxed future whose lifetime is tied to the mutable receiver. The trait
/// itself does not require `Send` or `Sync` as supertraits, select an
/// asynchronous runtime, or require an operating-system process.
///
/// # Example
///
/// An in-process emitter can use the same interface as a process-backed one:
///
/// ```
/// use asimov_patterns::{Emitter, Execute};
/// use async_trait::async_trait;
/// use core::convert::Infallible;
///
/// struct EmptyEmitter;
///
/// #[async_trait]
/// impl Execute<Vec<u8>> for EmptyEmitter {
/// type Error = Infallible;
///
/// async fn execute(&mut self) -> Result<Vec<u8>, Self::Error> {
/// // An empty N-Triples document represents an empty RDF graph.
/// Ok(Vec::new())
/// }
/// }
///
/// impl Emitter<Vec<u8>> for EmptyEmitter {}
///
/// // Associated errors can be constrained on generic bounds and trait objects.
/// async fn emit(
/// emitter: &mut (impl Emitter<Vec<u8>, Error = Infallible> + ?Sized),
/// ) -> Result<Vec<u8>, Infallible> {
/// emitter.execute().await
/// }
///
/// let mut emitter = EmptyEmitter;
/// let emitter: &mut dyn Emitter<Vec<u8>, Error = Infallible> = &mut emitter;
/// let operation = emit(emitter);
/// ```