ruststream 0.7.0-rc.12

Async messaging framework for Rust: broker-agnostic traits, router, codecs, and a conformance harness for broker authors.
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
//! The deferred reply-publishing routes: encoded, byte-for-byte, and batch.
//!
//! Unlike [`SubscribeRoute`](super::routes::SubscribeRoute) these store the pieces of their
//! handler rather than a built one: the app's publish pipeline is only known at mount time and
//! the live reply publisher only exists once the broker connects, so mounting captures the
//! pieces in a starter that pairs the publisher, resolves the startup injections and builds the
//! handler at startup. A router-mounted publishing handler therefore picks up the app-wide
//! [`publish_layer`](crate::runtime::RustStream::publish_layer) chain.

use std::any::type_name;
use std::fmt;
use std::num::NonZeroUsize;
use std::sync::Arc;

use serde::Serialize;

use crate::codec::Codec;
use crate::{
    BatchSubscriber, Broker, BuildBatchContext, BuildContext, Connected, PublishPolicy, Publisher,
    SubscriptionSource,
};

use crate::runtime::batch_publishing::{
    BatchPublishingCall, BatchPublishingDef, BatchPublishingHandler,
};
use crate::runtime::dispatch::{Workers, spawn_dispatch_workers};
use crate::runtime::failure::{DispatchFailure, FailurePolicies};
use crate::runtime::inject::FromStartup;
use crate::runtime::input::DecodeWith;
use crate::runtime::lifecycle::BoxError;
use crate::runtime::metadata::{HandlerMetadata, PublishDescription};
use crate::runtime::middleware::BlanketLayer;
use crate::runtime::publish::{
    ForReply, NamesDestination, PublishPipeline, PublishTransform, ReplyPublisher, TypedPublisher,
};
use crate::runtime::publishing::{
    PublishingCall, PublishingDef, PublishingHandler, warn_ignored_reply_name,
};
use crate::runtime::redelivery::{CopyPathAddress, CopyPathPairing, RetrySetup, open_subscription};
use crate::runtime::retry::RetryOpen;
#[cfg(feature = "testing")]
use crate::testing::coordinator::paired;

use super::SourceMessage;
use super::routes::{
    MountRoute, RouteMeta, RouteMetadata, RouteSubscription, impl_destination_declared,
};
use super::sink::RouterSink;

/// What the reply position's transform stack does to the destination, projected against the
/// publisher it runs over: [`Names`](crate::runtime::Names) where a transform decides where each
/// reply goes, [`Reads`](crate::runtime::Reads) otherwise.
type ReplyUse<Transforms, Cx, Options> =
    <Transforms as PublishTransform<ForReply<Cx>, Options>>::Destination;

/// One reply-publishing registration whose reply travels the encoded wiring: the stack naming
/// the reply codec and transforms. An implementation detail of
/// [`Router`](crate::runtime::Router)'s registration list.
///
/// `Extra` is the include-site attachment the definition's startup injections resolve against:
/// the unit padding when the handler declares none beyond its reply, the bound slot sources when
/// it carries [`Out`](crate::runtime::Out) parameters.
#[doc(hidden)]
pub struct PublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra> {
    pub(super) source: Source,
    pub(super) def: Def,
    pub(super) codec: DecodeCodec,
    pub(super) publisher: ReplySource,
    pub(super) extra: Extra,
    pub(super) meta: HandlerMetadata,
    pub(super) policies: FailurePolicies,
    pub(super) workers: Workers,
}

/// One reply-publishing registration whose reply leaves byte-for-byte through a bare publisher
/// (a [`Serialized`](crate::runtime::Serialized) reply type). See [`PublishingRoute`]: the
/// app's publish pipeline is not known when the registration is made, so each route names the
/// wiring its replies travel.
#[doc(hidden)]
pub struct RawReplyRoute<Source, Def, DecodeCodec, ReplySource, Extra> {
    pub(super) source: Source,
    pub(super) def: Def,
    pub(super) codec: DecodeCodec,
    pub(super) publisher: ReplySource,
    pub(super) extra: Extra,
    pub(super) meta: HandlerMetadata,
    pub(super) policies: FailurePolicies,
    pub(super) workers: Workers,
}

/// One batch reply-publishing registration, deferred (see [`PublishingRoute`]). An
/// implementation detail of [`Router`](crate::runtime::Router)'s registration list.
#[doc(hidden)]
pub struct BatchPublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra> {
    pub(super) source: Source,
    pub(super) def: Def,
    pub(super) codec: DecodeCodec,
    pub(super) publisher: ReplySource,
    pub(super) extra: Extra,
    pub(super) meta: HandlerMetadata,
    pub(super) policies: FailurePolicies,
    pub(super) workers: Workers,
    /// The size the subscription opens its batches at, from the registration's `batch(n)`.
    pub(super) batch_size: NonZeroUsize,
}

/// Renders the deferred routes by the registration they carry: they hold no built handler to
/// print, so without the metadata a router's registration list would be anonymous.
macro_rules! debug_by_metadata {
    ($($route:ident<$($param:ident),+>),+ $(,)?) => {$(
        impl<$($param),+> fmt::Debug for $route<$($param),+> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.debug_struct(stringify!($route))
                    .field("meta", &self.meta)
                    .finish_non_exhaustive()
            }
        }

        impl<$($param),+> RouteMeta for $route<$($param),+> {
            fn collect(&self, out: &mut Vec<HandlerMetadata>) {
                out.push(self.meta.clone());
            }
        }
    )+};
}

debug_by_metadata!(
    PublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra>,
    RawReplyRoute<Source, Def, DecodeCodec, ReplySource, Extra>,
    BatchPublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra>,
);

/// Implements [`RetryOpen`] for the reply-publishing routes: none of them has bound the
/// deferred-retry position, which is what `.out(Retry, policy)` asks.
macro_rules! impl_retry_open {
    ($($route:ident<$($param:ident),+>),+ $(,)?) => {$(
        impl<$($param),+> RetryOpen for $route<$($param),+> {}
    )+};
}

impl_retry_open!(
    PublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra>,
    RawReplyRoute<Source, Def, DecodeCodec, ReplySource, Extra>,
    BatchPublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra>,
);

/// Implements [`RouteMetadata`] for the deferred reply-publishing routes: each carries its
/// metadata in a `meta` field, which is where a declaration writes its dead-letter channel.
macro_rules! impl_route_metadata {
    ($($route:ident<$($param:ident),+>),+ $(,)?) => {$(
        impl<$($param),+> RouteMetadata for $route<$($param),+> {
            fn metadata_mut(&mut self) -> &mut HandlerMetadata {
                &mut self.meta
            }
        }
    )+};
}

impl_route_metadata!(
    PublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra>,
    RawReplyRoute<Source, Def, DecodeCodec, ReplySource, Extra>,
    BatchPublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra>,
);

/// Reads the subscription's declarations off the deferred reply-publishing routes. The one-by-one
/// forms read their context from [`PublishingDef`], the batch one from [`BatchPublishingDef`].
macro_rules! impl_route_subscription {
    ($($route:ident<$($param:ident),+> via $def:ident),+ $(,)?) => {$(
        impl<B, $($param),+> RouteSubscription<B> for $route<$($param),+>
        where
            B: Broker,
            Source: SubscriptionSource<Connected<B>>,
            Def: $def,
        {
            type Source = Source;
            type Copies = <Source as SubscriptionSource<Connected<B>>>::Copies;
            type Context = <Def as $def>::Context;
        }
    )+};
}

impl_route_subscription!(
    PublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra> via PublishingDef,
    RawReplyRoute<Source, Def, DecodeCodec, ReplySource, Extra> via PublishingDef,
    BatchPublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra> via BatchPublishingDef,
);

impl_destination_declared!(
    [Source, Def, DecodeCodec, ReplySource, Extra]
        PublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra>,
    [Source, Def, DecodeCodec, ReplySource, Extra]
        RawReplyRoute<Source, Def, DecodeCodec, ReplySource, Extra>,
    [Source, Def, DecodeCodec, ReplySource, Extra]
        BatchPublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra>,
);

impl<
    B,
    Source,
    Def,
    DecodeCodec,
    ReplySource,
    Extra,
    State,
    Leaf,
    ReplyCodec,
    Transforms,
    RetryPipeline,
> MountRoute<B, State, RetryPipeline>
    for PublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra>
where
    B: Broker + 'static,
    // The subscription side: the source opens against the connected form, and the definition's
    // handler runs over the messages it yields.
    Source: SubscriptionSource<Connected<B>> + Send + 'static,
    Source::Copies:
        CopyPathPairing<B, Def::Context, RetryPipeline> + CopyPathAddress<Connected<B>, Source>,
    RetryPipeline: Clone,
    Source::Subscriber: Sync + Send + 'static,
    SourceMessage<B, Source>: Send + Sync + 'static,
    State: Send + Sync + 'static,
    Def: PublishingCall<State> + 'static,
    Def::Input: DecodeWith<DecodeCodec>,
    Def::Injections: FromStartup<B, Source::Subscriber, Extra> + Send + Sync + 'static,
    Def::Reply: crate::runtime::publishing::EncodeReply + Send + Sync + 'static,
    Def::Context: BuildContext<SourceMessage<B, Source>> + Send + Sync + 'static,
    // `DecodeWith` above carries whatever the input asks of the codec, and a byte input asks
    // for nothing - so this route mounts with `()` in a build with no codec feature.
    DecodeCodec: Send + Sync + 'static,
    Extra: Send + Sync + 'static,
    // The reply side: a policy paired at startup into an encoded typed stack. Naming the live
    // form structurally is what lets the route stay independent of the app's publish pipeline,
    // which it learns only here.
    ReplySource: PublishPolicy<Connected<B>, Live = TypedPublisher<Leaf, ReplyCodec, Transforms>>
        + Send
        + 'static,
    Leaf: Publisher + 'static,
    ReplyCodec: Codec + Send + Sync + 'static,
    Transforms: PublishTransform<ForReply<Def::Context>, Leaf::Options> + Send + Sync + 'static,
{
    type Context = Def::Context;

    fn mount_one<G, PP>(
        self,
        global: &G,
        pipeline: &PP,
        retry_pipeline: &RetryPipeline,
        sink: &mut RouterSink<B, State>,
        setup: RetrySetup<B, Def::Context>,
    ) where
        G: BlanketLayer + Clone + Send + Sync + 'static,
        PP: PublishPipeline + Clone + Send + 'static,
    {
        let Self {
            source,
            def,
            codec,
            publisher,
            extra,
            mut meta,
            policies,
            workers,
        } = self;
        // The apply-and-push tail: `BlanketLayer::apply` is an RPITIT whose hidden type cannot
        // be named, so the wrapped handler cannot leave the startup factory; apply and spawn
        // stay in one block instead.
        let global = global.clone();
        let pipeline = pipeline.clone();
        let name: Arc<str> = Arc::from(meta.name.as_ref());
        // The reply's own description: the mount chain fixed the codec and the transforms, so
        // what the document says about the reply channel is settled here, once.
        meta.describe_reply(|channel| {
            PublishDescription::of::<Connected<B>, ReplySource>(
                &publisher,
                Some(ReplyCodec::CONTENT_TYPE),
                <ReplyUse<Transforms, Def::Context, Leaf::Options> as NamesDestination>::NAMES,
                channel,
            )
        });
        let setup = setup.resolve::<Source::Copies, _>(retry_pipeline, &mut meta);
        sink.push_raw(
            Box::new(move |connected, state, scope, shutdown| {
                Box::pin(async move {
                    warn_ignored_reply_name(
                        &name,
                        type_name::<Def::Reply>(),
                        def.reply_name(),
                        def.ignored_reply_name(),
                    );
                    let pairing = publisher.pair(connected.as_ref());
                    #[cfg(feature = "testing")]
                    let (publisher, origin) = paired(connected.as_ref(), pairing).await;
                    #[cfg(not(feature = "testing"))]
                    let publisher = pairing.await;
                    let publisher = publisher.map_err(|e| Box::new(e) as BoxError)?;
                    let (subscriber, delivery) = open_subscription::<B, _, Def::Context>(
                        source,
                        connected.as_ref(),
                        &scope,
                        &name,
                        setup,
                    )
                    .await?;
                    let injections =
                        Def::Injections::resolve(extra, connected.as_ref(), &subscriber)
                            .await
                            .map_err(|e| Box::new(e) as BoxError)?;
                    let handler = global.apply::<SourceMessage<B, Source>, Def::Context, State, _>(
                        PublishingHandler {
                            def,
                            codec,
                            publisher,
                            pipeline,
                            injections,
                            decode: policies.decode,
                            #[cfg(feature = "testing")]
                            origin,
                        },
                    );
                    let failure = DispatchFailure::new(policies, shutdown.clone());
                    Ok(spawn_dispatch_workers(
                        subscriber,
                        Arc::new(handler),
                        shutdown,
                        name,
                        state,
                        delivery,
                        failure,
                        workers,
                    )?)
                })
            }),
            meta,
        );
    }
}

impl<B, Source, Def, DecodeCodec, ReplySource, Extra, State, Live, RetryPipeline>
    MountRoute<B, State, RetryPipeline>
    for RawReplyRoute<Source, Def, DecodeCodec, ReplySource, Extra>
where
    B: Broker + 'static,
    Source: SubscriptionSource<Connected<B>> + Send + 'static,
    Source::Copies:
        CopyPathPairing<B, Def::Context, RetryPipeline> + CopyPathAddress<Connected<B>, Source>,
    RetryPipeline: Clone,
    Source::Subscriber: Sync + Send + 'static,
    SourceMessage<B, Source>: Send + Sync + 'static,
    State: Send + Sync + 'static,
    Def: PublishingCall<State> + 'static,
    Def::Input: DecodeWith<DecodeCodec>,
    Def::Injections: FromStartup<B, Source::Subscriber, Extra> + Send + Sync + 'static,
    Def::Reply: crate::runtime::Serialized + Send + Sync + 'static,
    Def::Context: BuildContext<SourceMessage<B, Source>> + Send + Sync + 'static,
    // `DecodeWith` above carries whatever the input asks of the codec, and a byte input asks
    // for nothing - so this route mounts with `()` in a build with no codec feature.
    DecodeCodec: Send + Sync + 'static,
    Extra: Send + Sync + 'static,
    // The reply side: the policy pairs into a bare publisher, and the reply bytes go out as-is.
    ReplySource: PublishPolicy<Connected<B>, Live = Live> + Send + 'static,
    Live: Publisher + Send + Sync + 'static,
{
    type Context = Def::Context;

    fn mount_one<G, PP>(
        self,
        global: &G,
        pipeline: &PP,
        retry_pipeline: &RetryPipeline,
        sink: &mut RouterSink<B, State>,
        setup: RetrySetup<B, Def::Context>,
    ) where
        G: BlanketLayer + Clone + Send + Sync + 'static,
        PP: PublishPipeline + Clone + Send + 'static,
    {
        let Self {
            source,
            def,
            codec,
            publisher,
            extra,
            mut meta,
            policies,
            workers,
        } = self;
        // See the encoded route's tail for why apply and spawn stay in one block. The pipeline
        // travels along for shape, though a byte-for-byte reply never runs it.
        let global = global.clone();
        let pipeline = pipeline.clone();
        let name: Arc<str> = Arc::from(meta.name.as_ref());
        // A byte-for-byte reply carries its own wire format and no transform stack, so the
        // position names no media type and settles no destination of its own.
        meta.describe_reply(|channel| {
            PublishDescription::of::<Connected<B>, ReplySource>(&publisher, None, false, channel)
        });
        let setup = setup.resolve::<Source::Copies, _>(retry_pipeline, &mut meta);
        sink.push_raw(
            Box::new(move |connected, state, scope, shutdown| {
                Box::pin(async move {
                    warn_ignored_reply_name(
                        &name,
                        type_name::<Def::Reply>(),
                        def.reply_name(),
                        def.ignored_reply_name(),
                    );
                    let pairing = publisher.pair(connected.as_ref());
                    #[cfg(feature = "testing")]
                    let (publisher, origin) = paired(connected.as_ref(), pairing).await;
                    #[cfg(not(feature = "testing"))]
                    let publisher = pairing.await;
                    let publisher = publisher.map_err(|e| Box::new(e) as BoxError)?;
                    let (subscriber, delivery) = open_subscription::<B, _, Def::Context>(
                        source,
                        connected.as_ref(),
                        &scope,
                        &name,
                        setup,
                    )
                    .await?;
                    let injections =
                        Def::Injections::resolve(extra, connected.as_ref(), &subscriber)
                            .await
                            .map_err(|e| Box::new(e) as BoxError)?;
                    let handler = global.apply::<SourceMessage<B, Source>, Def::Context, State, _>(
                        PublishingHandler {
                            def,
                            codec,
                            publisher,
                            pipeline,
                            injections,
                            decode: policies.decode,
                            #[cfg(feature = "testing")]
                            origin,
                        },
                    );
                    let failure = DispatchFailure::new(policies, shutdown.clone());
                    Ok(spawn_dispatch_workers(
                        subscriber,
                        Arc::new(handler),
                        shutdown,
                        name,
                        state,
                        delivery,
                        failure,
                        workers,
                    )?)
                })
            }),
            meta,
        );
    }
}

impl<B, Source, Def, DecodeCodec, ReplySource, Extra, BatchReply, State, RetryPipeline>
    MountRoute<B, State, RetryPipeline>
    for BatchPublishingRoute<Source, Def, DecodeCodec, ReplySource, Extra>
where
    B: Broker + 'static,
    // The subscription side: batches open against the connected form.
    Source: SubscriptionSource<Connected<B>> + Send + 'static,
    Source::Copies:
        CopyPathPairing<B, Def::Context, RetryPipeline> + CopyPathAddress<Connected<B>, Source>,
    RetryPipeline: Clone,
    Source::Subscriber: BatchSubscriber + Sync + Send + 'static,
    SourceMessage<B, Source>: Send + 'static,
    State: Send + Sync + 'static,
    Def: BatchPublishingCall<State> + 'static,
    Def::Input: DecodeWith<DecodeCodec>,
    Def::Injections: FromStartup<B, Source::Subscriber, Extra> + Send + Sync + 'static,
    Def::Reply: Serialize + Send + Sync + 'static,
    Def::Context: BuildBatchContext<SourceMessage<B, Source>> + Send + Sync + 'static,
    DecodeCodec: Send + Sync + 'static,
    Extra: Send + Sync + 'static,
    // The reply side: the source pairs at startup into a batch reply wiring (plain or
    // transactional) that reads the batch's context while publishing each reply.
    ReplySource: PublishPolicy<Connected<B>, Live = BatchReply> + Send + 'static,
    BatchReply: ReplyPublisher<Def::Context> + 'static,
{
    type Context = Def::Context;

    fn mount_one<G, PP>(
        self,
        _global: &G,
        pipeline: &PP,
        retry_pipeline: &RetryPipeline,
        sink: &mut RouterSink<B, State>,
        setup: RetrySetup<B, Def::Context>,
    ) where
        G: BlanketLayer + Clone + Send + Sync + 'static,
        PP: PublishPipeline + Clone + Send + 'static,
    {
        // Batch handlers are not wrapped by the per-message global stack, but they do pick up the
        // app's publish pipeline for their replies, so this whole mount is shared with the scope
        // rather than split per surface.
        let pipeline = pipeline.clone();
        let Self {
            source,
            def,
            codec,
            publisher,
            extra,
            mut meta,
            policies,
            workers,
            batch_size,
        } = self;
        // A batch's replies answer many deliveries at once, so the position never offers a
        // transform the right to name where one goes; what it does fix is the codec.
        meta.describe_reply(|channel| {
            PublishDescription::of::<Connected<B>, ReplySource>(
                &publisher,
                Some(<BatchReply as ReplyPublisher<Def::Context>>::Codec::CONTENT_TYPE),
                false,
                channel,
            )
        });
        let setup = setup.resolve::<Source::Copies, _>(retry_pipeline, &mut meta);
        let subscription = meta.name.clone();
        sink.push_injected_batch::<_, _, _, _, Def::Context>(
            source,
            async move |connected: Arc<Connected<B>>, subscriber| {
                warn_ignored_reply_name(
                    &subscription,
                    type_name::<Def::Reply>(),
                    def.reply_name(),
                    def.ignored_reply_name(),
                );
                let pairing = publisher.pair(connected.as_ref());
                #[cfg(feature = "testing")]
                let (publisher, origin) = paired(connected.as_ref(), pairing).await;
                #[cfg(not(feature = "testing"))]
                let publisher = pairing.await;
                let publisher = publisher.map_err(|e| Box::new(e) as BoxError)?;
                let injections = Def::Injections::resolve(extra, connected.as_ref(), &subscriber)
                    .await
                    .map_err(|e| Box::new(e) as BoxError)?;
                let handler = BatchPublishingHandler {
                    def,
                    codec,
                    publisher,
                    pipeline,
                    injections,
                    decode: policies.decode,
                    #[cfg(feature = "testing")]
                    origin,
                };
                Ok((subscriber, handler))
            },
            meta,
            policies,
            workers,
            batch_size,
            setup,
        );
    }
}

#[cfg(all(test, feature = "memory", feature = "json"))]
mod tests {
    use super::*;
    use crate::Name;
    use crate::codec::JsonCodec;
    use crate::memory::MemoryPublish;

    /// The deferred routes hold no built handler to print, so their `Debug` has to identify the
    /// registration by its metadata; without that a router's registration list is anonymous.
    #[test]
    fn deferred_route_debug_names_the_registration() {
        let publishing = PublishingRoute {
            source: Name::new("orders"),
            def: (),
            codec: JsonCodec,
            publisher: TypedPublisher::with_codec(MemoryPublish, JsonCodec),
            extra: ((),),
            meta: HandlerMetadata::raw("orders"),
            policies: FailurePolicies::default(),
            workers: Workers::sequential(),
        };
        let rendered = format!("{publishing:?}");
        assert!(rendered.contains("PublishingRoute"), "{rendered}");
        assert!(rendered.contains("orders"), "{rendered}");

        let raw_reply = RawReplyRoute {
            source: Name::new("raw-orders"),
            def: (),
            codec: JsonCodec,
            publisher: MemoryPublish,
            extra: ((),),
            meta: HandlerMetadata::raw("raw-orders"),
            policies: FailurePolicies::default(),
            workers: Workers::sequential(),
        };
        let rendered = format!("{raw_reply:?}");
        assert!(rendered.contains("RawReplyRoute"), "{rendered}");
        assert!(rendered.contains("raw-orders"), "{rendered}");

        let batch_publishing = BatchPublishingRoute {
            source: Name::new("bulk-orders"),
            def: (),
            codec: JsonCodec,
            publisher: TypedPublisher::with_codec(MemoryPublish, JsonCodec),
            extra: ((),),
            meta: HandlerMetadata::raw("bulk-orders"),
            policies: FailurePolicies::default(),
            workers: Workers::sequential(),
            batch_size: crate::nonzero!(8),
        };
        let rendered = format!("{batch_publishing:?}");
        assert!(rendered.contains("BatchPublishingRoute"), "{rendered}");
        assert!(rendered.contains("bulk-orders"), "{rendered}");
    }
}