title: Function accept type argument
body: <p>I am working on a client server application where i send messages to / from each as protocol buffers using the rust crate prost.  currently i have a generic <code>send_message</code> function that can take any struct that implements the <code>prost::Message</code> trait.  I'm trying to create a similar function for reading a type that implements <code>prost::Message</code>.  I believe the issue is that <code>decode</code> which is a method for types that implement <code>prost::Message</code> is an associated function and not an instance method. the actual instance of the object that implements <code>prost::Message</code> is created within the function.</p>
  <p>Im wondering if there is a way to make a generic <code>read_message</code> function that can accept any type that implements <code>prost::Message</code> and use the associated function <code>decode</code> on that type.</p>
  <p>This is my current <code>send_message</code> function:</p>
  <pre><code>pub fn send_message&lt;M&gt;(message: M, stream: &amp;mut TcpStream) -&gt; io::Result&lt;()&gt;
  where
      M: Message,
  {
      let length = message.encoded_len() as i32;
      let mut buf: Vec&lt;u8&gt; = Vec::with_capacity(length as usize);
      message.encode(&amp;mut buf)?;
      stream.write_all(&amp;length.to_le_bytes())?;
      stream.write_all(&amp;buf)?;
      Ok(())
  }
  </code></pre>
  <p>and right now i have two <code>read_TYPE_message</code> functions:</p>
  <pre><code>pub fn read_response_message(stream: &amp;mut TcpStream) -&gt; io::Result&lt;message::Response&gt; {
      let mut len_buf = [0u8; 4];
      stream.read_exact(&amp;mut len_buf)?;
      let len = i32::from_le_bytes(len_buf);
      println!(&quot;Incoming message length: {}&quot;, len);
      let mut buf = vec![0u8; len as usize];
      stream.read_exact(&amp;mut buf)?;
      let response = message::Response::decode(&amp;mut buf.as_slice())?;
      Ok(response)
  }
  
  fn read_request_message(stream: &amp;mut TcpStream) -&gt; io::Result&lt;message::Request&gt; {
      let mut len_buf = [0u8; 4];
      stream.read_exact(&amp;mut len_buf)?;
      let len = i32::from_le_bytes(len_buf);
      println!(&quot;Incoming message length: {}&quot;, len);
      let mut buf = vec![0u8; len as usize];
      stream.read_exact(&amp;mut buf)?;
      let user_input = message::Request::decode(&amp;mut buf.as_slice())?;
      Ok(user_input)
  }
  </code></pre>
  <p>When i tried the below:</p>
  <pre><code>fn read_message&lt;M: Message&gt;(msg: M, stream: &amp;mut TcpStream) -&gt; io::Result&lt;M&gt; {
      let mut len_buf = [0u8; 4];
      stream.read_exact(&amp;mut len_buf)?;
      let len = i32::from_le_bytes(len_buf);
      println!(&quot;Incoming message length: {}&quot;, len);
      let mut buf = vec![0u8; len as usize];
      stream.read_exact(&amp;mut buf)?;
      let pb_msg = msg::decode(&amp;mut buf.as_slice())?;
      Ok(pb_msg)
  }
  </code></pre>
  <p>I get the error</p>
  <pre><code>failed to resolve: use of undeclared crate or module `msg` use of undeclared crate or module `msg`
  </code></pre>
  <p>i was hoping to be able use the function something like this:</p>
  <pre><code>let input = read_message(message::Request, &amp;mut stream)?;
  let input = read_message(message::Response, &amp;mut stream)?;
  </code></pre>