Rename test function as_write_vec -> into_written_bytes

Tests use `Response::as_write_vec` to inspect the outgoing HTTP/1.1
request line and headers. The current version has two problems:

1. Called `as_write_vec` when it actually returns a `&[u8]`.
2. Inspects/uses the `Response::stream` without consuming `Response`.

The first problem is trivial, but the second is subtle. Currently all
calls on `Response` that works with the internal `Response::stream`
consumes `self` (`into_string`, `into_reader`).

`Response` is by itself `Send + Sync`, and must be so because the
nested Stream is `Read + Write + Send + Sync`. However for
implementors of `TLSStream`, it would be nice to relax the `Sync`
requirement.

Assumption: If all fields in Response are `Sync` except
`Response::stream`, but any access to `stream` consumes `Response`, we
can consider the entire `Response` `Sync`.

This assumption can help us relax the `TlsStream` `Sync` requirement
in a later PR.
This commit is contained in:
Martin Algesten
2022-01-29 11:06:29 +01:00
parent 6e5041044b
commit 219b5edf9e
5 changed files with 27 additions and 22 deletions

View File

@@ -520,8 +520,9 @@ impl Response {
}
#[cfg(test)]
pub fn as_write_vec(&self) -> &[u8] {
self.stream.as_write_vec()
pub fn into_written_bytes(self) -> Vec<u8> {
// Deliberately consume `self` so that any access to `self.stream` must be non-shared.
self.stream.written_bytes()
}
#[cfg(test)]