Skip to content

mismatched types and web:Data<> problem when called from different file #2341

@Leanerz

Description

@Leanerz

Hi all,

I have rust folder like so:

main.rs
api/
- user_api/
-- mod.rs
-- routes.rs
-- user_api.rs

mod.rs:

mod routes;
mod user_api;

pub use routes::init_routes;
pub use user_api::{AddUsers, testOnly};

This function is inside: user_api.rs file:

    pub async fn AddUsers(cgrpc: APIClient<tonic::transport::Channel>, uid: u64) -> Result<TResponse<UserResponse>, Status> {
            let request = tonic::Request::new(UserUID {
                   uid
            });
        

            let response = cgrpc.clone().nuser(request).await;

            println!("RESPONSE={:?}", response);

            response
    }

This one inside routes.rs:

#[post("/adduser")]
async fn add_user(_req: HttpRequest, cgrpc: web::Data<APIClient<tonic::transport::Channel>>) -> HttpResponse {

    user_api::testOnly(1);

    match AddUsers(cgrpc, 1) {
        Ok(result) => HttpResponse::Ok().json(result.unwrap()),
        Err(err) => HttpResponse::Ok().json(err),
    }
}

But I get this error for cgrpc:

mismatched types

expected struct `api::user_api::user_api::client_grpc_api::gateway_client::APIClient`, found struct `actix_web::web::Data`

note: expected struct `api::user_api::user_api::client_grpc_api::gateway_client::APIClient<tonic::transport::Channel>`
         found struct `actix_web::web::Data<api::user_api::routes::client_grpc_api::gateway_client::APIClient<tonic::transport::Channel>>`rustc(E0308)

Inside main.rs:

    ...
    HttpServer::new(move || {
        App::new()
            .data(cgrpc.clone())
            .wrap(middleware::Compress::new(ContentEncoding::Br))
            .wrap(middleware::Logger::default())
            .service(web::scope("/user").configure(api::user_api::init_routes))
    })
    .bind("127.0.0.1:80")?
    .run()
    .await

I can copy paste all functions inside user_api.rs to routes.rs and use directly:

web::Data<APIClient<tonic::transport::Channel>>

But for organization purpose, I need it like so, your help will be appreciated.

Thank you !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions