I'm working on a Rust project and have a trait called Auth with an asynchronous method auth_login. The issue I'm facing is that when I try to make the method accessible from other modules using pub, the compiler throws the following error:

remove the qualifier
auth.rs(41, 2): original diagnostic
Syntax Error: Unnecessary visibility qualifier
visibility qualifiers are not permitted here
trait items always share the visibility of their trait

This error seems to indicate that I cannot use the pub modifier on methods within a trait, but if I remove it, the method is no longer accessible outside the file.

Here is the code I'm using to import. auth.rs:

// Trait with async method
pub trait Auth {
    pub async fn auth_login(_req: HttpRequest, load: Payload, db_pool: Data<PgPool>) -> impl Responder;
}

// Implementation of the trait
impl Auth for AuthHandler {
    pub async fn auth_login(_req: HttpRequest, load: Payload, db_pool: Data<PgPool>) -> impl Responder {
        // Authentication logic here
    }
}

In another file, I import it like this, from lib.rs (the main file):

mod auth; // Import the module where Auth is defined
use auth::{Auth}; // Use the trait

// Calling the function
let result = auth::AuthHandler::auth_login(req, load, db_pool);

The error occurs on the line where I call auth::AuthHandler::auth_login. The method is not recognized unless I add pub, but then the error is triggered.

How can I make both the trait and its method public and accessible outside the file without getting this error?

NOTE: I'm relatively new to Rust, so I'm open to suggestions on how to achieve an "interface"-like contract, ensuring that all required methods and parameters are properly implemented. Any guidance on how to structure this in a more Rust-idiomatic way is appreciated.

Source: View source