|
5 | 5 | //! defining such a handler requires a significant amount of boilerplate and can be error prone.
|
6 | 6 | //!
|
7 | 7 | //! This crate provides the [`composite_custom_message_handler`] macro for easily composing
|
8 |
| -//! pre-defined custom message handlers. The resulting handler can be further composed with other |
9 |
| -//! custom message handlers using the same macro. |
| 8 | +//! pre-defined custom message handlers into one handler. The resulting handler can be further |
| 9 | +//! composed with other custom message handlers using the same macro. |
10 | 10 | //!
|
11 | 11 | //! The following example demonstrates defining a `FooBarHandler` to compose separate handlers for
|
12 | 12 | //! `Foo` and `Bar` messages, and further composing it with a handler for `Baz` messages.
|
|
25 | 25 | //! use lightning::util::ser::Writeable;
|
26 | 26 | //! # use lightning::util::ser::Writer;
|
27 | 27 | //!
|
| 28 | +//! // Assume that `FooHandler` and `BarHandler` are defined in one crate and `BazHandler` is |
| 29 | +//! // defined in another crate, handling messages `Foo`, `Bar`, and `Baz`, respectively. |
| 30 | +//! |
28 | 31 | //! #[derive(Debug)]
|
29 | 32 | //! pub struct Foo;
|
30 | 33 | //!
|
|
146 | 149 | //! }
|
147 | 150 | //!
|
148 | 151 | //! # fn main() {
|
| 152 | +//! // The first crate may define a handler composing `FooHandler` and `BarHandler` and export the |
| 153 | +//! // corresponding message type ids as a macro to use in further composition. |
| 154 | +//! |
149 | 155 | //! composite_custom_message_handler!(
|
150 | 156 | //! pub struct FooBarHandler {
|
151 | 157 | //! foo: FooHandler,
|
|
158 | 164 | //! }
|
159 | 165 | //! );
|
160 | 166 | //!
|
| 167 | +//! #[macro_export] |
161 | 168 | //! macro_rules! foo_bar_type_ids {
|
162 | 169 | //! () => { foo_type_id!() | bar_type_id!() }
|
163 | 170 | //! }
|
164 | 171 | //!
|
| 172 | +//! // Another crate can then define a handler further composing `FooBarHandler` with `BazHandler` |
| 173 | +//! // and similarly export the composition of message type ids as a macro. |
| 174 | +//! |
165 | 175 | //! composite_custom_message_handler!(
|
166 | 176 | //! pub struct FooBarBazHandler {
|
167 | 177 | //! foo_bar: FooBarHandler,
|
|
173 | 183 | //! Baz(baz_type_id!()),
|
174 | 184 | //! }
|
175 | 185 | //! );
|
| 186 | +//! |
| 187 | +//! #[macro_export] |
| 188 | +//! macro_rules! foo_bar_baz_type_ids { |
| 189 | +//! () => { foo_bar_type_ids!() | baz_type_id!() } |
| 190 | +//! } |
176 | 191 | //! # }
|
177 | 192 | //!```
|
178 | 193 | //!
|
|
0 commit comments