core/: Redesign upgrade::{write_one, write_with_len_prefix, read_one} (#2111)

1. Deprecating the `write_one` function

  Semantically, this function is a composition of `write_with_len_prefix` and
  `io.close()`. This represents a footgun because the `close` functionality is
  not obvious and only mentioned in the docs. Using this function multiple times
  on a single substream will produces hard to debug behaviour.

2. Deprecating `read_one` and `write_with_len_prefix` functions

3. Introducing `write_length_prefixed` and `read_length_prefixed`

- These functions are symmetric and do exactly what you would expect, just
  writing to the socket without closing
- They also have a symmetric interface (no more custom errors, just `io::Error`)

Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Thomas Eizinger
2021-07-04 00:23:10 +10:00
committed by GitHub
parent 4eb0659e4d
commit c1ef4bffd2
8 changed files with 116 additions and 38 deletions

View File

@ -186,7 +186,11 @@ where
let mut bytes = Vec::with_capacity(message.encoded_len());
message.encode(&mut bytes).expect("Vec<u8> provides capacity as needed");
upgrade::write_one(&mut io, &bytes).await
upgrade::write_length_prefixed(&mut io, bytes).await?;
io.close().await?;
Ok(())
}
async fn recv<T>(mut socket: T) -> io::Result<IdentifyInfo>
@ -195,7 +199,7 @@ where
{
socket.close().await?;
let msg = upgrade::read_one(&mut socket, 4096)
let msg = upgrade::read_length_prefixed(&mut socket, 4096)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
.await?;