Skip to main content

acton_reactive/message/
message_address.rs

1/*
2 * Copyright (c) 2024. Govcraft
3 *
4 * Licensed under either of
5 *   * Apache License, Version 2.0 (the "License");
6 *     you may not use this file except in compliance with the License.
7 *     You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8 *   * MIT license: http://opensource.org/licenses/MIT
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the applicable License for the specific language governing permissions and
14 * limitations under that License.
15 */
16
17use crate::common::ActorSender;
18use acton_ern::prelude::*;
19use derive_new::new; // Keep using derive_new for the constructor
20
21/// Represents the addressable endpoint of an actor, combining its identity and inbox channel.
22///
23/// A `MessageAddress` contains the necessary information to send a message to a specific
24/// actor: its unique identifier (`sender`, an [`Ern`]) and the sender half (`address`)
25/// of the MPSC channel connected to its inbox.
26///
27/// This struct is typically used within message envelopes ([`OutboundEnvelope`]) to specify
28/// the sender and recipient of a message.
29#[derive(new, Clone, Debug)]
30pub struct MessageAddress {
31    /// The sender part of the MPSC channel for the actor's inbox.
32    pub(crate) address: ActorSender,
33    /// The unique identifier (`Ern`) of the actor associated with this address.
34    pub(crate) sender: Ern,
35}
36
37impl MessageAddress {
38    /// Returns the root name component of the actor's identifier (`Ern`).
39    ///
40    /// This provides a simple string representation of the actor's base name.
41    #[inline]
42    #[must_use]
43    pub fn name(&self) -> &str {
44        self.sender.root().as_str()
45    }
46}
47
48impl Default for MessageAddress {
49    /// Creates a default `MessageAddress` with a default `Ern` and a closed channel sender.
50    ///
51    /// This is primarily useful for placeholder initialization before a real address is known.
52    /// Messages cannot be successfully sent using the default address's channel sender.
53    fn default() -> Self {
54        let (outbox, _) = tokio::sync::mpsc::channel(1); // Create a dummy, likely closed sender
55        Self::new(outbox, Ern::default())
56    }
57}
58
59// Unit tests remain unchanged and undocumented
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_return_address() {
66        let return_address = MessageAddress::default();
67        // The default sender might not *always* be closed immediately,
68        // depending on MPSC channel implementation details, but it's not usable.
69        // A better test might involve trying to send and expecting an error,
70        // but that requires more setup. This assertion is okay for basic check.
71        assert!(return_address.address.is_closed());
72    }
73}