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
//! Runtime for the code that `protoc-gen-protocontract-buffa` generates.
//!
//! For each protobuf `service`, the plugin emits:
//!
//! - `<Service>Sync`: each method returns `Result<Reply, Error>` when the
//! call is done. Streams are [`BoxIter`]s. The trait is dyn-compatible,
//! so an implementation chosen at run time is held as
//! `Arc<dyn GreeterSync>`.
//! - `<Service>Async`: each method returns `impl Future` of the same
//! result, so an implementation writes plain `async fn`. Streams are
//! [`Stream`]s. Generic callers (`impl GreeterAsync`) pay no
//! allocation.
//! - `Dyn<Service>Async`: a cloneable handle that holds any `<Service>Async`
//! implementation behind dynamic dispatch and implements the trait
//! itself. Each call through it boxes the future ([`BoxFuture`]) and the
//! reply stream ([`BoxStream`]).
//!
//! Every method takes its request by value and returns its reply owned; a
//! call in the same process is a plain method call, with no encoding and
//! no transport.
//!
//! Streaming methods, per form:
//!
//! | Method | `<Service>Sync` | `<Service>Async` |
//! | - | - | - |
//! | server streaming | `Result<BoxIter<'static, Result<Reply, Error>>, Error>` | `Result<impl Stream<Item = Result<Reply, Error>>, Error>` |
//! | client streaming | takes `BoxIter<'static, Result<Request, Error>>` | takes `impl Stream<Item = Result<Request, Error>>` |
//! | bidirectional | both | both |
//!
//! An `Err` before the first item fails the whole call; an `Err` item ends
//! a stream with that failure. Inbound items are `Result`s because a stream
//! that crosses a process boundary can break, and the implementation must
//! see that rather than a stream that merely ends early. No stream borrows
//! the service, so a stream can outlive the call and move between threads.
//!
//! # References to a service
//!
//! An empty message marked `option (protocontract.v1.service) =
//! "pkg.Service";` stands for an implementation of that service: the
//! plugin emits a type for it that a field, a request, or a reply carries,
//! holding the implementation in the form it was given. The option is
//! declared in `proto/protocontract/v1/options.proto`, which ships in this
//! crate. A reference does not survive encoding: taking a form of a
//! decoded one fails with [`RuntimeCode::Unbound`], and asking for an
//! async implementation in the blocking form where no tokio runtime can run
//! it fails with [`RuntimeCode::NoRuntime`]. The generated code uses the
//! hidden module `reference`; nothing there is named by hand.
//!
//! # Bridges
//!
//! The plugin also implements each form for wrappers around the other:
//!
//! | Have | Need | Wrapper | How |
//! | - | - | - | - |
//! | sync impl | async form | [`Inline`] | runs the sync call inside `poll`; for calls of microseconds |
//! | sync impl | async form | `Offload` (feature `tokio`) | runs the sync call on tokio's blocking pool |
//! | async impl | sync form | `Blocking` (feature `tokio`) | blocks the calling thread on a stored runtime handle |
//!
//! Each wrapper implements the form itself, so it fits in a
//! `Dyn<Service>Async` handle or an `Arc<dyn <Service>Sync>`.
//!
//! # Errors
//!
//! Every method fails with one type, [`Error`], whatever the service: an
//! implementation chosen at run time, a bridge, and a broken stream all
//! produce it, and `?` carries it across contracts. An `Error` holds a
//! [`Fault`] and the stack of operations it passed ([`Context`]).
//!
//! - **Code.** The class of the failure, which a caller branches on. Codes
//! are open: any [`ErrorCode`], which every protobuf enum is. A contract
//! declares the codes its callers may rely on as an `enum` next to its
//! `service`; [`RuntimeCode`] holds the runtime's own.
//! - **Retryable.** Whether the same call may succeed later; `false`
//! unless the fault says otherwise.
//! - **Message, source, backtrace.** `Display`, `std::error::Error::source`,
//! and [`Error::backtrace`].
//!
//! [`Failure`] is the ready-made fault; an implementation with more to say
//! implements [`Fault`] on a type of its own.
use Future;
use Pin;
pub use Blocking;
pub use ;
pub use ;
pub use Stream;
pub use Inline;
pub use Offload;
pub use ChannelStream;
pub use IterStream;
/// The future a call through a `Dyn<Service>Async` handle returns: boxed,
/// `Send`, and borrowing for at most `'a`.
///
/// ```
/// use protocontract::{BoxFuture, Error};
///
/// fn double(value: i64) -> BoxFuture<'static, Result<i64, Error>> {
/// Box::pin(async move { Ok(value * 2) })
/// }
/// # let _ = double(2);
/// ```
pub type BoxFuture<'a, T> = ;
/// A stream of a sync streaming method: a boxed `Send` iterator that
/// borrows for at most `'a`.
///
/// ```
/// use protocontract::{BoxIter, Error};
///
/// let replies: BoxIter<'static, Result<u32, Error>> = Box::new((1..=3).map(Ok));
/// assert_eq!(replies.count(), 3);
/// ```
pub type BoxIter<'a, T> = ;
/// A stream of an async streaming method called through a
/// `Dyn<Service>Async` handle: boxed, pinned, and `Send`.
pub type BoxStream<'a, T> = ;