No function or associated item named `new` found for struct

I implemented Registry which should return another registries, but got an error when try to call a function from it:

error[E0599]: no function or associated item named `new` found for struct `get_players::Handler` in the current scope
  --> src\ws\query\player\mod.rs:30:31
   |
30 |         get_players::Handler::new(self.sender, self.receiver)
   |                               ^^^ function or associated item not found in `get_players::Handler<'_>`
   |
  ::: src\ws\query\player\get_players.rs:20:1
   |
20 | pub struct Handler<'a> {
   | ---------------------- function or associated item `new` not found for this

This is my code:

pub struct QueryRegistry;

impl QueryRegistry {
    pub fn players<'a>(
        sender: &'a mut WSSender<Compat<TcpStream>>,
        receiver: &'a mut WSReceiver<Compat<TcpStream>>
    ) -> PlayerQueryRegistry<'a> {
        PlayerQueryRegistry::new(sender, receiver)
    }
}

pub struct PlayerQueryRegistry<'a> {
    sender: &'a mut WSSender<Compat<TcpStream>>,
    receiver: &'a mut WSReceiver<Compat<TcpStream>>,
}

impl<'a> PlayerQueryRegistry<'a> {
    pub fn new(
        sender: &'a mut WSSender<Compat<TcpStream>>,
        receiver: &'a mut WSReceiver<Compat<TcpStream>>
    ) -> Self {
        Self {
            sender,
            receiver,
        }
    }

    pub fn get_player(&mut self) -> get_player::Handler {
        get_player::Handler::new(self.sender, self.receiver)
    }

    pub fn get_players(&mut self) -> get_players::Handler {
        get_players::Handler::new(self.sender, self.receiver)
    }

    pub fn sync_player(&mut self) -> sync_player::Handler {
        sync_player::Handler::new(self.sender, self.receiver)
    }
}

and example of Handler:

#[async_trait]
pub trait QueryHandler {
    type Response: serde::de::DeserializeOwned;

    fn event() -> String;

    async fn handle(
        sender: &mut WSSender<Compat<TcpStream>>,
        receiver: &mut WSReceiver<Compat<TcpStream>>,
        request: String
    ) -> Result<Self::Response, Error> {
        sender.send_text(request).await.unwrap();

        let mut response = Vec::new();
        receiver.receive_data(&mut response).await.unwrap();

        let result = serde_json::from_slice(&response).unwrap();

        Ok(result)
    }
}

#[derive(Serialize, Deserialize, Debug)]
struct Payload {
    id: u64,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Query {
    event: String,
    payload: Payload,
}

pub struct Handler<'a> {
    sender: &'a mut Sender<Compat<TcpStream>>,
    receiver: &'a mut Receiver<Compat<TcpStream>>,
}

#[async_trait]
impl<'a> Handler<'a> {
    pub fn new(
        sender: &'a mut Sender<Compat<TcpStream>>,
        receiver: &'a mut Receiver<Compat<TcpStream>>
    ) -> Self {
        Self {
            sender,
            receiver,
        }
    }

    pub async fn get_player_name(&mut self, id: u64) -> Result<Self::Response, Error> {
        let query = Query {
            event: Self::event(),
            payload: Payload {
                id,
            },
        };

        serde_json::to_string(&query).unwrap();

        match Self::handle(self.sender, self.receiver, payload).await {
            Ok(name) => Ok(name),
            Err(err) => Err(err),
        }
    }
}

#[async_trait]
impl QueryHandler for Handler<'_> {
    type Response = String;

    fn event() -> String {
        String::from("player.get")
    }
}

Could somebody explain what I did wrong ? Why ::new method not found (considering I can forward to it with my IDE so imports should be OK) ?

You probably have an error like this, too:

error: expected a trait impl
 --> src/main.rs:5:1
  |
5 | #[async_trait]
  | ^^^^^^^^^^^^^^
  |

Playground

Remove #[async_trait] from impl<'a> Handler<'a>, and the code should compile.

1 Like

works ! thank you

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.