Add stab tag for async functions

This commit is contained in:
lumi 2025-06-26 22:58:30 +10:00
parent 679ffed807
commit d2e06c9a70
Signed by: luaneko
GPG Key ID: 406809B8763FF07A
3 changed files with 20 additions and 8 deletions

View File

@ -27,7 +27,7 @@ pub enum Error {
/// IP or socket address syntax error. /// IP or socket address syntax error.
#[error("{0}")] #[error("{0}")]
InvalidAddr(#[from] AddrParseError), InvalidAddr(#[from] AddrParseError),
/// Socket was already converted and cannot be used. /// Socket was already converted and cannot be used anymore.
#[error("socket was already converted")] #[error("socket was already converted")]
SocketConsumed, SocketConsumed,
} }
@ -141,7 +141,7 @@ impl lb_netlib {
Ok(Some(TcpSocket::new_v6()?).into()) Ok(Some(TcpSocket::new_v6()?).into())
} }
pub extern "Lua" fn bind_tcp(&self, addr: any, port: any) -> Result<lb_tcpsocket> { pub async extern "Lua" fn bind_tcp(&self, addr: any, port: any) -> Result<lb_tcpsocket> {
let addr = self.socketaddr(addr, port); let addr = self.socketaddr(addr, port);
let socket; let socket;
if addr.ip().is_v6() { if addr.ip().is_v6() {
@ -153,7 +153,7 @@ impl lb_netlib {
socket socket
} }
pub extern "Lua" fn connect_tcp(&self, addr: any, port: any) -> Result<lb_tcpstream> { pub async extern "Lua" fn connect_tcp(&self, addr: any, port: any) -> Result<lb_tcpstream> {
let addr = self.socketaddr(addr, port); let addr = self.socketaddr(addr, port);
let socket; let socket;
if addr.ip().is_v6() { if addr.ip().is_v6() {
@ -164,7 +164,7 @@ impl lb_netlib {
socket.connect(addr) socket.connect(addr)
} }
pub extern "Lua" fn listen_tcp(&self, addr: any, port: any) -> Result<lb_tcplistener> { pub async extern "Lua" fn listen_tcp(&self, addr: any, port: any) -> Result<lb_tcplistener> {
self.bind_tcp(addr, port).listen(1024) self.bind_tcp(addr, port).listen(1024)
} }
} }

View File

@ -35,7 +35,7 @@ impl lb_tasklib {
sleep(Duration::from_secs_f64(ms / 1000.)).await; sleep(Duration::from_secs_f64(ms / 1000.)).await;
} }
pub extern "Lua" fn spawn(&self, f: function, ...) { pub extern "Lua" fn spawn(&self, f: function, ...) -> lb_task {
// pack the function and its arguments into a table and pass its ref to rust. // pack the function and its arguments into a table and pass its ref to rust.
// //
// this table is used from rust-side to call the function with its args, and it's also // this table is used from rust-side to call the function with its args, and it's also
@ -82,7 +82,7 @@ pub struct lb_task {
#[metatype] #[metatype]
impl lb_task { impl lb_task {
pub extern "Lua" fn r#await(&self) -> many { pub async extern "Lua" fn r#await(&self) -> many {
self.__await(); self.__await();
let ret = __registry[self.__ref]; let ret = __registry[self.__ref];
__tunpack(ret, 1, ret.n) __tunpack(ret, 1, ret.n)

View File

@ -285,7 +285,7 @@ fn get_ffi_functions(imp: &mut ItemImpl) -> Result<Vec<FfiFunction>> {
let attrs = parse_ffi_function_attrs(&mut func.attrs)?; let attrs = parse_ffi_function_attrs(&mut func.attrs)?;
attrs.metamethod.map(|mm| document_metamethod(func, mm)); attrs.metamethod.map(|mm| document_metamethod(func, mm));
func.sig.asyncness.is_some().then(|| document_async(func));
document_ffi_function(func); document_ffi_function(func);
funcs.push(FfiFunction { funcs.push(FfiFunction {
@ -573,6 +573,12 @@ fn get_lua_functions(imp: &mut ItemImpl) -> Result<Vec<LuaFunction>> {
"cannot be generic" "cannot be generic"
); );
syn_assert!(
func.sig.constness.is_none(),
func.sig.constness,
"cannot be const"
);
let mut params: Vec<_> = func let mut params: Vec<_> = func
.sig .sig
.inputs .inputs
@ -593,7 +599,7 @@ fn get_lua_functions(imp: &mut ItemImpl) -> Result<Vec<LuaFunction>> {
let attrs = parse_lua_function_attrs(&mut func.attrs)?; let attrs = parse_lua_function_attrs(&mut func.attrs)?;
attrs.metamethod.map(|mm| document_metamethod(func, mm)); attrs.metamethod.map(|mm| document_metamethod(func, mm));
func.sig.asyncness.is_some().then(|| document_async(func));
document_lua_function(func); document_lua_function(func);
funcs.push(LuaFunction { funcs.push(LuaFunction {
@ -807,6 +813,12 @@ fn document_lua_function(func: &mut ImplItemFn) {
])); ]));
} }
fn document_async(func: &mut ImplItemFn) {
func.attrs.insert(0, parse_quote!(#[doc =
r#"<span class="stab" title="This function is asynchronous." style="float: right; background: #ebf5ff; margin-left: 3px; padding-left: 5px; padding-right: 5px;">Async</span>"#
]));
}
fn document_metamethod(func: &mut ImplItemFn, method: Metamethod) { fn document_metamethod(func: &mut ImplItemFn, method: Metamethod) {
let s = match method { let s = match method {
Metamethod::Eq => "This is a metamethod which is called by the `==` operator.".into(), Metamethod::Eq => "This is a metamethod which is called by the `==` operator.".into(),