Skip to content

Commit

Permalink
clippy: fix some pedantic warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
BiagioFesta committed Oct 5, 2023
1 parent 4a2fd11 commit 5253688
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 62 deletions.
2 changes: 1 addition & 1 deletion wtransport-proto/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl<'a, 'b> BufferReaderChild<'a, 'b> {
pub fn commit(self) {
self.parent
.skip(self.reader.offset())
.expect("Child offset is bounded to parent")
.expect("Child offset is bounded to parent");
}

#[inline(always)]
Expand Down
4 changes: 2 additions & 2 deletions wtransport-proto/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,6 @@ mod qpack_error_codes {
mod wt_error_codes {
use crate::varint::VarInt;

pub const WEBTRANSPORT_BUFFERED_STREAM_REJECTED: VarInt = VarInt::from_u32(0x3994bd84);
pub const WEBTRANSPORT_SESSION_GONE: VarInt = VarInt::from_u32(0x170d7b68);
pub const WEBTRANSPORT_BUFFERED_STREAM_REJECTED: VarInt = VarInt::from_u32(0x3994_bd84);
pub const WEBTRANSPORT_SESSION_GONE: VarInt = VarInt::from_u32(0x170d_7b68);
}
8 changes: 4 additions & 4 deletions wtransport-proto/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,10 @@ impl<'a> Frame<'a> {
/// Panics if the `payload` size if greater than [`VarInt::MAX`].
fn new(kind: FrameKind, payload: Cow<'a, [u8]>, session_id: Option<SessionId>) -> Self {
if let FrameKind::Exercise(id) = kind {
debug_assert!(FrameKind::is_id_exercise(id))
debug_assert!(FrameKind::is_id_exercise(id));
} else if let FrameKind::WebTransport = kind {
debug_assert!(payload.is_empty());
debug_assert!(session_id.is_some())
debug_assert!(session_id.is_some());
}

assert!(payload.len() <= VarInt::MAX.into_inner() as usize);
Expand Down Expand Up @@ -590,7 +590,7 @@ mod tests {

#[test]
fn unknown_frame() {
let buffer = Frame::serialize_any(VarInt::from_u32(0x424242), b"This is a test payload");
let buffer = Frame::serialize_any(VarInt::from_u32(0x0042_4242), b"This is a test payload");

assert!(matches!(
Frame::read(&mut buffer.as_slice()).unwrap(),
Expand All @@ -600,7 +600,7 @@ mod tests {

#[tokio::test]
async fn unknown_frame_async() {
let buffer = Frame::serialize_any(VarInt::from_u32(0x424242), b"This is a test payload");
let buffer = Frame::serialize_any(VarInt::from_u32(0x0042_4242), b"This is a test payload");

assert!(matches!(
Frame::read_async(&mut buffer.as_slice()).await,
Expand Down
11 changes: 6 additions & 5 deletions wtransport-proto/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ pub struct QStreamId(VarInt);
impl QStreamId {
/// The largest quarter stream id.
// SAFETY: value is less than max varint
pub const MAX: QStreamId = unsafe { Self(VarInt::from_u64_unchecked(1152921504606846975)) };
pub const MAX: QStreamId =
unsafe { Self(VarInt::from_u64_unchecked(1_152_921_504_606_846_975)) };

/// Creates a quarter stream id from its corresponding [`SessionId`]
#[inline(always)]
Expand Down Expand Up @@ -281,8 +282,8 @@ impl TryFrom<u8> for StatusCode {
type Error = InvalidStatusCode;

fn try_from(value: u8) -> Result<Self, Self::Error> {
if value as u16 >= Self::MIN.0 && value as u16 <= Self::MAX.0 {
Ok(Self(value as u16))
if u16::from(value) >= Self::MIN.0 && u16::from(value) <= Self::MAX.0 {
Ok(Self(u16::from(value)))
} else {
Err(InvalidStatusCode)
}
Expand All @@ -305,7 +306,7 @@ impl TryFrom<u32> for StatusCode {
type Error = InvalidStatusCode;

fn try_from(value: u32) -> Result<Self, Self::Error> {
if value >= Self::MIN.0 as u32 && value <= Self::MAX.0 as u32 {
if value >= u32::from(Self::MIN.0) && value <= u32::from(Self::MAX.0) {
Ok(Self(value as u16))
} else {
Err(InvalidStatusCode)
Expand All @@ -317,7 +318,7 @@ impl TryFrom<u64> for StatusCode {
type Error = InvalidStatusCode;

fn try_from(value: u64) -> Result<Self, Self::Error> {
if value >= Self::MIN.0 as u64 && value <= Self::MAX.0 as u64 {
if value >= u64::from(Self::MIN.0) && value <= u64::from(Self::MAX.0) {
Ok(Self(value as u16))
} else {
Err(InvalidStatusCode)
Expand Down
2 changes: 1 addition & 1 deletion wtransport-proto/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl SessionResponse {
K: ToString,
V: ToString,
{
self.0.insert(key, value)
self.0.insert(key, value);
}

/// Returns the whole headers associated with the request.
Expand Down
4 changes: 2 additions & 2 deletions wtransport-proto/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,6 @@ mod setting_ids {
pub const SETTINGS_QPACK_BLOCKED_STREAMS: VarInt = VarInt::from_u32(0x07);
pub const SETTINGS_ENABLE_CONNECT_PROTOCOL: VarInt = VarInt::from_u32(0x08);
pub const SETTINGS_H3_DATAGRAM: VarInt = VarInt::from_u32(0x33);
pub const SETTINGS_ENABLE_WEBTRANSPORT: VarInt = VarInt::from_u32(0x2b603742);
pub const SETTINGS_WEBTRANSPORT_MAX_SESSIONS: VarInt = VarInt::from_u32(0xc671706a);
pub const SETTINGS_ENABLE_WEBTRANSPORT: VarInt = VarInt::from_u32(0x2b60_3742);
pub const SETTINGS_WEBTRANSPORT_MAX_SESSIONS: VarInt = VarInt::from_u32(0xc671_706a);
}
16 changes: 8 additions & 8 deletions wtransport-proto/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ pub mod biremote {
Err(frame::IoReadError::IO(io_error)) => {
if matches!(io_error, bytes::IoReadError::UnexpectedFin) {
return Err(IoReadError::H3(ErrorCode::Frame));
} else {
return Err(IoReadError::IO(io_error));
}

return Err(IoReadError::IO(io_error));
}
}
}
Expand Down Expand Up @@ -319,9 +319,9 @@ pub mod bilocal {
Err(frame::IoReadError::IO(io_error)) => {
if matches!(io_error, bytes::IoReadError::UnexpectedFin) {
return Err(IoReadError::H3(ErrorCode::Frame));
} else {
return Err(IoReadError::IO(io_error));
}

return Err(IoReadError::IO(io_error));
}
}
}
Expand Down Expand Up @@ -642,9 +642,9 @@ pub mod uniremote {
Err(frame::IoReadError::IO(io_error)) => {
if matches!(io_error, bytes::IoReadError::UnexpectedFin) {
return Err(IoReadError::H3(ErrorCode::Frame));
} else {
return Err(IoReadError::IO(io_error));
}

return Err(IoReadError::IO(io_error));
}
}
}
Expand Down Expand Up @@ -965,9 +965,9 @@ pub mod session {
Err(frame::IoReadError::IO(io_error)) => {
if matches!(io_error, bytes::IoReadError::UnexpectedFin) {
return Err(IoReadError::H3(ErrorCode::Frame));
} else {
return Err(IoReadError::IO(io_error));
}

return Err(IoReadError::IO(io_error));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions wtransport-proto/src/stream_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,13 @@ mod tests {

#[test]
fn read_eof() {
let buffer = StreamHeader::serialize_any(VarInt::from_u32(0x424242));
let buffer = StreamHeader::serialize_any(VarInt::from_u32(0x0042_4242));
assert!(StreamHeader::read(&mut &buffer[..buffer.len() - 1]).is_none());
}

#[tokio::test]
async fn read_eof_async() {
let buffer = StreamHeader::serialize_any(VarInt::from_u32(0x424242));
let buffer = StreamHeader::serialize_any(VarInt::from_u32(0x0042_4242));

for len in 0..buffer.len() {
let result = StreamHeader::read_async(&mut &buffer[..len]).await;
Expand Down Expand Up @@ -428,7 +428,7 @@ mod tests {

#[test]
fn unknown_stream() {
let buffer = StreamHeader::serialize_any(VarInt::from_u32(0x424242));
let buffer = StreamHeader::serialize_any(VarInt::from_u32(0x0042_4242));

assert!(matches!(
StreamHeader::read(&mut buffer.as_slice()).unwrap(),
Expand All @@ -438,7 +438,7 @@ mod tests {

#[tokio::test]
async fn unknown_stream_async() {
let buffer = StreamHeader::serialize_any(VarInt::from_u32(0x424242));
let buffer = StreamHeader::serialize_any(VarInt::from_u32(0x0042_4242));

assert!(matches!(
StreamHeader::read_async(&mut buffer.as_slice()).await,
Expand Down
23 changes: 15 additions & 8 deletions wtransport-proto/src/varint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct VarInt(u64);

impl VarInt {
/// The largest value that can be represented by this integer type.
pub const MAX: Self = Self(4611686018427387903);
pub const MAX: Self = Self(4_611_686_018_427_387_903);

/// The smallest value that can be represented by this integer type.
pub const MIN: Self = Self(0);
Expand Down Expand Up @@ -86,14 +86,14 @@ impl VarInt {
impl From<u8> for VarInt {
#[inline(always)]
fn from(value: u8) -> Self {
Self::from_u32(value as u32)
Self::from_u32(u32::from(value))
}
}

impl From<u16> for VarInt {
#[inline(always)]
fn from(value: u16) -> Self {
Self::from_u32(value as u32)
Self::from_u32(u32::from(value))
}
}

Expand Down Expand Up @@ -155,11 +155,18 @@ mod tests {
assert!((2..=VarInt::MAX_SIZE).contains(&VarInt::try_from_u64(16383).unwrap().size()));

assert!((4..=VarInt::MAX_SIZE).contains(&VarInt::try_from_u64(16384).unwrap().size()));
assert!((4..=VarInt::MAX_SIZE).contains(&VarInt::try_from_u64(1073741823).unwrap().size()));

assert!((8..=VarInt::MAX_SIZE).contains(&VarInt::try_from_u64(1073741824).unwrap().size()));
assert!((8..=VarInt::MAX_SIZE)
.contains(&VarInt::try_from_u64(4611686018427387903).unwrap().size()));
assert!(
(4..=VarInt::MAX_SIZE).contains(&VarInt::try_from_u64(1_073_741_823).unwrap().size())
);

assert!(
(8..=VarInt::MAX_SIZE).contains(&VarInt::try_from_u64(1_073_741_824).unwrap().size())
);
assert!((8..=VarInt::MAX_SIZE).contains(
&VarInt::try_from_u64(4_611_686_018_427_387_903)
.unwrap()
.size()
));

assert_eq!(VarInt::MAX_SIZE, 8);
assert_eq!(VarInt::MAX.size(), VarInt::MAX_SIZE);
Expand Down
46 changes: 23 additions & 23 deletions wtransport/src/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,18 @@ impl Driver {

if stream.session_id() == session_id {
return Ok(stream);
} else {
debug!(
"Discarding WT stream (stream_id: {}, session_id: {})",
stream.id(),
stream.session_id()
);

stream
.into_stream()
.stop(ErrorCode::BufferedStreamRejected.to_code())
.expect("Stream not already stopped");
}

debug!(
"Discarding WT stream (stream_id: {}, session_id: {})",
stream.id(),
stream.session_id()
);

stream
.into_stream()
.stop(ErrorCode::BufferedStreamRejected.to_code())
.expect("Stream not already stopped");
}
}

Expand All @@ -153,19 +153,19 @@ impl Driver {

if stream.session_id() == session_id {
return Ok(stream);
} else {
debug!(
"Discarding WT stream (stream_id: {}, session_id: {})",
stream.id(),
stream.session_id()
);

stream
.into_stream()
.1
.stop(ErrorCode::BufferedStreamRejected.to_code())
.expect("Stream not already stopped");
}

debug!(
"Discarding WT stream (stream_id: {}, session_id: {})",
stream.id(),
stream.session_id()
);

stream
.into_stream()
.1
.stop(ErrorCode::BufferedStreamRejected.to_code())
.expect("Stream not already stopped");
}
}

Expand Down
2 changes: 1 addition & 1 deletion wtransport/src/driver/streams/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl QuicSendStream {
pub fn reset(mut self, error_code: VarInt) {
self.0
.reset(varint_w2q(error_code))
.expect("Stream has been already reset")
.expect("Stream has been already reset");
}

#[inline(always)]
Expand Down
2 changes: 1 addition & 1 deletion wtransport/src/driver/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ where
/// Awaits all subscribers are dead.
#[inline(always)]
pub async fn closed(&self) {
self.0.closed().await
self.0.closed().await;
}

/// Generates a new subscriber.
Expand Down
4 changes: 2 additions & 2 deletions wtransport/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl SendStream {
/// impact on performance.
#[inline(always)]
pub fn set_priority(&self, priority: i32) {
self.0.set_priority(priority)
self.0.set_priority(priority);
}

/// Gets the priority of the send stream.
Expand All @@ -83,7 +83,7 @@ impl SendStream {
/// already been made to finish the stream, the peer may still receive all written data.
#[inline(always)]
pub fn reset(self, error_code: VarInt) {
self.0.reset(error_code)
self.0.reset(error_code);
}

/// Awaits for the stream to be stopped by the peer.
Expand Down

0 comments on commit 5253688

Please sign in to comment.