Skip to content

Commit bb70b7b

Browse files
committed
lints: fix clippy
1 parent 84680f3 commit bb70b7b

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))]
@@ -514,8 +538,6 @@ impl<'a> Socket<'a> {
514538
panic!("receiving buffer too large, cannot exceed 1 GiB")
515539
}
516540
let rx_cap_log2 = mem::size_of::<usize>() * 8 - rx_capacity.leading_zeros() as usize;
517-
let remote_win_shift = rx_cap_log2.saturating_sub(16) as u8;
518-
let (rx_buffer, tx_buffer) = (rx_buffer.into(), tx_buffer.into());
519541

520542
Socket {
521543
state: State::Closed,
@@ -536,7 +558,7 @@ impl<'a> Socket<'a> {
536558
remote_last_ack: None,
537559
remote_last_win: 0,
538560
remote_win_len: 0,
539-
remote_win_shift,
561+
remote_win_shift: rx_cap_log2.saturating_sub(16) as u8,
540562
remote_win_scale: None,
541563
remote_has_sack: false,
542564
remote_mss: DEFAULT_MSS,
@@ -765,19 +787,24 @@ impl<'a> Socket<'a> {
765787
/// It may be reset to 0 during the handshake if remote side does not support window scaling.
766788
///
767789
/// # Errors
768-
/// Returns an error if the socket is not in the `Closed` or `Listen` state, or if the
769-
/// receive buffer is smaller than (1<<scale) bytes.
770-
pub fn set_local_recv_win_scale(&mut self, scale: u8) -> Result<(), ()> {
790+
/// `Err(ArgumentError::InvalidArgs)` if the scale is greater than 14.
791+
/// `Err(ArgumentError::InvalidState)` if the socket is not in the `Closed` or `Listen` state.
792+
/// `Err(ArgumentError::InsufficientResource)` if the receive buffer is smaller than (1<<scale) bytes.
793+
pub fn set_local_recv_win_scale(&mut self, scale: u8) -> Result<(), ArgumentError> {
794+
if scale > 14 {
795+
return Err(ArgumentError::InvalidArgs);
796+
}
797+
771798
if self.rx_buffer.capacity() < (1 << scale) as usize {
772-
return Err(());
799+
return Err(ArgumentError::InsufficientResource);
773800
}
774801

775802
match self.state {
776803
State::Closed | State::Listen => {
777804
self.remote_win_shift = scale;
778805
Ok(())
779806
}
780-
_ => Err(()),
807+
_ => Err(ArgumentError::InvalidState),
781808
}
782809
}
783810

@@ -7808,8 +7835,8 @@ mod test {
78087835
#[test]
78097836
fn test_too_large_window_scale() {
78107837
let mut socket = Socket::new(
7811-
SocketBuffer::new(vec![0; 128]),
7812-
SocketBuffer::new(vec![0; 128]),
7838+
SocketBuffer::new(vec![0; 8 * (1 << 15)]),
7839+
SocketBuffer::new(vec![0; 8 * (1 << 15)]),
78137840
);
78147841
assert!(socket.set_local_recv_win_scale(15).is_err())
78157842
}

0 commit comments

Comments
 (0)