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, I get a syntax error.
Here is the code I'm using to define the trait and implement it:
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
}
}
The line pub async fn auth_login 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
In another file, I import the trait like this:
lib.rs (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);
This error seems to indicate that I cannot use the pub modifier on methods within a trait. If I remove it, the method is no longer accessible outside the file, and when I try to call auth::AuthHandler::auth_login, the method is not recognized unless I add pub, which then triggers the error.
How can I make both the trait and its method public and accessible outside the file without getting this error?
pubfrom theauth_loginas the error suggests. It is correct that visibility modifiers are not allowed within traits. Then show the error without them - and please usecargo checkand paste that output instead of whatever your IDE shows.AuthHandlerpub?pubyou should post that error, because there is an unrelated problem that needs to be addressed.