Skip to content

Commit 9536785

Browse files
committed
Return AsyncCallFuture<R> instead of opaque impl Future from ObjectLike trait.
1 parent 49389c4 commit 9536785

File tree

4 files changed

+34
-57
lines changed

4 files changed

+34
-57
lines changed

src/function.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,13 @@ impl LuaType for Function {
657657
#[must_use = "futures do nothing unless you `.await` or poll them"]
658658
pub struct AsyncCallFuture<R: FromLuaMulti>(Result<AsyncThread<R>>);
659659

660+
#[cfg(feature = "async")]
661+
impl<R: FromLuaMulti> AsyncCallFuture<R> {
662+
pub(crate) fn error(err: Error) -> Self {
663+
AsyncCallFuture(Err(err))
664+
}
665+
}
666+
660667
#[cfg(feature = "async")]
661668
impl<R: FromLuaMulti> Future for AsyncCallFuture<R> {
662669
type Output = Result<R>;

src/table.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ use crate::util::{assert_stack, check_stack, get_metatable_ptr, StackGuard};
1313
use crate::value::{Nil, Value};
1414

1515
#[cfg(feature = "async")]
16-
use {
17-
crate::types::MaybeSend,
18-
futures_util::future::{self, Either, Future},
19-
};
16+
use crate::function::AsyncCallFuture;
2017

2118
#[cfg(feature = "serde")]
2219
use {
@@ -892,9 +889,9 @@ impl ObjectLike for Table {
892889

893890
#[cfg(feature = "async")]
894891
#[inline]
895-
fn call_async<R>(&self, args: impl IntoLuaMulti) -> impl Future<Output = Result<R>> + MaybeSend
892+
fn call_async<R>(&self, args: impl IntoLuaMulti) -> AsyncCallFuture<R>
896893
where
897-
R: FromLuaMulti + MaybeSend,
894+
R: FromLuaMulti,
898895
{
899896
Function(self.0.copy()).call_async(args)
900897
}
@@ -908,13 +905,9 @@ impl ObjectLike for Table {
908905
}
909906

910907
#[cfg(feature = "async")]
911-
fn call_async_method<R>(
912-
&self,
913-
name: &str,
914-
args: impl IntoLuaMulti,
915-
) -> impl Future<Output = Result<R>> + MaybeSend
908+
fn call_async_method<R>(&self, name: &str, args: impl IntoLuaMulti) -> AsyncCallFuture<R>
916909
where
917-
R: FromLuaMulti + MaybeSend,
910+
R: FromLuaMulti,
918911
{
919912
self.call_async_function(name, (self, args))
920913
}
@@ -932,21 +925,17 @@ impl ObjectLike for Table {
932925

933926
#[cfg(feature = "async")]
934927
#[inline]
935-
fn call_async_function<R>(
936-
&self,
937-
name: &str,
938-
args: impl IntoLuaMulti,
939-
) -> impl Future<Output = Result<R>> + MaybeSend
928+
fn call_async_function<R>(&self, name: &str, args: impl IntoLuaMulti) -> AsyncCallFuture<R>
940929
where
941-
R: FromLuaMulti + MaybeSend,
930+
R: FromLuaMulti,
942931
{
943932
match self.get(name) {
944-
Ok(Value::Function(func)) => Either::Left(func.call_async(args)),
933+
Ok(Value::Function(func)) => func.call_async(args),
945934
Ok(val) => {
946935
let msg = format!("attempt to call a {} value (function '{name}')", val.type_name());
947-
Either::Right(future::ready(Err(Error::RuntimeError(msg))))
936+
AsyncCallFuture::error(Error::RuntimeError(msg))
948937
}
949-
Err(err) => Either::Right(future::ready(Err(err))),
938+
Err(err) => AsyncCallFuture::error(err),
950939
}
951940
}
952941

src/traits.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::util::{check_stack, short_type_name};
1111
use crate::value::Value;
1212

1313
#[cfg(feature = "async")]
14-
use std::future::Future;
14+
use {crate::function::AsyncCallFuture, std::future::Future};
1515

1616
/// Trait for types convertible to [`Value`].
1717
pub trait IntoLua: Sized {
@@ -162,9 +162,9 @@ pub trait ObjectLike: Sealed {
162162
/// arguments.
163163
#[cfg(feature = "async")]
164164
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
165-
fn call_async<R>(&self, args: impl IntoLuaMulti) -> impl Future<Output = Result<R>> + MaybeSend
165+
fn call_async<R>(&self, args: impl IntoLuaMulti) -> AsyncCallFuture<R>
166166
where
167-
R: FromLuaMulti + MaybeSend;
167+
R: FromLuaMulti;
168168

169169
/// Gets the function associated to key `name` from the object and calls it,
170170
/// passing the object itself along with `args` as function arguments.
@@ -178,13 +178,9 @@ pub trait ObjectLike: Sealed {
178178
/// This might invoke the `__index` metamethod.
179179
#[cfg(feature = "async")]
180180
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
181-
fn call_async_method<R>(
182-
&self,
183-
name: &str,
184-
args: impl IntoLuaMulti,
185-
) -> impl Future<Output = Result<R>> + MaybeSend
181+
fn call_async_method<R>(&self, name: &str, args: impl IntoLuaMulti) -> AsyncCallFuture<R>
186182
where
187-
R: FromLuaMulti + MaybeSend;
183+
R: FromLuaMulti;
188184

189185
/// Gets the function associated to key `name` from the object and calls it,
190186
/// passing `args` as function arguments.
@@ -200,13 +196,9 @@ pub trait ObjectLike: Sealed {
200196
/// This might invoke the `__index` metamethod.
201197
#[cfg(feature = "async")]
202198
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
203-
fn call_async_function<R>(
204-
&self,
205-
name: &str,
206-
args: impl IntoLuaMulti,
207-
) -> impl Future<Output = Result<R>> + MaybeSend
199+
fn call_async_function<R>(&self, name: &str, args: impl IntoLuaMulti) -> AsyncCallFuture<R>
208200
where
209-
R: FromLuaMulti + MaybeSend;
201+
R: FromLuaMulti;
210202

211203
/// Converts the object to a string in a human-readable format.
212204
///

src/userdata/object.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ use crate::value::Value;
88
use crate::Function;
99

1010
#[cfg(feature = "async")]
11-
use {
12-
crate::types::MaybeSend,
13-
futures_util::future::{self, Either, Future},
14-
};
11+
use crate::function::AsyncCallFuture;
1512

1613
impl ObjectLike for AnyUserData {
1714
#[inline]
@@ -38,9 +35,9 @@ impl ObjectLike for AnyUserData {
3835

3936
#[cfg(feature = "async")]
4037
#[inline]
41-
fn call_async<R>(&self, args: impl IntoLuaMulti) -> impl Future<Output = Result<R>> + MaybeSend
38+
fn call_async<R>(&self, args: impl IntoLuaMulti) -> AsyncCallFuture<R>
4239
where
43-
R: FromLuaMulti + MaybeSend,
40+
R: FromLuaMulti,
4441
{
4542
Function(self.0.copy()).call_async(args)
4643
}
@@ -54,13 +51,9 @@ impl ObjectLike for AnyUserData {
5451
}
5552

5653
#[cfg(feature = "async")]
57-
fn call_async_method<R>(
58-
&self,
59-
name: &str,
60-
args: impl IntoLuaMulti,
61-
) -> impl Future<Output = Result<R>> + MaybeSend
54+
fn call_async_method<R>(&self, name: &str, args: impl IntoLuaMulti) -> AsyncCallFuture<R>
6255
where
63-
R: FromLuaMulti + MaybeSend,
56+
R: FromLuaMulti,
6457
{
6558
self.call_async_function(name, (self, args))
6659
}
@@ -79,21 +72,17 @@ impl ObjectLike for AnyUserData {
7972
}
8073

8174
#[cfg(feature = "async")]
82-
fn call_async_function<R>(
83-
&self,
84-
name: &str,
85-
args: impl IntoLuaMulti,
86-
) -> impl Future<Output = Result<R>> + MaybeSend
75+
fn call_async_function<R>(&self, name: &str, args: impl IntoLuaMulti) -> AsyncCallFuture<R>
8776
where
88-
R: FromLuaMulti + MaybeSend,
77+
R: FromLuaMulti,
8978
{
9079
match self.get(name) {
91-
Ok(Value::Function(func)) => Either::Left(func.call_async(args)),
80+
Ok(Value::Function(func)) => func.call_async(args),
9281
Ok(val) => {
9382
let msg = format!("attempt to call a {} value (function '{name}')", val.type_name());
94-
Either::Right(future::ready(Err(Error::RuntimeError(msg))))
83+
AsyncCallFuture::error(Error::RuntimeError(msg))
9584
}
96-
Err(err) => Either::Right(future::ready(Err(err))),
85+
Err(err) => AsyncCallFuture::error(err),
9786
}
9887
}
9988

0 commit comments

Comments
 (0)