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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
//! The guards a scope's `include` hands back: a router chain plus the scope it drains into.
//!
//! There are two of them, because a registration has two ways to finish. One that is complete as
//! it stands commits when the guard drops at the end of the statement ([`Mounting`]), so the type
//! carries no `#[must_use]`: dropping it IS the registration. One carrying
//! [`Out`](crate::runtime::Out) slots has nothing to commit until every slot is bound, so its
//! terminal is a call ([`MountingSlots::build`]) and the type says so with `#[must_use]` - a
//! forgotten `.build()` is then a warning at the mount site rather than a panic at startup.
//!
//! A form with nothing of the handler's own to attach still gets a [`Mounting`] guard, because
//! every registration can bind the deferred-retry slot ([`Retry`](crate::runtime::Retry)), and
//! binding it opens a mount chain over the finished route so the slot steps follow.

use std::borrow::Cow;
use std::fmt;
use std::num::NonZeroU32;

use crate::Broker;

use crate::runtime::app::scope::BrokerScope;
use crate::runtime::middleware::BlanketLayer;
use crate::runtime::publish::{PublishIdentity, PublishPipeline};
use crate::runtime::retry::{
    Absent, CapOpen, DeadLetterOpen, DeclareCap, DeclareDeadLetter, DeclareMount, DestinationLast,
    DestinationOpen, Present, Retry, RetryOpen, RouteDeclaring, RoutePosition,
};
use crate::runtime::router::{
    MapPublisher, Router, RouterBroker, RouterCommit, RouterDef, RouterWith,
};
use crate::runtime::slot::{
    AdmitsAt, BatchTransformLast, CodecLast, MapPolicyLast, NamedStep, OutPosition, Reply,
    ReplyLast, ReplyStep, TransactionalLast, TransformLast,
};

/// A mount chain that can be drained into a scope's sink. Machinery; never named directly.
#[doc(hidden)]
pub trait ScopeCommit<B: Broker, Layers, C, State, Pipeline>: Sized {
    fn commit_into(self, scope: &mut BrokerScope<B, Layers, C, State, Pipeline>);
}

// A finished chain is a router: mounting it is the same operation `include_router` performs, so
// a scope registration and a router one reach the sink through one path.
impl<B, Layers, C, State, Pipeline, Routes, RC, RL, RP> ScopeCommit<B, Layers, C, State, Pipeline>
    for Router<B, Routes, RC, RL, RP>
where
    B: Broker + 'static,
    Routes: RouterDef<B, State, RP>,
    RL: BlanketLayer + Clone + Send + Sync + 'static,
    Layers: BlanketLayer + Clone + Send + Sync + 'static,
    Pipeline: PublishPipeline + Clone + Send + 'static,
    State: Send + Sync + 'static,
{
    fn commit_into(self, scope: &mut BrokerScope<B, Layers, C, State, Pipeline>) {
        // The chain's router carries the scope's own publish path, so the fallback here is never
        // the one a registration takes.
        RouterDef::mount(
            self,
            &scope.global,
            &scope.pipeline,
            &PublishIdentity,
            &mut scope.sink,
        );
    }
}

// An unfinished chain commits by running the router's own terminal first.
impl<B, Layers, C, State, Pipeline, Mount, R, Def, Attach, Last>
    ScopeCommit<B, Layers, C, State, Pipeline> for RouterWith<Mount, R, Def, Attach, Last>
where
    B: Broker + 'static,
    Attach: RouterCommit<Mount, R, Def>,
    Attach::Out: ScopeCommit<B, Layers, C, State, Pipeline>,
{
    fn commit_into(self, scope: &mut BrokerScope<B, Layers, C, State, Pipeline>) {
        // Not `build()`: see `RouterWith::commit_chain`, a scope's chain answers for its retry
        // destinations at startup rather than here.
        self.commit_chain().commit_into(scope);
    }
}

/// The guard [`BrokerScope::include`](crate::runtime::BrokerScope::include) hands back for a
/// registration that is complete as it stands: one router mount chain and the scope it drains
/// into.
///
/// Every step forwards to the chain underneath, so the vocabulary, the typestate and the
/// diagnostics are the router's. Dropping the guard at the end of the statement is what commits,
/// which is why the type is not `#[must_use]`: `b.include(respond).out_reply(Publish);` is a
/// whole registration. A registration whose [`Out`](crate::runtime::Out) slots have to be bound
/// first gets [`MountingSlots`] instead.
pub struct Mounting<'s, B, Layers, C, State, Pipeline, Chain>
where
    B: Broker + 'static,
    Chain: ScopeCommit<B, Layers, C, State, Pipeline>,
{
    // Options only so a step can move the pieces into the next state out of a Drop type; both
    // stay `Some` until the terminal or that replacement takes them.
    scope: Option<&'s mut BrokerScope<B, Layers, C, State, Pipeline>>,
    chain: Option<Chain>,
}

impl<'s, B, Layers, C, State, Pipeline, Chain> Mounting<'s, B, Layers, C, State, Pipeline, Chain>
where
    B: Broker + 'static,
    Chain: ScopeCommit<B, Layers, C, State, Pipeline>,
{
    pub(super) fn new(
        chain: Chain,
        scope: &'s mut BrokerScope<B, Layers, C, State, Pipeline>,
    ) -> Self {
        Self {
            scope: Some(scope),
            chain: Some(chain),
        }
    }

    /// Rebuilds the guard over a stepped chain: how every forwarded step reaches the chain.
    ///
    /// # Panics
    ///
    /// Never in practice: both pieces stay present until the terminal or a step takes them.
    fn map_chain<NewChain>(
        mut self,
        f: impl FnOnce(Chain) -> NewChain,
    ) -> Mounting<'s, B, Layers, C, State, Pipeline, NewChain>
    where
        NewChain: ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        let chain = self
            .chain
            .take()
            .expect("the guard holds its chain until the terminal or a step takes it");
        let scope = self
            .scope
            .take()
            .expect("the guard holds its scope until the terminal or a step takes it");
        Mounting::new(f(chain), scope)
    }
}

/// The guard over a stepped chain: what each forwarded step of [`Mounting`] returns.
type Stepped<'s, B, Layers, C, State, Pipeline, Mount, R, Def, Attach, Last> =
    Mounting<'s, B, Layers, C, State, Pipeline, RouterWith<Mount, R, Def, Attach, Last>>;

/// The chain one step of [`Mounting`] produces, named once so the bound below reads.
type SteppedChain<Mount, R, Def, Attach, Last> = RouterWith<Mount, R, Def, Attach, Last>;

impl<'s, B, Layers, C, State, Pipeline, Mount, R, Def, Attach, Last>
    Mounting<'s, B, Layers, C, State, Pipeline, RouterWith<Mount, R, Def, Attach, Last>>
where
    B: Broker + 'static,
    RouterWith<Mount, R, Def, Attach, Last>: ScopeCommit<B, Layers, C, State, Pipeline>,
{
    /// See [`RouterWith::out`]: names the publish policy of one position, the reply's
    /// ([`Reply`](crate::runtime::Reply)), one slot's, or the deferred retry's
    /// ([`Retry`](crate::runtime::Retry)).
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn out<M, Policy, Index>(
        self,
        marker: M,
        policy: Policy,
    ) -> Stepped<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <M as OutPosition<Mount, R::Broker, Attach, Policy, Index>>::Out,
        Index,
    >
    where
        R: RouterBroker,
        M: OutPosition<Mount, R::Broker, Attach, Policy, Index>,
        SteppedChain<
            Mount,
            R,
            Def,
            <M as OutPosition<Mount, R::Broker, Attach, Policy, Index>>::Out,
            Index,
        >: ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        self.map_chain(|chain| chain.out(marker, policy))
    }

    /// See [`RouterWith::out_reply`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn out_reply<Policy>(
        self,
        policy: Policy,
    ) -> Stepped<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Reply as OutPosition<Mount, R::Broker, Attach, Policy, ReplyLast>>::Out,
        ReplyLast,
    >
    where
        R: RouterBroker,
        Reply: OutPosition<Mount, R::Broker, Attach, Policy, ReplyLast>,
        SteppedChain<
            Mount,
            R,
            Def,
            <Reply as OutPosition<Mount, R::Broker, Attach, Policy, ReplyLast>>::Out,
            ReplyLast,
        >: ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        self.out(Reply, policy)
    }

    /// See [`RouterWith::out_retry`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn out_retry<Policy, Index>(
        self,
        policy: Policy,
    ) -> Stepped<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Retry as OutPosition<Mount, R::Broker, Attach, Policy, Index>>::Out,
        Index,
    >
    where
        R: RouterBroker,
        Retry: OutPosition<Mount, R::Broker, Attach, Policy, Index>,
        SteppedChain<
            Mount,
            R,
            Def,
            <Retry as OutPosition<Mount, R::Broker, Attach, Policy, Index>>::Out,
            Index,
        >: ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        self.out(Retry, policy)
    }

    /// See [`RouterWith::max_attempts`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn max_attempts(
        self,
        attempts: NonZeroU32,
    ) -> Stepped<'s, B, Layers, C, State, Pipeline, Mount, R, Def, <Attach as DeclareCap>::Out, Last>
    where
        Attach: DeclareCap<Step: CapOpen>,
        SteppedChain<Mount, R, Def, <Attach as DeclareCap>::Out, Last>:
            ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        self.map_chain(|chain| chain.max_attempts(attempts))
    }

    /// See [`RouterWith::dead_letter`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn dead_letter(
        self,
        destination: impl Into<Cow<'static, str>>,
    ) -> Stepped<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Attach as DeclareDeadLetter>::Out,
        Last,
    >
    where
        Attach: DeclareDeadLetter<Step: DeadLetterOpen>,
        SteppedChain<Mount, R, Def, <Attach as DeclareDeadLetter>::Out, Last>:
            ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        self.map_chain(|chain| chain.dead_letter(destination))
    }

    /// See [`RouterWith::to`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn to(
        self,
        destination: impl Into<Cow<'static, str>>,
    ) -> Stepped<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Attach as DestinationLast<Last>>::Out,
        Last,
    >
    where
        Attach: DestinationLast<Last, Step: DestinationOpen>,
        SteppedChain<Mount, R, Def, <Attach as DestinationLast<Last>>::Out, Last>:
            ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        self.map_chain(|chain| chain.to(destination))
    }

    /// See [`RouterWith::codec`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn codec<Cd>(
        self,
        codec: Cd,
    ) -> Stepped<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Attach as CodecLast<Cd, Last>>::Out,
        Last,
    >
    where
        Attach: CodecLast<Cd, Last, Step: NamedStep>,
        SteppedChain<Mount, R, Def, <Attach as CodecLast<Cd, Last>>::Out, Last>:
            ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        self.map_chain(|chain| chain.codec(codec))
    }

    /// See [`RouterWith::transform`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn transform<N>(
        self,
        transform: N,
    ) -> Stepped<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Attach as TransformLast<N, Last>>::Out,
        Last,
    >
    where
        R: RouterBroker<Broker = B>,
        Attach: TransformLast<N, Last, Step: NamedStep> + AdmitsAt<N, Last, Mount, Def, B>,
        SteppedChain<Mount, R, Def, <Attach as TransformLast<N, Last>>::Out, Last>:
            ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        self.map_chain(|chain| chain.transform(transform))
    }

    /// See [`RouterWith::batch_transform`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn batch_transform<N>(
        self,
        transform: N,
    ) -> Stepped<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Attach as BatchTransformLast<N, Last>>::Out,
        Last,
    >
    where
        Attach: BatchTransformLast<N, Last, Step: ReplyStep>,
        SteppedChain<Mount, R, Def, <Attach as BatchTransformLast<N, Last>>::Out, Last>:
            ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        self.map_chain(|chain| chain.batch_transform(transform))
    }

    /// See [`RouterWith::transactional`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn transactional(
        self,
    ) -> Stepped<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Attach as TransactionalLast<Last>>::Out,
        Last,
    >
    where
        Attach: TransactionalLast<Last, Step: ReplyStep>,
        SteppedChain<Mount, R, Def, <Attach as TransactionalLast<Last>>::Out, Last>:
            ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        self.map_chain(RouterWith::transactional)
    }
}

/// The chain a form with nothing to attach hands the guard: its registration, already grown into
/// a router of one.
type EagerChain<B, Head, Tail, RouteCodec, RouteLayers, RoutePipe> =
    Router<B, (Head, Tail), RouteCodec, RouteLayers, RoutePipe>;

// A form that attaches nothing has its registration finished the moment `include` returns, so its
// chain is a router rather than a `RouterWith`. The one position such a registration can still
// bind is the deferred retry, which belongs to the registration and not to a publish the handler
// makes - hence this impl, on the guard over a finished router.
impl<'s, B, Layers, C, State, Pipeline, Head, Tail, RouteCodec, RouteLayers, RoutePipe>
    Mounting<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        EagerChain<B, Head, Tail, RouteCodec, RouteLayers, RoutePipe>,
    >
where
    B: Broker + 'static,
    EagerChain<B, Head, Tail, RouteCodec, RouteLayers, RoutePipe>:
        ScopeCommit<B, Layers, C, State, Pipeline>,
{
    /// Names the publish policy of one position. A registration whose handler publishes nothing
    /// of its own has exactly one, the deferred retry: see [`Retry`](crate::runtime::Retry), and
    /// it binds once. The call opens a mount chain over the finished route, so the steps after it
    /// are the slot steps.
    // The unit marker drives inference, so it travels by value to keep the call site
    // `.out(Retry, ..)`, like every other position's.
    #[allow(clippy::needless_pass_by_value, clippy::type_complexity)]
    pub fn out<M, Policy>(
        self,
        marker: M,
        policy: Policy,
    ) -> Mounting<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        <M as RoutePosition<
            B,
            EagerChain<B, Head, Tail, RouteCodec, RouteLayers, RoutePipe>,
            Policy,
        >>::Out,
    >
    where
        M: RoutePosition<B, EagerChain<B, Head, Tail, RouteCodec, RouteLayers, RoutePipe>, Policy>,
        <M as RoutePosition<
            B,
            EagerChain<B, Head, Tail, RouteCodec, RouteLayers, RoutePipe>,
            Policy,
        >>::Out: ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        let _ = marker;
        self.map_chain(|chain| M::bind(chain, policy))
    }

    /// See [`RouterWith::max_attempts`]: the same declaration on a registration that was a
    /// finished route the moment `include` returned. It opens a mount chain, so the steps after
    /// it are the chain's.
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn max_attempts(
        self,
        attempts: NonZeroU32,
    ) -> Mounting<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        RouterWith<
            DeclareMount,
            EagerChain<B, Head, Tail, RouteCodec, RouteLayers, RoutePipe>,
            (),
            RouteDeclaring<Present, Absent>,
        >,
    >
    where
        Head: RetryOpen,
        RouterWith<
            DeclareMount,
            EagerChain<B, Head, Tail, RouteCodec, RouteLayers, RoutePipe>,
            (),
            RouteDeclaring<Present, Absent>,
        >: ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        self.map_chain(|chain| chain.max_attempts(attempts))
    }

    /// See [`RouterWith::dead_letter`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn dead_letter(
        self,
        destination: impl Into<Cow<'static, str>>,
    ) -> Mounting<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        RouterWith<
            DeclareMount,
            EagerChain<B, Head, Tail, RouteCodec, RouteLayers, RoutePipe>,
            (),
            RouteDeclaring<Absent, Present>,
        >,
    >
    where
        Head: RetryOpen,
        RouterWith<
            DeclareMount,
            EagerChain<B, Head, Tail, RouteCodec, RouteLayers, RoutePipe>,
            (),
            RouteDeclaring<Absent, Present>,
        >: ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        self.map_chain(|chain| chain.dead_letter(destination))
    }

    /// The same call with the marker spelled out: `.out(Retry, policy)`, and the same chain
    /// afterwards.
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn out_retry<Policy>(
        self,
        policy: Policy,
    ) -> Mounting<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        <Retry as RoutePosition<
            B,
            EagerChain<B, Head, Tail, RouteCodec, RouteLayers, RoutePipe>,
            Policy,
        >>::Out,
    >
    where
        Retry:
            RoutePosition<B, EagerChain<B, Head, Tail, RouteCodec, RouteLayers, RoutePipe>, Policy>,
        <Retry as RoutePosition<
            B,
            EagerChain<B, Head, Tail, RouteCodec, RouteLayers, RoutePipe>,
            Policy,
        >>::Out: ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        self.out(Retry, policy)
    }
}

// The broker's own publisher settings reach a scope registration exactly as they reach a router
// one: through the chain the guard forwards to. The replacement policy has the same type, so the
// chain the guard hands on is the one it already held.
impl<B, Layers, C, State, Pipeline, Mount, R, Def, Attach, Last> MapPublisher
    for Mounting<'_, B, Layers, C, State, Pipeline, RouterWith<Mount, R, Def, Attach, Last>>
where
    B: Broker + 'static,
    Attach: MapPolicyLast<Last, Step: NamedStep>,
    RouterWith<Mount, R, Def, Attach, Last>: ScopeCommit<B, Layers, C, State, Pipeline>,
{
    type Policy = Attach::Policy;

    fn map_publisher(self, f: impl FnOnce(Self::Policy) -> Self::Policy) -> Self {
        self.map_chain(|chain| chain.map_publisher(f))
    }
}

impl<B, Layers, C, State, Pipeline, Chain> fmt::Debug
    for Mounting<'_, B, Layers, C, State, Pipeline, Chain>
where
    B: Broker + 'static,
    Chain: ScopeCommit<B, Layers, C, State, Pipeline>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Mounting").finish_non_exhaustive()
    }
}

impl<B, Layers, C, State, Pipeline, Chain> Drop
    for Mounting<'_, B, Layers, C, State, Pipeline, Chain>
where
    B: Broker + 'static,
    Chain: ScopeCommit<B, Layers, C, State, Pipeline>,
{
    fn drop(&mut self) {
        if let (Some(chain), Some(scope)) = (self.chain.take(), self.scope.take()) {
            chain.commit_into(scope);
        }
    }
}

/// The guard a registration carrying [`Out`](crate::runtime::Out) slots gets.
///
/// It holds the same chain as [`Mounting`], finished by [`build`](Self::build) once every slot is
/// bound. A chain that still holds an unbound slot has nothing to commit, so this guard's
/// terminal is a call rather than a drop - and the type is `#[must_use]` for exactly that reason:
/// a chain dropped before `.build()` registered nothing, so the warning lands on the mount site
/// that forgot it. The drop of such a chain panics rather than starting a service whose handler
/// would never consume.
#[must_use = "a handler with Out slots is not mounted until `.build()` commits it: bind every \
              slot with `.out(marker, policy)`, then finish the chain with `.build()`"]
pub struct MountingSlots<'s, B, Layers, C, State, Pipeline, Chain>
where
    B: Broker + 'static,
{
    // See `Mounting`: the pieces move out of a Drop type one step at a time.
    scope: Option<&'s mut BrokerScope<B, Layers, C, State, Pipeline>>,
    chain: Option<Chain>,
}

impl<'s, B, Layers, C, State, Pipeline, Chain>
    MountingSlots<'s, B, Layers, C, State, Pipeline, Chain>
where
    B: Broker + 'static,
{
    pub(super) fn new(
        chain: Chain,
        scope: &'s mut BrokerScope<B, Layers, C, State, Pipeline>,
    ) -> Self {
        Self {
            scope: Some(scope),
            chain: Some(chain),
        }
    }

    /// The chain and the scope, moved out without running the terminal.
    ///
    /// # Panics
    ///
    /// Never in practice: both stay present until the terminal or a step takes them.
    fn take(mut self) -> (Chain, &'s mut BrokerScope<B, Layers, C, State, Pipeline>) {
        let chain = self
            .chain
            .take()
            .expect("the guard holds its chain until the terminal or a step takes it");
        let scope = self
            .scope
            .take()
            .expect("the guard holds its scope until the terminal or a step takes it");
        (chain, scope)
    }

    /// Rebuilds the guard over a stepped chain: how every forwarded step reaches the chain.
    ///
    /// # Panics
    ///
    /// Never in practice: see [`take`](Self::take).
    fn map_chain<NewChain>(
        self,
        f: impl FnOnce(Chain) -> NewChain,
    ) -> MountingSlots<'s, B, Layers, C, State, Pipeline, NewChain> {
        let (chain, scope) = self.take();
        MountingSlots::new(f(chain), scope)
    }

    /// Commits the registration. Exists only once every [`Out`](crate::runtime::Out) slot is
    /// bound: a chain that still has a `MissingSlot<..>` in its attachment fails to compile here,
    /// naming the slot.
    ///
    /// # Panics
    ///
    /// Never in practice: the internal expects guard invariants that hold until this terminal
    /// consumes them.
    pub fn build(self)
    where
        Chain: ScopeCommit<B, Layers, C, State, Pipeline>,
    {
        let (chain, scope) = self.take();
        chain.commit_into(scope);
    }
}

/// The guard over a stepped chain: what each forwarded step of [`MountingSlots`] returns.
type SteppedSlots<'s, B, Layers, C, State, Pipeline, Mount, R, Def, Attach, Last> =
    MountingSlots<'s, B, Layers, C, State, Pipeline, RouterWith<Mount, R, Def, Attach, Last>>;

impl<'s, B, Layers, C, State, Pipeline, Mount, R, Def, Attach, Last>
    MountingSlots<'s, B, Layers, C, State, Pipeline, RouterWith<Mount, R, Def, Attach, Last>>
where
    B: Broker + 'static,
{
    /// See [`RouterWith::out`]: names the publish policy of one position, the reply's
    /// ([`Reply`](crate::runtime::Reply)), one slot's, or the deferred retry's
    /// ([`Retry`](crate::runtime::Retry)).
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn out<M, Policy, Index>(
        self,
        marker: M,
        policy: Policy,
    ) -> SteppedSlots<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <M as OutPosition<Mount, R::Broker, Attach, Policy, Index>>::Out,
        Index,
    >
    where
        R: RouterBroker,
        M: OutPosition<Mount, R::Broker, Attach, Policy, Index>,
    {
        self.map_chain(|chain| chain.out(marker, policy))
    }

    /// See [`RouterWith::out_reply`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn out_reply<Policy>(
        self,
        policy: Policy,
    ) -> SteppedSlots<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Reply as OutPosition<Mount, R::Broker, Attach, Policy, ReplyLast>>::Out,
        ReplyLast,
    >
    where
        R: RouterBroker,
        Reply: OutPosition<Mount, R::Broker, Attach, Policy, ReplyLast>,
    {
        self.out(Reply, policy)
    }

    /// See [`RouterWith::out_retry`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn out_retry<Policy, Index>(
        self,
        policy: Policy,
    ) -> SteppedSlots<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Retry as OutPosition<Mount, R::Broker, Attach, Policy, Index>>::Out,
        Index,
    >
    where
        R: RouterBroker,
        Retry: OutPosition<Mount, R::Broker, Attach, Policy, Index>,
    {
        self.out(Retry, policy)
    }

    /// See [`RouterWith::max_attempts`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn max_attempts(
        self,
        attempts: NonZeroU32,
    ) -> SteppedSlots<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Attach as DeclareCap>::Out,
        Last,
    >
    where
        Attach: DeclareCap<Step: CapOpen>,
    {
        self.map_chain(|chain| chain.max_attempts(attempts))
    }

    /// See [`RouterWith::dead_letter`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn dead_letter(
        self,
        destination: impl Into<Cow<'static, str>>,
    ) -> SteppedSlots<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Attach as DeclareDeadLetter>::Out,
        Last,
    >
    where
        Attach: DeclareDeadLetter<Step: DeadLetterOpen>,
    {
        self.map_chain(|chain| chain.dead_letter(destination))
    }

    /// See [`RouterWith::to`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn to(
        self,
        destination: impl Into<Cow<'static, str>>,
    ) -> SteppedSlots<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Attach as DestinationLast<Last>>::Out,
        Last,
    >
    where
        Attach: DestinationLast<Last, Step: DestinationOpen>,
    {
        self.map_chain(|chain| chain.to(destination))
    }

    /// See [`RouterWith::codec`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn codec<Cd>(
        self,
        codec: Cd,
    ) -> SteppedSlots<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Attach as CodecLast<Cd, Last>>::Out,
        Last,
    >
    where
        Attach: CodecLast<Cd, Last, Step: NamedStep>,
    {
        self.map_chain(|chain| chain.codec(codec))
    }

    /// See [`RouterWith::transform`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn transform<N>(
        self,
        transform: N,
    ) -> SteppedSlots<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Attach as TransformLast<N, Last>>::Out,
        Last,
    >
    where
        R: RouterBroker<Broker = B>,
        Attach: TransformLast<N, Last, Step: NamedStep> + AdmitsAt<N, Last, Mount, Def, B>,
    {
        self.map_chain(|chain| chain.transform(transform))
    }

    /// See [`RouterWith::batch_transform`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn batch_transform<N>(
        self,
        transform: N,
    ) -> SteppedSlots<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Attach as BatchTransformLast<N, Last>>::Out,
        Last,
    >
    where
        Attach: BatchTransformLast<N, Last, Step: ReplyStep>,
    {
        self.map_chain(|chain| chain.batch_transform(transform))
    }

    /// See [`RouterWith::transactional`].
    #[allow(clippy::type_complexity)] // the chain's own state; an alias would hide the position
    pub fn transactional(
        self,
    ) -> SteppedSlots<
        's,
        B,
        Layers,
        C,
        State,
        Pipeline,
        Mount,
        R,
        Def,
        <Attach as TransactionalLast<Last>>::Out,
        Last,
    >
    where
        Attach: TransactionalLast<Last, Step: ReplyStep>,
    {
        self.map_chain(RouterWith::transactional)
    }
}

// See `Mounting`'s own impl: the settings hook is the chain's, forwarded.
impl<B, Layers, C, State, Pipeline, Mount, R, Def, Attach, Last> MapPublisher
    for MountingSlots<'_, B, Layers, C, State, Pipeline, RouterWith<Mount, R, Def, Attach, Last>>
where
    B: Broker + 'static,
    Attach: MapPolicyLast<Last, Step: NamedStep>,
{
    type Policy = Attach::Policy;

    fn map_publisher(self, f: impl FnOnce(Self::Policy) -> Self::Policy) -> Self {
        self.map_chain(|chain| chain.map_publisher(f))
    }
}

impl<B, Layers, C, State, Pipeline, Chain> fmt::Debug
    for MountingSlots<'_, B, Layers, C, State, Pipeline, Chain>
where
    B: Broker + 'static,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MountingSlots").finish_non_exhaustive()
    }
}

impl<B, Layers, C, State, Pipeline, Chain> Drop
    for MountingSlots<'_, B, Layers, C, State, Pipeline, Chain>
where
    B: Broker + 'static,
{
    fn drop(&mut self) {
        // The `must_use` warning is the diagnostic that belongs at the mount site; this is the
        // backstop for a mount that silenced or ignored it, like the on_startup ordering check.
        // A deliberately discarded incomplete registration must not vanish quietly - the handler
        // would never consume.
        assert!(
            self.chain.is_none(),
            "a handler with Out slots was included but never mounted: bind each slot with \
             .out(marker, policy) and finish the chain with .build()"
        );
    }
}