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
use *;
/// Messages passed to [`MessageBuilder::message`] are converted into a buffer through this
/// trait.
///
/// Any type that implements [`Into<Bytes<'static>>`](Bytes<'static>) inherently implements this
/// trait with [`Error = !`](core::convert::Infallible).
///
/// # Implementing [`IntoPayload`] for fun and profit
/// A common usage for [`IntoPayload`] is to implement it for a wrapper type that selects a
/// serialization method.
///
/// For example, here's how to leverage this in a system that uses both JSON and CBOR:
/// ```
/// # use dittolive_ditto::experimental::bus::{IntoPayload, Payload};
/// pub struct Cbor<'a, T>(pub &'a T);
/// impl<T: serde::Serialize> IntoPayload for Cbor<'_, T> {
/// type Error = serde_cbor::Error;
/// fn into_payload(self) -> Result<Payload, Self::Error> {
/// Ok(serde_cbor::to_vec(self.0)?.into())
/// }
/// }
///
/// pub struct Json<'a, T>(pub &'a T);
/// impl<T: serde::Serialize> IntoPayload for Json<'_, T> {
/// type Error = serde_json::Error;
/// fn into_payload(self) -> Result<Payload, Self::Error> {
/// Ok(serde_json::to_vec(self.0)?.into())
/// }
/// }
/// ```
///
/// Letting you then use both of these serializations in streams:
/// ```
/// # use dittolive_ditto::experimental::bus::{IntoPayload, Payload, Stream};
/// # pub struct Cbor<'a, T>(pub &'a T);
/// # impl<T: serde::Serialize> IntoPayload for Cbor<'_, T> {
/// # type Error = serde_cbor::Error;
/// # fn into_payload(self) -> Result<Payload, Self::Error> {
/// # Ok(serde_cbor::to_vec(self.0)?.into())
/// # }
/// # }
/// # pub struct Json<'a, T>(pub &'a T);
/// # impl<T: serde::Serialize> IntoPayload for Json<'_, T> {
/// # type Error = serde_json::Error;
/// # fn into_payload(self) -> Result<Payload, Self::Error> {
/// # Ok(serde_json::to_vec(self.0)?.into())
/// # }
/// # }
/// # async fn usage(json_stream: Stream<()>, cbor_stream: Stream<()>) {
/// let mut payload = vec!["Hello", "there"];
/// cbor_stream.message(Cbor(&payload)).try_send().unwrap();
/// json_stream.message(Json(&payload)).try_send().unwrap();
/// # }
/// ```
/// Wrap a reference to a [serializable](Serialize) value with this to use [CBOR](https://en.wikipedia.org/wiki/CBOR)
/// serialization to convert it into a [`Bytes`] buffer.
///
/// ```no_run
/// # use {dittolive_ditto::experimental::bus::{Stream, Cbor}};
/// # async fn usage(stream: Stream<()>) {
/// let mut payload = vec!["Hello", "there"];
/// stream.message(Cbor(&payload)).try_send().unwrap();
/// # }
/// ```
;
/// Wrap a reference to a [serializable](Serialize) value with this to use [JSON](https://en.wikipedia.org/wiki/JSON)
/// serialization to convert it into a [`Bytes`] buffer.
///
/// ```no_run
/// # use {dittolive_ditto::experimental::bus::{Stream, Json}};
/// # async fn usage(stream: Stream<()>) {
/// let mut payload = vec!["General", "Kenobi!"];
/// stream.message(Json(&payload)).try_send().unwrap();
/// # }
/// ```
;