Skip to content

Commit 0953b05

Browse files
committed
lints: fix clippy
1 parent 58300bc commit 0953b05

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

src/socket/tcp.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ impl Display for ConnectError {
6363
#[cfg(feature = "std")]
6464
impl std::error::Error for ConnectError {}
6565

66+
/// Error returned by set_*
67+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
68+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
69+
pub enum ArgumentError {
70+
InvalidArgs,
71+
InvalidState,
72+
InsufficientResource,
73+
}
74+
75+
impl Display for crate::socket::tcp::ArgumentError {
76+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77+
match *self {
78+
crate::socket::tcp::ArgumentError::InvalidArgs => write!(f, "invalid arguments by RFC"),
79+
crate::socket::tcp::ArgumentError::InvalidState => write!(f, "invalid state"),
80+
crate::socket::tcp::ArgumentError::InsufficientResource => {
81+
write!(f, "insufficient runtime resource")
82+
}
83+
}
84+
}
85+
}
86+
87+
#[cfg(feature = "std")]
88+
impl std::error::Error for crate::socket::tcp::ArgumentError {}
89+
6690
/// Error returned by [`Socket::send`]
6791
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
6892
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -508,8 +532,6 @@ impl<'a> Socket<'a> {
508532
panic!("receiving buffer too large, cannot exceed 1 GiB")
509533
}
510534
let rx_cap_log2 = mem::size_of::<usize>() * 8 - rx_capacity.leading_zeros() as usize;
511-
let remote_win_shift = rx_cap_log2.saturating_sub(16) as u8;
512-
let (rx_buffer, tx_buffer) = (rx_buffer.into(), tx_buffer.into());
513535

514536
Socket {
515537
state: State::Closed,
@@ -530,7 +552,7 @@ impl<'a> Socket<'a> {
530552
remote_last_ack: None,
531553
remote_last_win: 0,
532554
remote_win_len: 0,
533-
remote_win_shift,
555+
remote_win_shift: rx_cap_log2.saturating_sub(16) as u8,
534556
remote_win_scale: None,
535557
remote_has_sack: false,
536558
remote_mss: DEFAULT_MSS,
@@ -747,19 +769,24 @@ impl<'a> Socket<'a> {
747769
/// It may be reset to 0 during the handshake if remote side does not support window scaling.
748770
///
749771
/// # Errors
750-
/// Returns an error if the socket is not in the `Closed` or `Listen` state, or if the
751-
/// receive buffer is smaller than (1<<scale) bytes.
752-
pub fn set_local_recv_win_scale(&mut self, scale: u8) -> Result<(), ()> {
772+
/// `Err(ArgumentError::InvalidArgs)` if the scale is greater than 14.
773+
/// `Err(ArgumentError::InvalidState)` if the socket is not in the `Closed` or `Listen` state.
774+
/// `Err(ArgumentError::InsufficientResource)` if the receive buffer is smaller than (1<<scale) bytes.
775+
pub fn set_local_recv_win_scale(&mut self, scale: u8) -> Result<(), ArgumentError> {
776+
if scale > 14 {
777+
return Err(ArgumentError::InvalidArgs);
778+
}
779+
753780
if self.rx_buffer.capacity() < (1 << scale) as usize {
754-
return Err(());
781+
return Err(ArgumentError::InsufficientResource);
755782
}
756783

757784
match self.state {
758785
State::Closed | State::Listen => {
759786
self.remote_win_shift = scale;
760787
Ok(())
761788
}
762-
_ => Err(()),
789+
_ => Err(ArgumentError::InvalidState),
763790
}
764791
}
765792

@@ -7532,8 +7559,8 @@ mod test {
75327559
#[test]
75337560
fn test_too_large_window_scale() {
75347561
let mut socket = Socket::new(
7535-
SocketBuffer::new(vec![0; 128]),
7536-
SocketBuffer::new(vec![0; 128]),
7562+
SocketBuffer::new(vec![0; 8 * (1 << 15)]),
7563+
SocketBuffer::new(vec![0; 8 * (1 << 15)]),
75377564
);
75387565
assert!(socket.set_local_recv_win_scale(15).is_err())
75397566
}

0 commit comments

Comments
 (0)