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 struct for creating act_on response futures.
21pub struct AgentReply;
22
23impl AgentReply {
24 /// Creates a no-op (no operation) future.
25 ///
26 /// This method returns a future that does nothing and completes immediately.
27 /// It's useful in situations where you need to provide a future but don't want
28 /// it to perform any actual work.
29 ///
30 /// # Returns
31 ///
32 /// A pinned boxed future that resolves immediately without doing anything.
33 /// ```
34 pub fn immediate() -> Pin<Box<impl Future<Output=()> + Sized>> {
35 Box::pin(async move {})
36 }
37
38 /// Wraps a future in a pinned box.
39 ///
40 /// This method is useful for converting a future into a pinned boxed future,
41 /// which is required returning from async act_on message handlers.
42 ///
43 /// # Arguments
44 ///
45 /// * `future` - The future to be wrapped.
46 ///
47 /// # Returns
48 ///
49 /// A pinned boxed future.
50 /// ```
51 pub fn from_async<F>(future: F) -> Pin<Box<F>>
52 where
53 F: Future<Output=()> + Sized,
54 {
55 Box::pin(future)
56 }
57}