Skip to content

Commit

Permalink
chore: fix clippy issues + run clippy in CI (#45)
Browse files Browse the repository at this point in the history
* Chore: fix clippy issues + run clippy in CI

* clippy

* clippy

* clippy

* Formatting
  • Loading branch information
heeckhau authored Nov 11, 2024
1 parent 43995c5 commit 09f1abe
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 77 deletions.
38 changes: 19 additions & 19 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@ env:

jobs:
build_and_test:
if: ( ! github.event.pull_request.draft )
name: Build and test
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Checkout repository
uses: actions/checkout@v4

- name: Stable
uses: actions-rs/toolchain@v1
- name: Install stable rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
profile: minimal
toolchain: stable
- uses: Swatinem/[email protected]
components: clippy

- name: "Build"
- name: Use caching
uses: Swatinem/[email protected]

- name: Clippy
run: cargo clippy --all-features --all-targets -- -D warnings

- name: Build
run: cargo build

- name: "Test"
Expand All @@ -39,17 +45,11 @@ jobs:
run: cargo doc --no-deps --document-private-items --all-features --workspace --examples

rustfmt:
name: Rustfmt
name: Check formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Nightly with rustfmt
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
components: rustfmt

- name: "Check formatting"
run: cargo +nightly fmt --check --all
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/[email protected]
- name: Check formatting
run: cargo fmt --check --all
4 changes: 2 additions & 2 deletions serio/examples/tokio_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ async fn main() {
let a = LengthDelimitedCodec::builder().new_framed(a);
let b = LengthDelimitedCodec::builder().new_framed(b);

let a = Framed::new(a, Bincode::default());
let b = Framed::new(b, Bincode::default());
let a = Framed::new(a, Bincode);
let b = Framed::new(b, Bincode);

tokio::try_join!(alice(a), bob(b)).unwrap();
}
Expand Down
6 changes: 3 additions & 3 deletions serio/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod bincode_impl {
type Error = bincode::Error;

fn deserialize<T: Deserialize>(&mut self, buf: &BytesMut) -> Result<T, Self::Error> {
Ok(deserialize(buf)?)
deserialize(buf)
}
}

Expand Down Expand Up @@ -178,8 +178,8 @@ mod tests {
fn test_framed() {
let (a, b) = duplex(1024);

let mut a = Bincode::default().new_framed(a.compat());
let mut b = Bincode::default().new_framed(b.compat());
let mut a = Bincode.new_framed(a.compat());
let mut b = Bincode.new_framed(b.compat());

let a = async {
a.send(Ping).await.unwrap();
Expand Down
10 changes: 5 additions & 5 deletions serio/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ pub trait Stream {
/// stream state:
///
/// - `Poll::Pending` means that this stream's next value is not ready
/// yet. Implementations will ensure that the current task will be notified
/// when the next value may be ready.
/// yet. Implementations will ensure that the current task will be notified
/// when the next value may be ready.
///
/// - `Poll::Ready(Some(val))` means that the stream has successfully
/// produced a value, `val`, and may produce further values on subsequent
/// `poll_next` calls.
/// produced a value, `val`, and may produce further values on subsequent
/// `poll_next` calls.
///
/// - `Poll::Ready(None)` means that the stream has terminated, and
/// `poll_next` should not be invoked again.
/// `poll_next` should not be invoked again.
fn poll_next<Item: Deserialize>(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
18 changes: 10 additions & 8 deletions spansy/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ impl Iterator for Requests {
if self.pos >= self.src.len() {
None
} else {
Some(parse_request_from_bytes(&self.src, self.pos).map(|req| {
self.pos += req.span.len();
req
}))
Some(
parse_request_from_bytes(&self.src, self.pos).inspect(|req| {
self.pos += req.span.len();
}),
)
}
}
}
Expand Down Expand Up @@ -82,10 +83,11 @@ impl Iterator for Responses {
if self.pos >= self.src.len() {
None
} else {
Some(parse_response_from_bytes(&self.src, self.pos).map(|resp| {
self.pos += resp.span.len();
resp
}))
Some(
parse_response_from_bytes(&self.src, self.pos).inspect(|resp| {
self.pos += resp.span.len();
}),
)
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions spansy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ impl<T: ?Sized> AsRef<RangeSet<usize>> for Span<T> {
}
}

impl<T: ?Sized> Into<Bytes> for Span<T> {
fn into(self) -> Bytes {
self.data
impl<T: ?Sized> From<Span<T>> for Bytes {
fn from(val: Span<T>) -> Self {
val.data
}
}

impl<T: ?Sized> Into<RangeSet<usize>> for Span<T> {
fn into(self) -> RangeSet<usize> {
self.indices
impl<T: ?Sized> From<Span<T>> for RangeSet<usize> {
fn from(val: Span<T>) -> Self {
val.indices
}
}

Expand Down
6 changes: 2 additions & 4 deletions uid-mux/src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,8 @@ where
// If we've finished sending, flush the write buffer. If flushing
// succeeds then we can return Ready, otherwise we need to keep
// trying.
if state.is_done() {
if pin!(&mut state.io).poll_flush(cx)?.is_ready() {
return Poll::Ready(Ok(state.io));
}
if state.is_done() && pin!(&mut state.io).poll_flush(cx)?.is_ready() {
return Poll::Ready(Ok(state.io));
}

self.0 = State::Pending(state);
Expand Down
1 change: 1 addition & 0 deletions uid-mux/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn test_yamux_pair(
///
/// * `buffer` - The buffer size.
/// * `codec` - The codec.
#[allow(clippy::type_complexity)]
pub fn test_yamux_pair_framed<C: Clone>(
buffer: usize,
codec: C,
Expand Down
32 changes: 11 additions & 21 deletions uid-mux/src/yamux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,14 @@ pub struct Yamux<Io> {
shutdown_notify: Arc<AtomicBool>,
}

#[derive(Debug)]
#[derive(Debug, Default)]
struct Queue {
waiting: HashMap<InternalId, oneshot::Sender<Stream>>,
ready: HashMap<InternalId, Stream>,
alloc: usize,
waker: Option<Waker>,
}

impl Default for Queue {
fn default() -> Self {
Self {
waiting: Default::default(),
ready: Default::default(),
alloc: 0,
waker: None,
}
}
}

impl<Io> Yamux<Io> {
/// Returns a new control handle.
pub fn control(&self) -> YamuxCtrl {
Expand Down Expand Up @@ -368,20 +357,19 @@ impl YamuxCtrl {

let mut queue = self.queue.lock().unwrap();
queue.alloc += count;
queue.waker.as_ref().map(|waker| waker.wake_by_ref());
if let Some(waker) = queue.waker.as_ref() {
waker.wake_by_ref()
}
}

/// Closes the yamux connection.
pub fn close(&self) {
self.shutdown_notify.store(true, Ordering::Relaxed);

// Wake up the connection.
self.queue
.lock()
.unwrap()
.waker
.as_ref()
.map(|waker| waker.wake_by_ref());
if let Some(waker) = self.queue.lock().unwrap().waker.as_ref() {
waker.wake_by_ref()
}
}
}

Expand Down Expand Up @@ -418,7 +406,9 @@ where
// Insert the oneshot into the queue.
queue.waiting.insert(internal_id, sender);
// Wake up the connection.
queue.waker.as_ref().map(|waker| waker.wake_by_ref());
if let Some(waker) = queue.waker.as_ref() {
waker.wake_by_ref()
}

trace!("waiting for stream");

Expand All @@ -431,7 +421,7 @@ where
.inspect(|_| debug!("caller received stream"))
.inspect_err(|_| error!("connection cancelled stream"))
.map_err(|_| {
std::io::Error::other(format!("connection cancelled stream"))
std::io::Error::other("connection cancelled stream".to_string())
}),
_ = self.close_notify.notified().fuse() => {
error!("connection closed before stream opened");
Expand Down
12 changes: 6 additions & 6 deletions utils/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ impl NestedId {
}
}

impl ToString for NestedId {
fn to_string(&self) -> String {
impl std::fmt::Display for NestedId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NestedId::String { id, root } => match root {
Some(root) => format!("{}/{}", root.to_string(), id),
None => id.to_string(),
Some(root) => write!(f, "{}/{}", root, id),
None => write!(f, "{}", id),
},
NestedId::Counter { value, root } => match root {
Some(root) => format!("{}/{}", root.to_string(), value),
None => value.to_string(),
Some(root) => write!(f, "{}/{}", root, value),
None => write!(f, "{}", value),
},
}
}
Expand Down
4 changes: 1 addition & 3 deletions utils/src/range/difference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ pub trait DifferenceMut<Rhs> {

impl<T: Copy + Ord> DifferenceMut<Range<T>> for RangeSet<T> {
fn difference_mut(&mut self, other: &Range<T>) {
if other.is_empty() {
return;
} else if self.ranges.is_empty() {
if other.is_empty() || self.ranges.is_empty() {
return;
}

Expand Down
1 change: 1 addition & 0 deletions utils/src/range/intersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl<T: Copy + Ord> BitAnd<&RangeSet<T>> for RangeSet<T> {
}

#[cfg(test)]
#[allow(clippy::single_range_in_vec_init)]
mod tests {
use std::collections::HashSet;

Expand Down
1 change: 1 addition & 0 deletions utils/src/range/subset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl<T: Copy + Ord> Subset<RangeSet<T>> for RangeSet<T> {
}

#[cfg(test)]
#[allow(clippy::single_range_in_vec_init)]
mod tests {
use super::*;

Expand Down

0 comments on commit 09f1abe

Please sign in to comment.