Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary intermediate allocations #269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,7 @@ impl<'u> ClientBuilder<'u> {
{
// send request
let resource = self.build_request();
let data = format!("GET {} {}\r\n{}\r\n", resource, self.version, self.headers);
stream.write_all(data.as_bytes())?;
write!(stream, "GET {} {}\r\n{}\r\n", resource, self.version, self.headers)?;

// wait for a response
let mut reader = BufReader::new(stream);
Expand Down
4 changes: 2 additions & 2 deletions src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ impl fmt::Display for WebSocketOtherError {
WebSocketOtherError::ProtocolError(e) => write!(fmt, "WebSocketError: {}", e),
#[cfg(any(feature = "sync-ssl", feature = "async-ssl"))]
WebSocketOtherError::TlsHandshakeFailure => {
write!(fmt, "WebSocketError: {}", "TLS Handshake failure")
fmt.write_str("WebSocketError: TLS Handshake failure")
}
#[cfg(any(feature = "sync-ssl", feature = "async-ssl"))]
WebSocketOtherError::TlsHandshakeInterruption => {
write!(fmt, "WebSocketError: {}", "TLS Handshake interrupted")
fmt.write_str("WebSocketError: TLS Handshake interrupted")
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ where
{
fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
fmt.debug_struct("InvalidConnection")
.field("stream", &String::from("..."))
.field("parsed", &String::from("..."))
.field("buffer", &String::from("..."))
.field("stream", &"...")
.field("parsed", &"...")
.field("buffer", &"...")
.field("error", &self.error)
.finish()
}
Expand Down
7 changes: 1 addition & 6 deletions src/server/upgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,7 @@ where

#[cfg(feature = "sync")]
fn send(&mut self, status: StatusCode) -> io::Result<()> {
let data = format!(
"{} {}\r\n{}\r\n",
self.request.version, status, self.headers
);
self.stream.write_all(data.as_bytes())?;
Ok(())
write!(self.stream, "{} {}\r\n{}\r\n", self.request.version, status, self.headers)
}

#[doc(hidden)]
Expand Down
9 changes: 4 additions & 5 deletions websocket-base/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ impl FromStr for WebSocketAccept {
impl WebSocketAccept {
/// Create a new WebSocketAccept from the given WebSocketKey
pub fn new(key: &WebSocketKey) -> WebSocketAccept {
let serialized = key.serialize();
let mut concat_key = String::with_capacity(serialized.len() + 36);
concat_key.push_str(&serialized[..]);
concat_key.push_str(MAGIC_GUID);
let hash = Sha1::digest(concat_key.as_bytes());
let mut serialized = key.serialize();
serialized.reserve_exact(MAGIC_GUID.len());
serialized.push_str(MAGIC_GUID);
let hash = Sha1::digest(serialized.as_bytes());
WebSocketAccept(hash.into())
}
/// Return the Base64 encoding of this WebSocketAccept
Expand Down
2 changes: 1 addition & 1 deletion websocket-base/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl<'a> ws::dataframe::DataFrame for Message<'a> {
let mut buf = Vec::with_capacity(2 + self.payload.len());
buf.write_u16::<BigEndian>(reason)
.expect("failed to write close code in take_payload");
buf.append(&mut self.payload.into_owned());
buf.extend_from_slice(&self.payload);
buf
} else {
self.payload.into_owned()
Expand Down