Expressing a generic TryFrom interface?

I have a protocol parser that for simplicity I'm going to say reads data from the network into a HashMap<String, String>. I have a recv_map() that the application can use to translate the HashMap into a parsed type:

fn recv_map<C, F, T>(
  frmio: &mut Framed<C, Codec>,
  f: F
) -> Result<T, Error>
where
  C: AsyncRead + AsyncWrite + Unpin,
  F: FnOnce(HashMap<String, String>) -> Result<T, Error>
{
  let map = recv(frmio).await?;
  f(map)
}

Used as:

let mydata = recv_map(&mut frmio, |m| {
  // .. do conversion ..
})?;

This works fine, but I want a similar function that uses T's TryFrom<HashMap<String, String>> (or TryInto) implementation, but I'm unable to express express these bounds to make the compiler happy.

fn recv_into<C, T>(
  frmio: &mut Framed<C, Codec>
) -> Result<T, Error>
where
  C: AsyncRead + AsyncWrite + Unpin,
  // ?
{
  let map = recv_map(frmio, |map| map.try_into()).await?;
}

Playground

This was apparently not particularly difficult (at least, the code compiles now). T'was a rare case where I was led astray by the compiler's suggestions.

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.