IETF RFC 3986和RFC 6874的Rust URI解析器。type_alias_impl_trait特性还未稳定),想自定义命名Future需要依赖box和dyn特性;rename-future提供了一种解决命名impl Future的思路:impl Service<Request> for AsyncFnService {
type Response = ();
type Error = ();
type Future = FooAsyncFnFuture; // simply use renamed Future! no extra costs!
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request) -> Self::Future {
foo()
}
}
#[rename_future(FooAsyncFnFuture)]
async fn foo() -> usize {
10
}
#[rename_future(AsyncFnFuture)]
async fn async_fn() -> usize {
10
}
pub const fn __internal_async_fn_sof<F, Fut>(_: &F) -> usize
where
F: Fn() -> Fut,
{
std::mem::size_of::<Fut>()
}
pub const fn __internal_async_fn_aof<F, Fut>(_: &F) -> usize
where
F: Fn() -> Fut,
{
std::mem::align_of::<Fut>()
}
struct AsyncFnFuture(
(
[u8; __internal_async_fn_sof::<_, _>(&__internal_async_fn)],
rename_future::Align<{ __internal_async_fn_aof::<_, _>(&__internal_async_fn) }>,
),
std::marker::PhantomData<()>,
std::marker::PhantomPinned,
);
async fn __internal_async_fn() -> usize {
10
}
fn async_fn() -> AsyncFnFuture {
impl std::future::Future for AsyncFnFuture {
type Output = usize;
fn poll(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
fn call_poll<__T, __Q, __F>(
_: &__T,
fut: std::pin::Pin<&mut __F>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<__F::Output>
where
__T: Fn() -> __Q,
__Q: std::future::Future<Output = __F::Output>,
__F: std::future::Future,
{
let fut: std::pin::Pin<&mut __Q> = unsafe { std::mem::transmute(fut) };
fut.poll(cx)
}
call_poll::<_, _, _>(&__internal_async_fn, self, cx)
}
}
unsafe { std::mem::transmute(__internal_async_fn()) }
}
这个项目还没有经过很好的测试!不要在生产代码中使用这个库!
feature flags在它们之间进行简单切换。require函数,因为标准实现中没有这个功能。唯一值得注意的缺失功能是只读表和沙盒功能,这些功能将在下一个版本中得到支持。use mlua::prelude::*;
fn main() -> LuaResult<()> {
let lua = Lua::new();
let map_table = lua.create_table()?;
map_table.set(1, "one")?;
map_table.set("two", 2)?;
lua.globals().set("map_table", map_table)?;
lua.load("for k,v in pairs(map_table) do print(k,v) end").exec()?;
Ok(())
}
cargo run [example.lisp]
From 日报小组 odd-cat
原文作者:Rust语言中文社区;如有侵权,请联系管理员删除。