acktor-ipc 1.0.1

Interprocess communication support for the acktor actor framework
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
use std::fmt::{self, Debug};
use std::result::Result as StdResult;

use ahash::HashMap;
use bytes::Bytes;
use futures_util::{FutureExt, TryFutureExt};
use tokio::time::{Duration, Instant};
use tracing::{Instrument, info, warn};

use acktor::{
    Actor, ActorContext, ActorId, Address, ErrorReport, Handler, Message, Recipient, Sender,
    SenderId, channel::oneshot, message::FutureMessageResult, utils::debug_trace,
};
use acktor_ipc_proto::{actor_message, ipc_message, node_message, utils as proto_utils};

use crate::actor_handle::ActorHandle;
use crate::codec::{Decode, DecodeContext, Encode};
use crate::errors::{DecodeError, SessionError};
use crate::ipc_method::IpcConnection;
use crate::node::{
    LabelMap,
    factory::{self, Factory},
};
use crate::remote_actor::RemoteActorRegistry;
use crate::remote_address::RemoteAddress;
use crate::remote_message::RemoteMessage;

pub mod command;

mod session_handle;
pub use session_handle::SessionHandle;

mod context;
use context::SessionContext;

type Result<T> = StdResult<T, SessionError>;

#[derive(Message)]
#[result_type(())]
struct ActorMessageResponse {
    tag: u64,
    result: StdResult<Bytes, String>,
}

impl Debug for ActorMessageResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("ActorMessageResponse")
            .field(&self.tag)
            .finish()
    }
}

#[derive(Message)]
#[result_type(())]
struct CreateActorResponse {
    tag: u64,
    result: StdResult<ActorId, String>,
}

impl Debug for CreateActorResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("CreateActorResponse")
            .field(&self.tag)
            .finish()
    }
}

/// An actor which manages the IPC connection to a remote endpoint.
pub struct Session {
    connection: Box<dyn IpcConnection>,
    factory: Address<Factory>,
    registry: RemoteActorRegistry,
    label_map: LabelMap,
    tag: u64, // unique tag generator
    decode_context: Option<DecodeContext>,
    node_msg_res_tx_map: HashMap<u64, (oneshot::Sender<Result<RemoteAddress>>, Instant)>,
    actor_msg_res_tx_map: HashMap<u64, (oneshot::Sender<Bytes>, Instant)>,
}

impl Session {
    /// Constructs a new [`Session`].
    pub fn new(
        connection: Box<dyn IpcConnection>,
        factory: Address<Factory>,
        registry: RemoteActorRegistry,
        label_map: LabelMap,
    ) -> Self {
        Self {
            connection,
            factory,
            registry,
            label_map,
            tag: 0,
            decode_context: None,
            actor_msg_res_tx_map: HashMap::default(),
            node_msg_res_tx_map: HashMap::default(),
        }
    }

    fn cleanup_expired_response_tx(&mut self) {
        let now = Instant::now();

        self.node_msg_res_tx_map
            .retain(|_, (_, timestamp)| now.duration_since(*timestamp) < Duration::from_secs(30));
        self.actor_msg_res_tx_map
            .retain(|_, (_, timestamp)| now.duration_since(*timestamp) < Duration::from_secs(30));
    }

    fn next_tag(&mut self) -> u64 {
        let tag = self.tag;
        self.tag = self.tag.wrapping_add(1);
        tag
    }

    async fn send_ipc_message(&mut self, ipc_msg: ipc_message::IpcMessage) -> Result<()> {
        let encoded_ipc_msg = ipc_msg.encode_to_bytes(None)?;
        self.connection
            .send(encoded_ipc_msg)
            .await
            .map_err(SessionError::SendOutboundMessageFailed)?;

        Ok(())
    }

    fn decode_context(&self) -> Result<&DecodeContext> {
        self.decode_context
            .as_ref()
            .ok_or_else(|| DecodeError::MissingDecodeContext.into())
    }

    fn find_actor(&self, actor: &ActorHandle) -> Result<Recipient<RemoteMessage>> {
        match actor {
            ActorHandle::Index(actor_id) => {
                if actor_id.is_remote() {
                    return Err(DecodeError::DecodeRemoteAddress.into());
                }

                self.registry
                    .get(*actor_id)
                    .ok_or_else(|| SessionError::ActorNotFound(actor_id.to_string()))
            }

            ActorHandle::Label(label) => self
                .label_map
                .get(label)
                .ok_or_else(|| SessionError::ActorNotFound(label.clone()))
                .and_then(|actor_id| {
                    self.registry
                        .get(*actor_id)
                        .ok_or_else(|| SessionError::ActorNotFound(actor_id.to_string()))
                }),
        }
    }

    async fn handle_node_message(
        &mut self,
        message: node_message::NodeMessage,
        ctx: &mut <Self as Actor>::Context,
    ) -> Result<()> {
        match message.message {
            Some(node_message::MessageType::CreateActor(node_message::CreateActor {
                label,
                r#type,
                config,
                tag,
            })) => {
                let factory = self.factory.clone();
                let address = ctx.address();

                // spawn a task to handle the potentially time consuming actor creation process
                tokio::spawn(
                    async move {
                        factory
                            .send(factory::CreateActor {
                                label,
                                r#type,
                                config,
                            })
                            .await?
                            .await?
                    }
                    .then(move |result| async move {
                        // send the result back to this actor with CreateActorResponse message
                        // the IpcConnection can not be cloned into the spawned task without a
                        // Arc<Mutex<..>>, so we convert this into a sequential message handling
                        // process
                        address
                            .do_send(CreateActorResponse {
                                tag,
                                result: result.map_err(|e| e.report()),
                            })
                            .await
                    })
                    .inspect_err(|e| {
                        // we can not do much if sending the response back to this actor fails,
                        // just log it
                        warn!(
                            "Could not send `NodeMessageResponse::CreateActor` to remote peer: {}",
                            e.report()
                        );
                    })
                    .in_current_span(),
                );

                Ok(())
            }

            Some(node_message::MessageType::GetActor(node_message::GetActor { actor, tag })) => {
                // convert proto::utils::ActorHandle to crate::ActorHandle, ugly
                let actor = match actor {
                    Some(proto_utils::ActorHandle { handle }) => match handle {
                        Some(proto_utils::ActorHandleType::Index(actor_id)) => {
                            ActorHandle::Index(actor_id)
                        }
                        Some(proto_utils::ActorHandleType::Label(label)) => {
                            ActorHandle::Label(label)
                        }
                        None => {
                            return Err(DecodeError::from(
                                "missing field `handle` in `ActorHandle`",
                            )
                            .into());
                        }
                    },

                    _ => {
                        return Err(DecodeError::from(
                            "missing field `actor` in `NodeMessage::GetActor`",
                        )
                        .into());
                    }
                };

                let result = self.find_actor(&actor).map(|recipient| recipient.index());

                let ipc_msg = ipc_message::IpcMessage::node_message_response(
                    node_message::NodeMessageResponse::get_actor(
                        tag,
                        result.map_err(|e| e.report()),
                    ),
                );

                self.send_ipc_message(ipc_msg).await.inspect_err(|e| {
                    // we can not do much if sending the response back to the remote peer fails,
                    // just log it
                    warn!(
                        "Could not send `NodeMessageResponse::GetActor` to remote peer: {}",
                        e.report()
                    );
                })
            }

            _ => Err(DecodeError::from("missing field `message` in `NodeMessage`").into()),
        }
    }

    fn _handle_node_message_response(
        &mut self,
        tag: u64,
        result: Option<node_message::ResultType>,
        name: &str,
    ) -> Result<()> {
        let (sender, _) = self
            .node_msg_res_tx_map
            .remove(&tag)
            // if the tag is not found in the map, we do not know who to send the result to, and
            // we do not kown who to report the error to either, so just return an error and the
            // session's context will log it
            .ok_or(SessionError::InvalidNodeMsgResTxTag(tag))?;

        // remote error and processing error should be reported to the original sender who is
        // waiting for a `Result<RemoteAddress, SessionError>`
        let result = match result {
            Some(node_message::ResultType::ActorId(actor_id)) => match self.decode_context() {
                Ok(ctx) => ctx.create_remote_address(actor_id).map_err(Into::into),
                Err(e) => Err(e),
            },
            Some(node_message::ResultType::Err(e)) => Err(SessionError::RemotePeerError(e)),

            _ => Err(DecodeError::from(format!(
                "missing field `result` in `NodeMessageResponse::{}`",
                name
            ))
            .into()),
        };

        sender
            .send(result)
            // we can not do much if sending the result back to the original sender fails, just
            // return an error and the session's context will log it
            .map_err(|_| SessionError::ForwardNodeMsgResFailed)
    }

    fn handle_node_message_response(
        &mut self,
        response: node_message::NodeMessageResponse,
        _ctx: &mut <Self as Actor>::Context,
    ) -> Result<()> {
        let tag = response.tag;

        match response.response {
            Some(node_message::ResponseType::CreateActor(node_message::ResultRemoteAddress {
                result,
            })) => self._handle_node_message_response(tag, result, "CreateActor"),

            Some(node_message::ResponseType::GetActor(node_message::ResultRemoteAddress {
                result,
            })) => self._handle_node_message_response(tag, result, "GetActor"),

            _ => Err(DecodeError::from("missing field `response` in `NodeMessageResponse`").into()),
        }
    }

    async fn handle_actor_message(
        &mut self,
        message: actor_message::ActorMessage,
        ctx: &mut <Self as Actor>::Context,
    ) -> Result<()> {
        let actor_message::ActorMessage {
            actor_id,
            message_id,
            message,
            tag,
        } = message;

        match tag {
            Some(tag) => {
                // send

                let address = ctx.address();
                let recipient = self.find_actor(&ActorHandle::Index(actor_id));
                let decode_context = self.decode_context().cloned();
                let (tx, rx) = oneshot::channel();

                // spawn a task to handle the potentially time consuming message handling process
                tokio::spawn(
                    async move {
                        recipient?
                            .do_send(
                                RemoteMessage::send(actor_id, message_id, message, tx)
                                    .with_context(decode_context?),
                            )
                            .await
                            .map_err(|e| SessionError::ForwardInboundMessageFailed(e.into()))?;

                        let result = rx
                            .await
                            .map_err(|e| SessionError::HandleInboundMessageFailed(e.into()))?;

                        Ok::<Bytes, SessionError>(result)
                    }
                    .then(move |result| async move {
                        // send the result back to this actor with ActorMessageResponse message
                        // the IpcConnection can not be cloned into the spawned task without a
                        // Arc<Mutex<..>>, so we convert this into a sequential message handling
                        // process
                        address
                            .do_send(ActorMessageResponse {
                                tag,
                                result: result.map_err(|e| e.report()),
                            })
                            .await
                    })
                    .inspect_err(|e| {
                        // we can not do much if sending the response back to this actor fails,
                        // just log it
                        warn!(
                            "Could not send `ActorMessageResponse` to remote peer: {}",
                            e.report()
                        );
                    })
                    .in_current_span(),
                );

                Ok(())
            }

            None => {
                // do_send

                // sender has explicitly indicated that it does not care about the result of this
                // message, so we just return the error and the session's context will log it
                self.find_actor(&ActorHandle::Index(actor_id))?
                    .do_send(
                        RemoteMessage::do_send(actor_id, message_id, message)
                            .with_context(self.decode_context()?.clone()),
                    )
                    .await
                    .map_err(|e| SessionError::ForwardInboundMessageFailed(e.into()))
            }
        }
    }

    async fn handle_actor_message_response(
        &mut self,
        response: actor_message::ActorMessageResponse,
        _ctx: &mut <Self as Actor>::Context,
    ) -> Result<()> {
        let tag = response.tag;

        let (sender, _) = self
            .actor_msg_res_tx_map
            .remove(&tag)
            // if the tag is not found in the map, we do not know who to send the result
            // to, and we do not kown who to report the error to either, so just return
            // an error and the session's context will log it
            .ok_or(SessionError::InvalidActorMsgResTxTag(tag))?;

        // remote error and processing error should be reported to the original sender who is
        // waiting for a `Result<M::Result, RecvError>`
        let result: Result<_> = match response.response {
            Some(actor_message::ResponseType::Ok(bytes)) => Ok(bytes),
            Some(actor_message::ResponseType::Err(err)) => Err(SessionError::RemotePeerError(err)),
            None => {
                Err(DecodeError::from("missing field `response` in `ActorMessageResponse`").into())
            }
        };

        // we can not do much if sending the result back to the original sender fails, just return
        // an error and the session's context will log it
        match result {
            Ok(bytes) => sender
                .send(bytes)
                .map_err(|_| SessionError::ForwardActorMessageResFailed),
            Err(e) => sender
                .send_err(e)
                .map_err(|_| SessionError::ForwardActorMessageResFailed),
        }
    }

    async fn handle_ipc_message(
        &mut self,
        message: Bytes,
        ctx: &mut <Self as Actor>::Context,
    ) -> Result<()> {
        let ipc_message = ipc_message::IpcMessage::decode(message, None)?;

        match ipc_message.message {
            Some(ipc_message::IpcMessageType::NodeMessage(message)) => {
                self.handle_node_message(message, ctx).await
            }
            Some(ipc_message::IpcMessageType::NodeMessageResponse(response)) => {
                self.handle_node_message_response(response, ctx)
            }
            Some(ipc_message::IpcMessageType::ActorMessage(message)) => {
                self.handle_actor_message(message, ctx).await
            }
            Some(ipc_message::IpcMessageType::ActorMessageResponse(response)) => {
                self.handle_actor_message_response(response, ctx).await
            }
            _ => Err(DecodeError::from("missing field `message` in `IpcMessage`").into()),
        }
    }
}

impl Actor for Session {
    type Context = SessionContext;
    type Error = SessionError;

    async fn post_start(&mut self, ctx: &mut Self::Context) -> Result<()> {
        info!("Session {} is started", self.connection.peer_endpoint());

        self.decode_context = Some(DecodeContext::new(ctx.address(), self.registry.clone()));

        Ok(())
    }

    async fn post_stop(&mut self, _ctx: &mut Self::Context) -> Result<()> {
        self.connection
            .close()
            .await
            .map_err(SessionError::IoError)?;

        info!("Session {} is stopped", self.connection.peer_endpoint());

        Ok(())
    }
}

// See `handle_node_message` for what the remote peer actor will do when it receives the
// `NodeMessage` sent by this handler.
// See `handle_node_message_response` for how this actor forwards the result to the original
// sender when it receives the `NodeMessageResponse` from the remote peer actor.
impl Handler<command::CreateRemoteActor> for Session {
    type Result = FutureMessageResult<command::CreateRemoteActor>;

    async fn handle(
        &mut self,
        msg: command::CreateRemoteActor,
        _ctx: &mut <Self as Actor>::Context,
    ) -> Self::Result {
        debug_trace!("Handle command {:?}", msg);

        let command::CreateRemoteActor {
            label,
            r#type,
            config,
        } = msg;

        let (tx, rx) = oneshot::channel();

        let tag = self.next_tag();
        let ipc_msg = ipc_message::IpcMessage::node_message(
            node_message::NodeMessage::create_actor(label, r#type, config, tag),
        );

        if let Err(e) = self.send_ipc_message(ipc_msg).await {
            warn!(
                "Could not send `NodeMessage::CreateActor` to remote peer: {}",
                e.report()
            );
            // sends the error back to the original sender who is waiting for a
            // `Result<RemoteAddress, SessionError>`
            if let Err(e) = tx.send(Err(e)) {
                // we can not do much if sending the error back to the original sender fails, just
                // log it
                warn!(
                    "Could not report the error in `Handler<CreateRemoteActor>` to original \
                     sender: {}",
                    e.report()
                );
            }
        } else {
            self.node_msg_res_tx_map.insert(tag, (tx, Instant::now()));
        }

        FutureMessageResult::new(rx.map(|r| r.unwrap_or_else(|e| Err(e.into()))))
    }
}

// See `handle_node_message` for what the remote peer actor will do when it receives the
// `NodeMessage` sent by this handler.
// See `handle_node_message_response` for how this actor forwards the result to the original
// sender when it receives the `NodeMessageResponse` from the remote peer actor.
impl Handler<command::GetRemoteActor> for Session {
    type Result = FutureMessageResult<command::GetRemoteActor>;

    async fn handle(
        &mut self,
        msg: command::GetRemoteActor,
        _ctx: &mut <Self as Actor>::Context,
    ) -> Self::Result {
        debug_trace!("Handle command {:?}", msg);

        let command::GetRemoteActor { actor } = msg;

        let (tx, rx) = oneshot::channel();

        let tag = self.next_tag();
        let ipc_msg = match &actor {
            ActorHandle::Index(actor_id) => ipc_message::IpcMessage::node_message(
                node_message::NodeMessage::get_actor_with_index(*actor_id, tag),
            ),
            ActorHandle::Label(label) => ipc_message::IpcMessage::node_message(
                node_message::NodeMessage::get_actor_with_label(label.clone(), tag),
            ),
        };

        if let Err(e) = self.send_ipc_message(ipc_msg).await {
            warn!(
                "Could not send `NodeMessage::GetActor` to remote peer: {}",
                e.report()
            );
            // sends the error back to the original sender who is waiting for a
            // `Result<RemoteAddress, SessionError>`
            if let Err(e) = tx.send(Err(e)) {
                // we can not do much if sending the error back to the original sender fails, just
                // log it
                warn!(
                    "Could not report the error in `Handler<GetRemoteActor>` to original sender: \
                     {}",
                    e.report()
                );
            }
        } else {
            self.node_msg_res_tx_map.insert(tag, (tx, Instant::now()));
        }

        FutureMessageResult::new(rx.map(|r| r.unwrap_or_else(|e| Err(e.into()))))
    }
}

// See `handle_actor_message` for what the remote peer actor will do when it receives the
// `ActorMessage` sent by this handler.
// See `handle_actor_message_response` for how this actor forwards the result to the original
// sender when it receives the `ActorMessageResponse` from the remote peer actor.
impl Handler<RemoteMessage> for Session {
    type Result = ();

    async fn handle(
        &mut self,
        msg: RemoteMessage,
        _ctx: &mut <Self as Actor>::Context,
    ) -> Self::Result {
        debug_trace!("Handle command {:?}", msg);

        let RemoteMessage {
            actor_id,
            message_id,
            message,
            result_tx,
            ..
        } = msg;

        match result_tx {
            Some(tx) => {
                // send

                let tag = self.next_tag();
                let ipc_msg = ipc_message::IpcMessage::actor_message(
                    actor_message::ActorMessage::send(actor_id, message_id, message, tag),
                );

                if let Err(e) = self.send_ipc_message(ipc_msg).await {
                    warn!(
                        "Could not send `ActorMessage` to remote peer: {}",
                        e.report()
                    );
                    // sends the error back to the original sender who is waiting for a
                    // `Result<M::Result, RecvError>`, typically a `RemoteAddress`, note that the
                    // error original sender receives is a `RecvError` because the signature of
                    // the `Sender` trait so we need to use `send_err` here
                    if let Err(e) = tx.send_err(e) {
                        // we can not do much if sending the error back to the original sender
                        // fails, just log it
                        warn!(
                            "Could not report the error in `Handler<RemoteMessage>` to original \
                             sender: {}",
                            e.report()
                        );
                    }

                    return;
                }

                self.actor_msg_res_tx_map.insert(tag, (tx, Instant::now()));
            }

            None => {
                // do_send

                let ipc_msg = ipc_message::IpcMessage::actor_message(
                    actor_message::ActorMessage::do_send(actor_id, message_id, message),
                );

                if let Err(e) = self.send_ipc_message(ipc_msg).await {
                    // sender has explicitly indicated that it does not care about the result of
                    // this message, so we just log the error
                    warn!(
                        "Could not do_send `ActorMessage` to remote peer: {}",
                        e.report()
                    );
                }
            }
        }
    }
}

impl Handler<CreateActorResponse> for Session {
    type Result = ();

    async fn handle(&mut self, msg: CreateActorResponse, _ctx: &mut Self::Context) -> Self::Result {
        debug_trace!("Handle command {:?}", msg);

        let CreateActorResponse { tag, result } = msg;

        let ipc_msg = ipc_message::IpcMessage::node_message_response(
            node_message::NodeMessageResponse::create_actor(tag, result),
        );

        if let Err(e) = self.send_ipc_message(ipc_msg).await {
            // we can not do much if sending the response back to the remote peer fails, just log
            // it
            warn!(
                "Could not send `NodeMessageResponse::CreateActor` to remote peer: {}",
                e.report()
            )
        }
    }
}

impl Handler<ActorMessageResponse> for Session {
    type Result = ();

    async fn handle(
        &mut self,
        msg: ActorMessageResponse,
        _ctx: &mut <Self as Actor>::Context,
    ) -> Self::Result {
        debug_trace!("Handle command {:?}", msg);

        let ActorMessageResponse { tag, result } = msg;

        let ipc_msg = ipc_message::IpcMessage::actor_message_response(
            actor_message::ActorMessageResponse::new(tag, result),
        );

        if let Err(e) = self.send_ipc_message(ipc_msg).await {
            warn!(
                "Could not send `ActorMessageResponse` to remote peer: {}",
                e.report()
            )
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_debug_str() {
        let ok = ActorMessageResponse {
            tag: 42,
            result: Ok(Bytes::from_static(b"payload")),
        };
        assert_eq!(format!("{ok:?}"), "ActorMessageResponse(42)");

        let err = ActorMessageResponse {
            tag: 7,
            result: Err("boom".to_string()),
        };
        assert_eq!(format!("{err:?}"), "ActorMessageResponse(7)");

        let ok = CreateActorResponse {
            tag: 42,
            result: Ok(1234),
        };
        assert_eq!(format!("{ok:?}"), "CreateActorResponse(42)");

        let err = CreateActorResponse {
            tag: 7,
            result: Err("boom".to_string()),
        };
        assert_eq!(format!("{err:?}"), "CreateActorResponse(7)");
    }
}