0

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?

3
  • 1
    Please remove the pub from the auth_login as the error suggests. It is correct that visibility modifiers are not allowed within traits. Then show the error without them - and please use cargo check and paste that output instead of whatever your IDE shows. Commented Jan 23 at 4:23
  • Is AuthHandler pub? Commented Jan 23 at 4:26
  • All trait members are implicitly visible to anything that can see the trait itself; visibility modifiers are therefore prohibited on trait members. If you're getting some error after removing pub you should post that error, because there is an unrelated problem that needs to be addressed. Commented Jan 23 at 6:03

1 Answer 1

1

Remove the pub inside the Auth trait, but not the one on the outside.

i.e.

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

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

You can see a working example on the Playground

If you add back the pubs inside the trait or impl you get:

error[E0449]: visibility qualifiers are not permitted here
  --> src/lib.rs:12:9
   |
12 |         pub async fn auth_login(_req: (), load: (), db_pool: Vec<u8>) -> impl Responder;
   |         ^^^ help: remove the qualifier
   |
   = note: trait items always share the visibility of their trait

And if you remove the pub on the outside of the trait you get:

error[E0603]: trait `Auth` is private
  --> src/lib.rs:27:23
   |
27 |     use super::part1::Auth;
   |                       ^^^^ private trait
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.