Ambiguous associated type

I have this error when try to compile my code:

error[E0223]: ambiguous associated type
  --> src\ws\query\player\get_player.rs:36:66
   |
36 |     pub async fn get_player_name(&mut self, guid: u64) -> Result<Self::Response, Error> {
   |                                                                  ^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<get_player::Handler<'a> as Trait>::Response`

the code:

#[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>>,
}

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, guid: u64) -> Result<Self::Response, Error> {
        let query = Query {
            event: Self::event(),
            payload: Payload {
                id,
            },
        };

        let request = serde_json::to_string(&query).unwrap();

        match Self::handle(self.sender, self.receiver, request).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 why I cannot use just Self::Response here and what is the best way to fix ?

Just in case, I use it from here:

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)
    }
}

I think it's just because you're in an inherent impl block returning from a function with no trait bounds. I.e. there's no context tying that function to any particular trait. Return Result<String, Error> or follow the hint.

1 Like

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.