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
use crate :: { *, import::* };


/// Behavior representing the capability of delivering a message to an actor's mailbox.
///
/// The send method comes from [Sink]:
///
/// Send a message without wanting a return from the actor. This is a one-way operation.
/// This still returns a future because the mailbox might be async, so delivering the
/// message might be async, but this will resolve as soon as the message is delivered to the mailbox.
/// You will not get notified when the message is handled by receiving actor.
///
/// This returns result because sending to the mailbox might be a fallible action.
/// If any errors happen after the message is sent to the mailbox, you shall not be notified.
/// There shall be no acknowledgement of reception.
///
/// The call method provides a request-response pattern. You can also use it when the actor
/// returns `()` to be notified that the message has been processed.
//
pub trait Address<M>: Identify

where Self: Sink<M> + fmt::Debug + Unpin + Send                           ,
      M   : Message                                                       ,
      <Self as Sink<M>>::Error: std::error::Error + Send + Sync + 'static ,

{
	/// Call an actor and receive the result of the call. This is a two-way operation. Calling with
	/// a message type that has `Return=()` will notify you that the message has been handled by the
	/// receiver.
	///
	/// Note that all types implementing `Address` also implement Sink if you want to throw a message
	/// in a bottle. See the trait documentation for more information on the `send` method.
	//
	#[ must_use = "Futures do nothing unless polled" ]
	//
	fn call( &mut self, msg: M ) -> Return<'_, Result< <M as Message>::Return, <Self as Sink<M> >::Error >>;

	/// Get a clone of this address as a `Box<Address<M>>`.
	//
	fn clone_box( &self ) -> BoxAddress<M, <Self as Sink<M> >::Error>;

	/// Upcast `&self` to `&dyn Address`.
	//
	fn as_address( &self ) -> &dyn Address<M, Error = <Self as Sink<M> >::Error>

		where Self: Sized,
	{
		self
	}
}


impl<M, T> Address<M> for Box<T>

	where  M: Message                                                      ,
	       T: Address<M> + Identify + ?Sized                               ,
	       T: Sink<M> + fmt::Debug + Unpin + Send                          ,
	      <T as Sink<M>>::Error: std::error::Error + Send + Sync + 'static ,
{
	#[ must_use = "Futures do nothing unless polled" ]
	//
	fn call( &mut self, msg: M ) -> Return<'_, Result< <M as Message>::Return, <T as Sink<M> >::Error >>
	{
		(**self).call( msg )
	}

	/// Get a clone of this address as a `Box<Address<M>>`.
	//
	fn clone_box( &self ) -> BoxAddress<M, <T as Sink<M> >::Error>
	{
		(**self).clone_box()
	}
}



impl<M, T> Address<M> for &mut T

	where  M: Message                                                      ,
	       T: Address<M> + Identify + ?Sized                               ,
	       T: Sink<M> + fmt::Debug + Unpin + Send                          ,
	      <T as Sink<M>>::Error: std::error::Error + Send + Sync + 'static ,
{
	#[ must_use = "Futures do nothing unless polled" ]
	//
	fn call( &mut self, msg: M ) -> Return<'_, Result< <M as Message>::Return, <T as Sink<M> >::Error >>
	{
		(**self).call( msg )
	}

	/// Get a clone of this address as a `Box<Address<M>>`.
	//
	fn clone_box( &self ) -> BoxAddress<M, <T as Sink<M> >::Error>
	{
		(**self).clone_box()
	}
}