salvo-rs / salvo

A powerful web framework built with a simplified design.
https://salvo.rs
Apache License 2.0
3.26k stars 197 forks source link

使用#[salvo::oapi::endpoint]时,遇到返回std::result::Result<(), impl salvo::Writer>的方法时报错 #908

Closed 3517277 closed 1 day ago

3517277 commented 5 days ago

rust version:1.81 salvo verion:1.72

#[salvo::oapi::endpoint]
    async fn test2(
        __proc_macro_depot: &mut salvo::Depot,
    ) -> std::result::Result<(), impl salvo::Writer> {
        use kitsen_server::core::authorities::AuthDetails;
        use kitsen_server::core::authorities::AuthoritiesCheck;
        let auth_details = __proc_macro_depot
            .obtain::<AuthDetails<String>>()
            .expect("Can't not found AuthDetails in Depot,Did you sure add AuthDetails in Depot?");
        if auth_details.has_authority("333") {
            Ok(async move {
                {
                    println!("222");
                };
            }
            .await)
        } else {
            Err(salvo::prelude::StatusCode::FORBIDDEN)
        }
    }

,这里会直接报错 'impl Traitis not allowed in paths [E0562] Note:impl Trait` is only allowed in arguments and return types of functions and methods。 查看代码,好像endpoint宏展开时把方法作为参数传递出现的问题? 微信截图_20240915222259

3517277 commented 5 days ago
#[salvo::oapi::endpoint]
    async fn test3() -> std::result::Result<(), impl salvo::Writer> {
        if false {
            Ok(async move {
                {
                    println!("222");
                };
            }.await)
    } else {
            Err(salvo::prelude::StatusCode::FORBIDDEN)
        }
    }

其实,这样就行了,其它代码多余的

chrislearn commented 5 days ago

看了一下,应该是这句话引起的:

 <std::result::Result<(), impl salvo::Writer> as salvo::oapi::EndpointOutRegister>::register(
            components, operation,
        )

似乎 Rust 现在不允许这么表示。

3517277 commented 4 days ago

看了一下,应该是这句话引起的:

 <std::result::Result<(), impl salvo::Writer> as salvo::oapi::EndpointOutRegister>::register(
            components, operation,
        )

似乎 Rust 现在不允许这么表示。

提示确实说只能用于方法返回,看来只能放弃使用impl trait了