bombay-behavior 0.21.1

Pure, statically typed actor behavior primitives
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
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
//! Protocol-indexed actor recipients and deliveries.

use core::marker::PhantomData;

use crate::{ActionItem, Behavior, MessageProtocol, Never, Protocol};

/// A pure logical actor-address namespace.
///
/// An address names a transport or resolution domain. It does not allocate an
/// actor and it does not prove that an actor is installed. Fresh allocation is
/// interpreter-owned; the creator-local [`Address::Nonce`] is correlation
/// evidence only and is deliberately not convertible into an address here.
pub trait Address: Copy + Eq {
    type Nonce: Copy + Eq;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MailAddr(pub u64);

impl From<u64> for MailAddr {
    fn from(value: u64) -> Self {
        Self(value)
    }
}

impl From<MailAddr> for u64 {
    fn from(value: MailAddr) -> Self {
        value.0
    }
}

impl Address for MailAddr {
    type Nonce = u64;
}

/// Runtime-owned exact message endpoint family for one logical address namespace.
///
/// A runtime implements this trait on its own address newtype, selecting one
/// statically projected endpoint representation for each concrete protocol.
/// The projection may reuse a representation; [`EstablishedRecipient<P>`]
/// still preserves `P` and prevents cross-protocol substitution. Ordinary
/// protocols continue to declare only their canonical [`Protocol::Addr`] and
/// [`Protocol::Msg`]; they never author endpoint keys or endpoint associated
/// types. An endpoint is cloneable acquaintance evidence, but it is not
/// intrinsically `Send`: thread transfer is required only by the concrete
/// asynchronous interpretation boundary that performs it.
///
/// A local endpoint remains valid, but cannot enter the sendable
/// [`crate::InterpretSends`] path:
///
/// ```compile_fail,E0277
/// #[derive(Clone, Copy, PartialEq, Eq)]
/// struct LocalAddr(u8);
/// impl behavior::Address for LocalAddr { type Nonce = u8; }
/// struct LocalEndpoint<P>(std::rc::Rc<()>, core::marker::PhantomData<fn() -> P>);
/// impl<P> Clone for LocalEndpoint<P> {
///     fn clone(&self) -> Self { Self(self.0.clone(), core::marker::PhantomData) }
/// }
/// impl behavior::RecipientAddress for LocalAddr {
///     type Established<P> = LocalEndpoint<P> where P: behavior::Protocol<Addr = Self>;
/// }
/// struct LocalProtocol;
/// impl behavior::Protocol for LocalProtocol {
///     type Addr = LocalAddr;
///     type Msg = std::rc::Rc<()>;
/// }
/// struct Runtime;
/// impl<RootEvent, Path> behavior::InterpretItem<behavior::EstablishedDelivery<LocalProtocol>, RootEvent, Path>
///     for Runtime
/// {
///     fn interpret_item(
///         &mut self,
///         delivery: behavior::EstablishedDelivery<LocalProtocol>,
///     ) -> impl core::future::Future<Output = behavior::ItemSettlement<
///         behavior::EstablishedDelivery<LocalProtocol>, (), behavior::Never, behavior::Never,
///     >> + Send {
///         async move {
///             drop(delivery);
///             behavior::ItemSettlement::Accepted(())
///         }
///     }
/// }
/// fn require_async<T>()
/// where
///     T: behavior::InterpretSends<Runtime, behavior::User<LocalAddr, std::rc::Rc<()>>, behavior::Here>,
/// {}
/// let endpoint = LocalEndpoint(std::rc::Rc::new(()), core::marker::PhantomData);
/// let recipient = behavior::EstablishedRecipient::<LocalProtocol>::issued(endpoint);
/// let _delivery = behavior::EstablishedDelivery::new(recipient, std::rc::Rc::new(()));
/// require_async::<Vec<behavior::EstablishedDelivery<LocalProtocol>>>();
/// ```
pub trait RecipientAddress: Address + Sized {
    type Established<P>: Clone
    where
        P: Protocol<Addr = Self>;
}

/// Runtime-owned installed actor family for an address namespace.
///
/// The runtime must bind one protocol endpoint and its matching concrete
/// lifecycle authority to the same incarnation before issuing this value.
/// A namespace used only for protocol messaging implements
/// [`RecipientAddress`] without this stronger port.
pub trait EndpointAddress: Address + Sized {
    type Established<P>: Clone
    where
        P: Protocol<Addr = Self>;

    type Installed<B>: Clone
    where
        B: Behavior<Protocol: Protocol<Addr = Self>>;

    /// Project the message-only endpoint from one installed actor.
    fn recipient<B>(
        installed: &Self::Installed<B>,
    ) -> <Self as EndpointAddress>::Established<B::Protocol>
    where
        B: Behavior<Protocol: Protocol<Addr = Self>>;
}

impl<A: EndpointAddress> RecipientAddress for A {
    type Established<P>
        = <A as EndpointAddress>::Established<P>
    where
        P: Protocol<Addr = Self>;
}

/// Pure logical destination for one concrete protocol signature.
///
/// The destination protocol owner is part of the type even when two protocols
/// share the same address namespace and message type. This value proves only
/// the static signature at a logical address; it does not prove that an exact
/// executable incarnation has been installed there. Addressed recipients are
/// retained for genuine transport and name-resolution boundaries.
pub struct Recipient<P: Protocol> {
    address: P::Addr,
    protocol: PhantomData<fn() -> P>,
}

impl<P: Protocol> Copy for Recipient<P> {}

impl<P: Protocol> Clone for Recipient<P> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<P: Protocol> Recipient<P> {
    #[must_use]
    pub fn global(address: P::Addr) -> Self {
        Self::new(address)
    }

    /// Return the logical address, independent of any sending actor.
    #[must_use]
    pub const fn address(self) -> P::Addr {
        self.address
    }

    const fn new(address: P::Addr) -> Self {
        Self {
            address,
            protocol: PhantomData,
        }
    }
}

impl<A: Address, M> From<A> for Recipient<MessageProtocol<A, M>> {
    fn from(address: A) -> Self {
        Self::new(address)
    }
}

/// Inert capability for one exact protocol endpoint.
///
/// The endpoint type is selected by `P::Addr`, so `P` remains the only
/// protocol identity and ordinary domain types carry no endpoint parameter.
/// The endpoint has no direct accessor or send method. It crosses only an
/// explicit interpretation boundary. That boundary is public and therefore a
/// deliberate power-user authority boundary, not exclusive runtime authority.
///
/// ```compile_fail,E0599
/// #[derive(Clone, Copy, PartialEq, Eq)]
/// struct RuntimeAddr(u64);
/// impl behavior::Address for RuntimeAddr { type Nonce = u64; }
/// struct Worker;
/// impl behavior::Protocol for Worker { type Addr = RuntimeAddr; type Msg = (); }
/// #[derive(Clone)]
/// struct Endpoint;
/// impl behavior::RecipientAddress for RuntimeAddr {
///     type Established<P> = Endpoint where P: behavior::Protocol<Addr = Self>;
/// }
/// let recipient = behavior::EstablishedRecipient::<Worker>::issued(Endpoint);
/// let _endpoint = recipient.endpoint();
/// ```
pub struct EstablishedRecipient<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
{
    pub(crate) endpoint: <P::Addr as RecipientAddress>::Established<P>,
}

impl<P> EstablishedRecipient<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
{
    /// Issue a capability from an exact endpoint established or imported by
    /// an interpreter.
    ///
    /// This constructor performs no allocation or validation. It proves only
    /// protocol messaging; it never establishes a concrete behavior or
    /// lifecycle authority.
    #[must_use]
    pub const fn issued(endpoint: <P::Addr as RecipientAddress>::Established<P>) -> Self {
        Self { endpoint }
    }

    /// Transfer the endpoint through an explicit interpretation boundary.
    pub fn interpret<I>(self, interpreter: &mut I) -> I::Output
    where
        I: InterpretEstablished<P>,
    {
        interpreter.interpret_established(self.endpoint)
    }
}

/// Public power-user transfer boundary for one exact endpoint.
pub trait InterpretEstablished<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
{
    type Output;

    fn interpret_established(
        &mut self,
        endpoint: <P::Addr as RecipientAddress>::Established<P>,
    ) -> Self::Output;
}

impl<P> Clone for EstablishedRecipient<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
{
    fn clone(&self) -> Self {
        Self::issued(self.endpoint.clone())
    }
}

impl<P> core::fmt::Debug for EstablishedRecipient<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
    <P::Addr as RecipientAddress>::Established<P>: core::fmt::Debug,
{
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        formatter
            .debug_tuple("EstablishedRecipient")
            .field(&self.endpoint)
            .finish()
    }
}

impl<P> PartialEq for EstablishedRecipient<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
    <P::Addr as RecipientAddress>::Established<P>: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.endpoint == other.endpoint
    }
}

impl<P> Eq for EstablishedRecipient<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
    <P::Addr as RecipientAddress>::Established<P>: Eq,
{
}

/// Inert capability for one exact installed concrete behavior.
///
/// An established recipient proves the public protocol endpoint. This value
/// additionally preserves which concrete behavior was installed, allowing
/// lifecycle effects to require static evidence about that behavior's event
/// algebra. It remains inert: no direct send, shutdown, endpoint accessor, or
/// other ambient effect is exposed.
///
/// A protocol recipient cannot issue installed-actor authority:
///
/// ```compile_fail,E0271
/// #[derive(Clone, Copy, Eq, PartialEq)]
/// struct RuntimeAddr;
/// impl behavior::Address for RuntimeAddr { type Nonce = u8; }
/// #[derive(Clone)]
/// struct Endpoint;
/// struct Installed<B: behavior::Behavior>(Endpoint, std::sync::mpsc::Sender<B::Event>);
/// impl<B: behavior::Behavior> Clone for Installed<B> {
///     fn clone(&self) -> Self { Self(self.0.clone(), self.1.clone()) }
/// }
/// impl behavior::EndpointAddress for RuntimeAddr {
///     type Established<P> = Endpoint where P: behavior::Protocol<Addr = Self>;
///     type Installed<B> = Installed<B>
///         where B: behavior::Behavior<Protocol: behavior::Protocol<Addr = Self>>;
///     fn recipient<B>(installed: &Self::Installed<B>) -> Endpoint
///     where B: behavior::Behavior<Protocol: behavior::Protocol<Addr = Self>> {
///         installed.0.clone()
///     }
/// }
/// struct Worker;
/// impl behavior::Protocol for Worker { type Addr = RuntimeAddr; type Msg = (); }
/// impl behavior::Behavior for Worker {
///     type Protocol = Self;
///     type Event = behavior::User<RuntimeAddr, ()>;
///     type Sends = behavior::NoSends;
///     type Ph = behavior::Never;
///     type Error = behavior::Never;
///     type Birth = behavior::NoBirths;
///     fn transition(&mut self, _: behavior::ActiveTurn, _: Self::Event)
///         -> behavior::BehaviorActed<Self> { Ok(behavior::Actions::cont()) }
/// }
/// let recipient = behavior::EstablishedRecipient::<Worker>::issued(Endpoint);
/// let _: behavior::EstablishedActor<Worker> = behavior::EstablishedActor::issued(recipient);
/// ```
///
/// Two behaviors sharing one protocol retain distinct installed authority:
///
/// ```compile_fail,E0308
/// #[derive(Clone, Copy, Eq, PartialEq)]
/// struct RuntimeAddr;
/// impl behavior::Address for RuntimeAddr { type Nonce = u8; }
/// #[derive(Clone)]
/// struct Endpoint;
/// struct Installed<B: behavior::Behavior>(Endpoint, std::sync::mpsc::Sender<B::Event>);
/// impl<B: behavior::Behavior> Clone for Installed<B> {
///     fn clone(&self) -> Self { Self(self.0.clone(), self.1.clone()) }
/// }
/// impl behavior::EndpointAddress for RuntimeAddr {
///     type Established<P> = Endpoint where P: behavior::Protocol<Addr = Self>;
///     type Installed<B> = Installed<B>
///         where B: behavior::Behavior<Protocol: behavior::Protocol<Addr = Self>>;
///     fn recipient<B>(installed: &Self::Installed<B>) -> Endpoint
///     where B: behavior::Behavior<Protocol: behavior::Protocol<Addr = Self>> {
///         installed.0.clone()
///     }
/// }
/// struct Shared;
/// impl behavior::Protocol for Shared { type Addr = RuntimeAddr; type Msg = (); }
/// struct First;
/// struct Second;
/// impl behavior::Behavior for First {
///     type Protocol = Shared;
///     type Event = behavior::User<RuntimeAddr, ()>;
///     type Sends = behavior::NoSends;
///     type Ph = behavior::Never;
///     type Error = behavior::Never;
///     type Birth = behavior::NoBirths;
///     fn transition(&mut self, _: behavior::ActiveTurn, _: Self::Event)
///         -> behavior::BehaviorActed<Self> { Ok(behavior::Actions::cont()) }
/// }
/// impl behavior::Behavior for Second {
///     type Protocol = Shared;
///     type Event = behavior::EventLayer<u8, behavior::User<RuntimeAddr, ()>>;
///     type Sends = behavior::NoSends;
///     type Ph = behavior::Never;
///     type Error = behavior::Never;
///     type Birth = behavior::NoBirths;
///     fn transition(&mut self, _: behavior::ActiveTurn, _: Self::Event)
///         -> behavior::BehaviorActed<Self> { Ok(behavior::Actions::cont()) }
/// }
/// let (control, _inbox) = std::sync::mpsc::channel::<<First as behavior::Behavior>::Event>();
/// let first = behavior::EstablishedActor::<First>::issued(Installed(Endpoint, control));
/// let _: behavior::EstablishedActor<Second> = first;
/// ```
pub struct EstablishedActor<B>
where
    B: Behavior,
    <B::Protocol as Protocol>::Addr: EndpointAddress,
{
    installed: <<B::Protocol as Protocol>::Addr as EndpointAddress>::Installed<B>,
}

impl<B> EstablishedActor<B>
where
    B: Behavior,
    <B::Protocol as Protocol>::Addr: EndpointAddress,
{
    /// Issue an exact actor capability after successful installation.
    ///
    /// This power-user boundary accepts one runtime-owned installed value.
    /// Only successful fresh installation and binding commit may issue it.
    /// It performs no allocation or validation itself.
    #[must_use]
    pub const fn issued(
        installed: <<B::Protocol as Protocol>::Addr as EndpointAddress>::Installed<B>,
    ) -> Self {
        Self { installed }
    }

    /// Project the exact public-protocol recipient for this incarnation.
    #[must_use]
    pub fn recipient(&self) -> EstablishedRecipient<B::Protocol> {
        EstablishedRecipient::issued(
            <<B::Protocol as Protocol>::Addr as EndpointAddress>::recipient(&self.installed),
        )
    }

    /// Consume the concrete-actor proof and retain its exact protocol
    /// recipient.
    #[must_use]
    pub fn into_recipient(self) -> EstablishedRecipient<B::Protocol> {
        EstablishedRecipient::issued(
            <<B::Protocol as Protocol>::Addr as EndpointAddress>::recipient(&self.installed),
        )
    }

    /// Transfer the complete installed value with its exact `B` index.
    pub fn interpret_actor<I>(self, interpreter: &mut I) -> I::Output
    where
        I: InterpretInstalledActor<B>,
    {
        interpreter.interpret_actor(self.installed)
    }
}

/// Public power-user transfer boundary for one exact installed actor.
pub trait InterpretInstalledActor<B>
where
    B: Behavior,
    <B::Protocol as Protocol>::Addr: EndpointAddress,
{
    type Output;

    fn interpret_actor(
        &mut self,
        installed: <<B::Protocol as Protocol>::Addr as EndpointAddress>::Installed<B>,
    ) -> Self::Output;
}

impl<B> Clone for EstablishedActor<B>
where
    B: Behavior,
    <B::Protocol as Protocol>::Addr: EndpointAddress,
{
    fn clone(&self) -> Self {
        Self::issued(self.installed.clone())
    }
}

impl<B> core::fmt::Debug for EstablishedActor<B>
where
    B: Behavior,
    <B::Protocol as Protocol>::Addr: EndpointAddress,
    <<B::Protocol as Protocol>::Addr as EndpointAddress>::Installed<B>: core::fmt::Debug,
{
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        formatter
            .debug_tuple("EstablishedActor")
            .field(&self.installed)
            .finish()
    }
}

impl<B> PartialEq for EstablishedActor<B>
where
    B: Behavior,
    <B::Protocol as Protocol>::Addr: EndpointAddress,
    <<B::Protocol as Protocol>::Addr as EndpointAddress>::Installed<B>: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.installed == other.installed
    }
}

impl<B> Eq for EstablishedActor<B>
where
    B: Behavior,
    <B::Protocol as Protocol>::Addr: EndpointAddress,
    <<B::Protocol as Protocol>::Addr as EndpointAddress>::Installed<B>: Eq,
{
}

impl<P: Protocol> PartialEq for Recipient<P> {
    fn eq(&self, other: &Self) -> bool {
        self.address == other.address
    }
}

impl<P: Protocol> Eq for Recipient<P> {}

impl<P: Protocol> core::fmt::Debug for Recipient<P>
where
    P::Addr: core::fmt::Debug,
    <P::Addr as Address>::Nonce: core::fmt::Debug,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.address.fmt(f)
    }
}

/// One pure communication addressed to a concrete protocol signature.
///
/// Protocol identity is not inferred from the payload. Consequently, two
/// protocols with the same address and message types still have distinct
/// delivery types.
///
/// ```compile_fail,E0308
/// struct Queue;
/// struct Worker;
/// impl behavior::Protocol for Queue {
///     type Addr = behavior::MailAddr;
///     type Msg = u8;
/// }
/// impl behavior::Protocol for Worker {
///     type Addr = behavior::MailAddr;
///     type Msg = u8;
/// }
///
/// let worker = behavior::Recipient::<Worker>::global(behavior::MailAddr(1));
/// let _: behavior::Delivery<Queue> = behavior::Delivery::new(worker, 7);
/// ```
///
/// A destination also fixes its message and address namespaces:
///
/// ```compile_fail,E0308
/// #[derive(Clone, Copy, PartialEq, Eq)]
/// struct OtherAddr(u64);
/// impl behavior::Address for OtherAddr {
///     type Nonce = u64;
/// }
/// struct Worker;
/// impl behavior::Protocol for Worker {
///     type Addr = behavior::MailAddr;
///     type Msg = u8;
/// }
/// let _ = behavior::Recipient::<Worker>::global(OtherAddr(1));
/// ```
///
/// ```compile_fail,E0308
/// # struct Worker;
/// # impl behavior::Protocol for Worker {
/// #     type Addr = behavior::MailAddr;
/// #     type Msg = u8;
/// # }
/// let worker = behavior::Recipient::<Worker>::global(behavior::MailAddr(1));
/// let _ = behavior::Delivery::<Worker>::new(worker, "wrong payload");
/// ```
/// Exact reason one logical delivery was not accepted.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LogicalDeliveryReason {
    UnknownAddress,
    ClosedRecipient,
}

/// Exact reason one established delivery was not accepted.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ExactDeliveryReason {
    ClosedRecipient,
}

pub struct Delivery<P: Protocol> {
    pub to: Recipient<P>,
    pub message: P::Msg,
}

impl<P: Protocol> Delivery<P> {
    #[must_use]
    pub fn new(to: Recipient<P>, message: P::Msg) -> Self {
        Self { to, message }
    }
}

impl<P> Clone for Delivery<P>
where
    P: Protocol,
    P::Msg: Clone,
{
    fn clone(&self) -> Self {
        Self {
            to: self.to,
            message: self.message.clone(),
        }
    }
}

impl<P> PartialEq for Delivery<P>
where
    P: Protocol,
    P::Msg: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.to == other.to && self.message == other.message
    }
}

impl<P> Eq for Delivery<P>
where
    P: Protocol,
    P::Msg: Eq,
{
}

impl<P> ActionItem for Delivery<P>
where
    P: Protocol,
    P::Addr: Send,
    P::Msg: Send,
{
    type Accepted = ();
    type Rejection = LogicalDeliveryReason;
    type Prerequisite = Never;
}

/// One pure communication to an exact installed incarnation.
///
/// Unlike [`Delivery`], this effect carries the runtime-issued endpoint and
/// requires no address-to-endpoint resolution. Constructing it remains pure;
/// only an explicit [`crate::InterpretItem`] implementation for this exact
/// delivery type can
/// perform the communication.
///
/// Exact endpoints remain protocol-indexed even when two protocols share an
/// address namespace and message type:
///
/// ```compile_fail,E0308
/// #[derive(Clone, Copy, PartialEq, Eq)]
/// struct RuntimeAddr(u64);
/// impl behavior::Address for RuntimeAddr { type Nonce = u64; }
/// struct Endpoint<P>(core::marker::PhantomData<fn() -> P>);
/// impl<P> Clone for Endpoint<P> {
///     fn clone(&self) -> Self { Self(core::marker::PhantomData) }
/// }
/// impl behavior::RecipientAddress for RuntimeAddr {
///     type Established<P> = Endpoint<P> where P: behavior::Protocol<Addr = Self>;
/// }
/// struct Queue;
/// struct Worker;
/// impl behavior::Protocol for Queue { type Addr = RuntimeAddr; type Msg = u8; }
/// impl behavior::Protocol for Worker { type Addr = RuntimeAddr; type Msg = u8; }
/// let worker = behavior::EstablishedRecipient::<Worker>::issued(Endpoint(core::marker::PhantomData));
/// let _: behavior::EstablishedDelivery<Queue> = behavior::EstablishedDelivery::new(worker, 7);
/// ```
pub struct EstablishedDelivery<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
{
    pub to: EstablishedRecipient<P>,
    pub message: P::Msg,
}

impl<P> EstablishedDelivery<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
{
    #[must_use]
    pub const fn new(to: EstablishedRecipient<P>, message: P::Msg) -> Self {
        Self { to, message }
    }
}

impl<P> Clone for EstablishedDelivery<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
    P::Msg: Clone,
{
    fn clone(&self) -> Self {
        Self::new(self.to.clone(), self.message.clone())
    }
}

impl<P> core::fmt::Debug for EstablishedDelivery<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
    EstablishedRecipient<P>: core::fmt::Debug,
    P::Msg: core::fmt::Debug,
{
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        formatter
            .debug_struct("EstablishedDelivery")
            .field("to", &self.to)
            .field("message", &self.message)
            .finish()
    }
}

impl<P> PartialEq for EstablishedDelivery<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
    EstablishedRecipient<P>: PartialEq,
    P::Msg: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.to == other.to && self.message == other.message
    }
}

impl<P> Eq for EstablishedDelivery<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
    EstablishedRecipient<P>: Eq,
    P::Msg: Eq,
{
}

impl<P> ActionItem for EstablishedDelivery<P>
where
    P: Protocol,
    P::Addr: RecipientAddress,
    <P::Addr as RecipientAddress>::Established<P>: Send,
    P::Msg: Send,
{
    type Accepted = ();
    type Rejection = ExactDeliveryReason;
    type Prerequisite = Never;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Actions, Never, NoBirths, User};
    use std::rc::Rc;

    struct Inbox;

    struct SignatureOnly;

    #[derive(Clone, Copy, PartialEq, Eq)]
    struct LocalAddr(u8);

    impl Address for LocalAddr {
        type Nonce = u8;
    }

    struct LocalProtocol;

    impl Protocol for LocalProtocol {
        type Addr = LocalAddr;
        type Msg = u8;
    }

    struct LocalEndpoint<P> {
        token: Rc<()>,
        protocol: PhantomData<fn() -> P>,
    }

    impl<P> Clone for LocalEndpoint<P> {
        fn clone(&self) -> Self {
            Self {
                token: self.token.clone(),
                protocol: PhantomData,
            }
        }
    }

    impl<P> core::fmt::Debug for LocalEndpoint<P> {
        fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            formatter.write_str("LocalEndpoint")
        }
    }

    impl<P> PartialEq for LocalEndpoint<P> {
        fn eq(&self, other: &Self) -> bool {
            Rc::ptr_eq(&self.token, &other.token)
        }
    }

    impl<P> Eq for LocalEndpoint<P> {}

    struct LocalInstalled<B: Behavior> {
        endpoint: LocalEndpoint<B::Protocol>,
        control: std::sync::mpsc::Sender<B::Event>,
        incarnation: Rc<()>,
    }

    impl<B: Behavior> Clone for LocalInstalled<B> {
        fn clone(&self) -> Self {
            Self {
                endpoint: self.endpoint.clone(),
                control: self.control.clone(),
                incarnation: self.incarnation.clone(),
            }
        }
    }

    impl<B: Behavior> core::fmt::Debug for LocalInstalled<B> {
        fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            formatter.write_str("LocalInstalled")
        }
    }

    impl<B: Behavior> PartialEq for LocalInstalled<B> {
        fn eq(&self, other: &Self) -> bool {
            Rc::ptr_eq(&self.incarnation, &other.incarnation)
        }
    }

    impl<B: Behavior> Eq for LocalInstalled<B> {}

    impl EndpointAddress for LocalAddr {
        type Established<P>
            = LocalEndpoint<P>
        where
            P: Protocol<Addr = Self>;

        type Installed<B>
            = LocalInstalled<B>
        where
            B: Behavior<Protocol: Protocol<Addr = Self>>;

        fn recipient<B>(installed: &Self::Installed<B>) -> Self::Established<B::Protocol>
        where
            B: Behavior<Protocol: Protocol<Addr = Self>>,
        {
            installed.endpoint.clone()
        }
    }

    struct LocalTransfer;

    impl InterpretEstablished<LocalProtocol> for LocalTransfer {
        type Output = Rc<()>;

        fn interpret_established(
            &mut self,
            endpoint: LocalEndpoint<LocalProtocol>,
        ) -> Self::Output {
            endpoint.token
        }
    }

    struct LocalEndpointTransfer;

    impl InterpretEstablished<LocalProtocol> for LocalEndpointTransfer {
        type Output = LocalEndpoint<LocalProtocol>;

        fn interpret_established(
            &mut self,
            endpoint: LocalEndpoint<LocalProtocol>,
        ) -> Self::Output {
            endpoint
        }
    }

    struct LocalBehavior;

    impl Behavior for LocalBehavior {
        type Protocol = LocalProtocol;
        type Event = User<LocalAddr, u8>;
        type Sends = Vec<Never>;
        type Ph = Never;
        type Error = Never;
        type Birth = NoBirths;

        fn transition(
            &mut self,
            _: crate::ActiveTurn,
            _: Self::Event,
        ) -> crate::BehaviorActed<Self> {
            Ok(Actions::cont())
        }
    }

    impl crate::Protocol for SignatureOnly {
        type Addr = MailAddr;
        type Msg = u8;
    }

    impl behavior::Protocol for Inbox {
        type Addr = MailAddr;
        type Msg = u8;
    }

    impl Behavior for Inbox {
        type Protocol = Self;
        type Event = User<MailAddr, u8>;
        type Sends = Vec<Never>;
        type Ph = Never;
        type Error = Never;
        type Birth = NoBirths;

        fn init(&mut self, _: crate::InitializationTurn) -> crate::BehaviorActed<Self> {
            Ok(Actions::cont())
        }

        fn transition(
            &mut self,
            _: crate::ActiveTurn,
            _: Self::Event,
        ) -> crate::BehaviorActed<Self> {
            Ok(Actions::cont())
        }
    }

    #[test]
    fn mail_address_conversion_preserves_nonzero_value() {
        assert_eq!(u64::from(MailAddr(41)), 41);
    }

    #[test]
    fn routing_requires_only_the_static_protocol_signature() {
        let recipient = Recipient::<SignatureOnly>::global(MailAddr(7));
        let delivery = Delivery::new(recipient, 11);

        assert_eq!(delivery.to.address(), MailAddr(7));
        assert_eq!(delivery.message, 11);
    }

    #[test]
    fn recipient_value_contract_distinguishes_logical_addresses() {
        let global = Recipient::<Inbox>::global(MailAddr(7));
        let same_global = Recipient::<Inbox>::global(MailAddr(7));
        let other_global = Recipient::<Inbox>::global(MailAddr(8));

        assert_eq!(global, same_global);
        assert_ne!(global, other_global);
        assert_eq!(global.address(), MailAddr(7));
        assert_eq!(format!("{global:?}"), "MailAddr(7)");
    }

    #[test]
    fn established_capability_accepts_a_local_non_send_endpoint() {
        let token = Rc::new(());
        let recipient = EstablishedRecipient::<LocalProtocol>::issued(LocalEndpoint {
            token: token.clone(),
            protocol: PhantomData,
        });
        let retained = recipient.clone();
        let extracted = retained.interpret(&mut LocalTransfer);

        assert!(Rc::ptr_eq(&token, &extracted));
        drop(recipient);
    }

    #[test]
    fn established_values_compare_complete_identity_and_expose_debug_shape() {
        let token = Rc::new(());
        let other_token = Rc::new(());
        let recipient = EstablishedRecipient::<LocalProtocol>::issued(LocalEndpoint {
            token: token.clone(),
            protocol: PhantomData,
        });
        let same_recipient = EstablishedRecipient::<LocalProtocol>::issued(LocalEndpoint {
            token,
            protocol: PhantomData,
        });
        let other_recipient = EstablishedRecipient::<LocalProtocol>::issued(LocalEndpoint {
            token: other_token,
            protocol: PhantomData,
        });

        assert_eq!(recipient, same_recipient);
        assert_ne!(recipient, other_recipient);
        assert_eq!(
            format!("{recipient:?}"),
            "EstablishedRecipient(LocalEndpoint)"
        );

        let (control, _consumer) = std::sync::mpsc::channel();
        let installed = LocalInstalled {
            endpoint: recipient.clone().interpret(&mut LocalEndpointTransfer),
            control,
            incarnation: Rc::new(()),
        };
        let actor = EstablishedActor::<LocalBehavior>::issued(installed.clone());
        let same_actor = EstablishedActor::<LocalBehavior>::issued(installed);
        let (other_control, _other_consumer) = std::sync::mpsc::channel();
        let other_actor = EstablishedActor::<LocalBehavior>::issued(LocalInstalled {
            endpoint: other_recipient
                .clone()
                .interpret(&mut LocalEndpointTransfer),
            control: other_control,
            incarnation: Rc::new(()),
        });
        assert_eq!(actor, same_actor);
        assert_ne!(actor, other_actor);
        assert_eq!(format!("{actor:?}"), "EstablishedActor(LocalInstalled)");

        let delivery = EstablishedDelivery::new(recipient, 7);
        let same_delivery = EstablishedDelivery::new(same_recipient.clone(), 7);
        let other_destination = EstablishedDelivery::new(other_recipient.clone(), 7);
        let other_message = EstablishedDelivery::new(same_recipient, 9);
        let both_different = EstablishedDelivery::new(other_recipient, 9);
        assert_eq!(delivery, same_delivery);
        assert_ne!(delivery, other_destination);
        assert_ne!(delivery, other_message);
        assert_ne!(delivery, both_different);
        assert_eq!(
            format!("{delivery:?}"),
            "EstablishedDelivery { to: EstablishedRecipient(LocalEndpoint), message: 7 }"
        );
    }

    #[test]
    fn delivery_equality_requires_both_destination_and_message() {
        let value = Delivery::<Inbox>::new(Recipient::global(MailAddr(1)), 5);
        let same = Delivery::<Inbox>::new(Recipient::global(MailAddr(1)), 5);
        let other_destination = Delivery::<Inbox>::new(Recipient::global(MailAddr(2)), 5);
        let other_message = Delivery::<Inbox>::new(Recipient::global(MailAddr(1)), 6);
        let both_different = Delivery::<Inbox>::new(Recipient::global(MailAddr(2)), 6);

        assert!(value == same);
        assert!(value != other_destination);
        assert!(value != other_message);
        assert!(value != both_different);
    }
}