typedb_driver/common/
promise_async.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20pub use futures::future::BoxFuture as BoxPromise;
21use futures::future::Future;
22
23pub fn box_promise<'a, T>(future: impl Promise<'a, T>) -> BoxPromise<'a, T> {
24    Box::pin(future)
25}
26
27/// Async promise, an alias for Rust's built-in Future.
28/// A `BoxPromise` is an alias for Rust's built-in BoxFuture.
29///
30/// # Examples
31///
32/// ```rust
33/// promise.await
34/// ```
35pub trait Promise<'a, T>: Future<Output = T> + Send + 'a {}
36impl<'a, T, U: Future<Output = T> + Send + 'a> Promise<'a, T> for U {}
37
38#[macro_export]
39macro_rules! promisify {
40    {$($fut:tt)*} => {
41        #[allow(redundant_semicolons)]
42        async move { $($fut)* }
43    };
44}
45
46#[macro_export]
47macro_rules! resolve {
48    ($fut:expr $(,)?) => {
49        ($fut).await
50    };
51}