Bounds; need explicit TryFrom in some cases?

⚓ rust    📅 2025-05-09    👤 surdeus    👁️ 1      

surdeus

I have a function that sends a request and returns a reply. The request and reply types are converted to/from an underlying line-based protocol using regular TryFrom.

pub async fn create_account<C, P>(
  frmio: &mut Framed<C, blather::Codec>,
  req: MkAccReq<P>
) -> Result<i64, Error>
where
  C: AsyncRead + AsyncWrite + Unpin + Send,
  P: Hash + Eq + AsRef<str>
{
  let reply: MkAccReply = sendrecv(frmio, req).await?;
  Ok(reply.id)
}

pub async fn list_accounts<C, P>(
  frmio: &mut Framed<C, blather::Codec>
) -> Result<Vec<AccInfo<P>>, Error>
where
  C: AsyncRead + AsyncWrite + Unpin + Send,
  LsAccReply<P>: TryFrom<Params, Error = ReqErr>  // <-- ?
{
  let req = LsAccReq;
  let reply: LsAccReply<P> = sendrecv(frmio, req).await?;
  Ok(reply.lst)
}

My question concerns the second bound in list_accounts(). That LsAccReply<P>: TryFrom<Params, Error = ReqErr> is not something I came up with myself. The compiler claimed that there exists no TryFrom<Params> implementation for LsAccReply<P> (which isn't true), and suggested that I add the bound. When I did, it accepted the code -- but I don't understand why.

No such bound is needed in create_account(). Is it related to the <P> in the return type (which isn't present in MkAccReply)?

5 posts - 2 participants

Read full topic

🏷️ rust_feed