acton_core/common/agent_reply.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 std::future::Future;
18use std::pin::Pin;
19
20/// A utility namespace for creating standard return types for `act_on` message handlers.
21///
22/// Message handlers registered with [`ManagedAgent::act_on`](crate::actor::ManagedAgent::act_on)
23/// typically need to return a future that is boxed and pinned, specifically [`FutureBox`].
24/// This struct provides helpers to create common future types that might be needed
25/// as part of that process.
26///
27/// It acts purely as a namespace and is not intended to be instantiated.
28pub struct AgentReply;
29
30impl AgentReply {
31 /// Creates an immediately resolving, no-operation future, boxed and pinned.
32 ///
33 /// This is useful for message handlers that perform synchronous work or do not need
34 /// to perform any asynchronous operations after processing the message.
35 ///
36 /// # Returns
37 ///
38 /// A `Pin<Box<impl Future<Output=()>>>` that completes immediately. This can often
39 /// be coerced or converted into the required [`FutureBox`].
40 #[inline]
41 pub fn immediate() -> Pin<Box<impl Future<Output=()> + Sized>> { // Original return type
42 Box::pin(async move {})
43 }
44
45 /// Wraps an existing future into a `Pin<Box<F>>`.
46 ///
47 /// This method takes any future `F` with `Output=()` and boxes and pins it.
48 /// This is often a necessary step before potentially casting or using it where
49 /// a [`FutureBox`] is expected, provided `F` meets the `Send + Sync + 'static` bounds.
50 ///
51 /// # Type Parameters
52 ///
53 /// * `F`: The type of the input future. Must have `Output=()`.
54 ///
55 /// # Arguments
56 ///
57 /// * `future`: The future to be wrapped.
58 ///
59 /// # Returns
60 ///
61 /// A `Pin<Box<F>>` containing the provided future.
62 #[inline]
63 pub fn from_async<F>(future: F) -> Pin<Box<F>> // Original return type
64 where
65 F: Future<Output = ()> + Sized, // Original bounds
66 {
67 Box::pin(future)
68 }
69}