proxmox/tools/websocket: fix some clippy warnings

* reformat docs
* use &mut [u8] instead of Box<[u8]> (conversion can be done automatically)
* use:
    let foo = if condition { A } else { B };
  instead of matching on a boolean condition
* rename WaitingForData to Receiving so that not all
  variants of the enum end with Data

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Dominik Csapak 2020-07-17 14:17:09 +02:00 committed by Wolfgang Bumiller
parent eebacab282
commit 6f125bc70b

View File

@ -1,8 +1,7 @@
//! Websocket helpers //! Websocket helpers
//! //!
//! Provides methods to read and write from websockets //! Provides methods to read and write from websockets The reader and writer take a reader/writer
//! The reader and writer take a reader/writer with AsyncRead/AsyncWrite //! with AsyncRead/AsyncWrite respectively and provides the same
//! respectively and provides the same
use std::pin::Pin; use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
@ -113,7 +112,7 @@ impl OpCode {
} }
} }
fn mask_bytes(mask: Option<[u8; 4]>, data: &mut Box<[u8]>) { fn mask_bytes(mask: Option<[u8; 4]>, data: &mut [u8]) {
let mask = match mask { let mask = match mask {
Some([0,0,0,0]) | None => return, Some([0,0,0,0]) | None => return,
Some(mask) => mask, Some(mask) => mask,
@ -278,10 +277,7 @@ impl<W: AsyncWrite + Unpin> AsyncWrite for WebSocketWriter<W> {
) -> Poll<io::Result<usize>> { ) -> Poll<io::Result<usize>> {
let this = Pin::get_mut(self); let this = Pin::get_mut(self);
let frametype = match this.text { let frametype = if this.text { OpCode::Text } else { OpCode::Binary };
true => OpCode::Text,
false => OpCode::Binary,
};
if this.frame.is_none() { if this.frame.is_none() {
// create frame buf // create frame buf
@ -448,16 +444,15 @@ impl FrameHeader {
)); ));
} }
let mask = match mask_bit { let mask = if mask_bit {
true => { if len < mask_offset + 4 {
if len < mask_offset + 4 { return Ok(None);
return Ok(None);
}
let mut mask = [0u8; 4];
mask.copy_from_slice(&data[mask_offset as usize..payload_offset as usize]);
Some(mask)
} }
false => None, let mut mask = [0u8; 4];
mask.copy_from_slice(&data[mask_offset as usize..payload_offset as usize]);
Some(mask)
} else {
None
}; };
Ok(Some(FrameHeader { Ok(Some(FrameHeader {
@ -513,7 +508,7 @@ struct ReadResult<R> {
enum ReaderState<R> { enum ReaderState<R> {
NoData, NoData,
WaitingForData(Pin<Box<dyn Future<Output = io::Result<ReadResult<R>>> + Send + 'static>>), Receiving(Pin<Box<dyn Future<Output = io::Result<ReadResult<R>>> + Send + 'static>>),
HaveData, HaveData,
} }
@ -547,9 +542,9 @@ impl<R: AsyncReadExt + Unpin + Send + 'static> AsyncRead for WebSocketReader<R>
.map(move |len| ReadResult { len, reader, buffer }) .map(move |len| ReadResult { len, reader, buffer })
}; };
this.state = ReaderState::WaitingForData(future.boxed()); this.state = ReaderState::Receiving(future.boxed());
}, },
ReaderState::WaitingForData(ref mut future) => { ReaderState::Receiving(ref mut future) => {
match ready!(future.as_mut().poll(cx)) { match ready!(future.as_mut().poll(cx)) {
Ok(ReadResult { len, reader, buffer }) => { Ok(ReadResult { len, reader, buffer }) => {
this.reader = Some(reader); this.reader = Some(reader);