From 6253e97717d0db91912405ea8e5725dd6d240765 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Tue, 1 Oct 2019 16:52:10 +0900 Subject: [PATCH 1/7] feat: Add Default trait --- src/fs/dir_builder.rs | 2 +- src/fs/open_options.rs | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/fs/dir_builder.rs b/src/fs/dir_builder.rs index 0de230db..3064f7a3 100644 --- a/src/fs/dir_builder.rs +++ b/src/fs/dir_builder.rs @@ -14,7 +14,7 @@ use crate::task::blocking; /// /// [`os::unix::fs::DirBuilderExt`]: ../os/unix/fs/trait.DirBuilderExt.html /// [`std::fs::DirBuilder`]: https://doc.rust-lang.org/std/fs/struct.DirBuilder.html -#[derive(Debug)] +#[derive(Debug, Default)] pub struct DirBuilder { /// Set to `true` if non-existent parent directories should be created. recursive: bool, diff --git a/src/fs/open_options.rs b/src/fs/open_options.rs index c6cc74a0..252873cd 100644 --- a/src/fs/open_options.rs +++ b/src/fs/open_options.rs @@ -290,6 +290,12 @@ impl OpenOptions { } } +impl Default for OpenOptions { + fn default() -> Self { + Self::new() + } +} + cfg_if! { if #[cfg(feature = "docs")] { use crate::os::unix::fs::OpenOptionsExt; From 468cb6348fe4484e9a8c9cc72f4ba7ffe1f61b14 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Tue, 1 Oct 2019 16:52:22 +0900 Subject: [PATCH 2/7] fix: Remove unnecessary &mut --- src/io/stderr.rs | 2 +- src/io/stdout.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/io/stderr.rs b/src/io/stderr.rs index 64e0b186..e743e004 100644 --- a/src/io/stderr.rs +++ b/src/io/stderr.rs @@ -117,7 +117,7 @@ impl Write for Stderr { // Start the operation asynchronously. *state = State::Busy(blocking::spawn(async move { - let res = std::io::Write::write(&mut inner.stderr, &mut inner.buf); + let res = std::io::Write::write(&mut inner.stderr, &inner.buf); inner.last_op = Some(Operation::Write(res)); State::Idle(Some(inner)) })); diff --git a/src/io/stdout.rs b/src/io/stdout.rs index b7fee709..faf0c790 100644 --- a/src/io/stdout.rs +++ b/src/io/stdout.rs @@ -117,7 +117,7 @@ impl Write for Stdout { // Start the operation asynchronously. *state = State::Busy(blocking::spawn(async move { - let res = std::io::Write::write(&mut inner.stdout, &mut inner.buf); + let res = std::io::Write::write(&mut inner.stdout, &inner.buf); inner.last_op = Some(Operation::Write(res)); State::Idle(Some(inner)) })); From 2460f35768b21947fee0dfe6a145958dabac984a Mon Sep 17 00:00:00 2001 From: k-nasa Date: Tue, 1 Oct 2019 17:41:21 +0900 Subject: [PATCH 3/7] fix: Remove unnecessary Borrowed Each implements a Copy trait --- src/net/udp/mod.rs | 10 +++++----- src/task/task.rs | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/net/udp/mod.rs b/src/net/udp/mod.rs index a750899d..4588be12 100644 --- a/src/net/udp/mod.rs +++ b/src/net/udp/mod.rs @@ -391,14 +391,14 @@ impl UdpSocket { /// let mdns_addr = Ipv4Addr::new(224, 0, 0, 123); /// /// let socket = UdpSocket::bind("127.0.0.1:0").await?; - /// socket.join_multicast_v4(&mdns_addr, &interface)?; + /// socket.join_multicast_v4(mdns_addr, interface)?; /// # /// # Ok(()) }) } /// ``` - pub fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> { + pub fn join_multicast_v4(&self, multiaddr: Ipv4Addr, interface: Ipv4Addr) -> io::Result<()> { self.watcher .get_ref() - .join_multicast_v4(multiaddr, interface) + .join_multicast_v4(&multiaddr, &interface) } /// Executes an operation of the `IPV6_ADD_MEMBERSHIP` type. @@ -435,10 +435,10 @@ impl UdpSocket { /// For more information about this option, see [`join_multicast_v4`]. /// /// [`join_multicast_v4`]: #method.join_multicast_v4 - pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> { + pub fn leave_multicast_v4(&self, multiaddr: Ipv4Addr, interface: Ipv4Addr) -> io::Result<()> { self.watcher .get_ref() - .leave_multicast_v4(multiaddr, interface) + .leave_multicast_v4(&multiaddr, &interface) } /// Executes an operation of the `IPV6_DROP_MEMBERSHIP` type. diff --git a/src/task/task.rs b/src/task/task.rs index 8a1addea..ba808aa2 100644 --- a/src/task/task.rs +++ b/src/task/task.rs @@ -122,7 +122,7 @@ impl TaskId { unsafe { TaskId(NonZeroU64::new_unchecked(id)) } } - pub(crate) fn as_u64(&self) -> u64 { + pub(crate) fn as_u64(self) -> u64 { self.0.get() } } From 87b272f83dec63353c4edfb5cf7588336006424a Mon Sep 17 00:00:00 2001 From: k-nasa Date: Tue, 1 Oct 2019 17:42:45 +0900 Subject: [PATCH 4/7] refacotr: Refactor match expression --- src/stream/stream/filter.rs | 10 ++++------ src/stream/stream/find.rs | 10 ++++------ src/stream/stream/skip_while.rs | 9 +++------ 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/stream/stream/filter.rs b/src/stream/stream/filter.rs index 3fd54539..ad7b6d20 100644 --- a/src/stream/stream/filter.rs +++ b/src/stream/stream/filter.rs @@ -36,12 +36,10 @@ where let next = futures_core::ready!(self.as_mut().stream().poll_next(cx)); match next { - Some(v) => match (self.as_mut().predicate())(&v) { - true => Poll::Ready(Some(v)), - false => { - cx.waker().wake_by_ref(); - Poll::Pending - } + Some(v) if (self.as_mut().predicate())(&v) => Poll::Ready(Some(v)), + Some(_) => { + cx.waker().wake_by_ref(); + Poll::Pending }, None => Poll::Ready(None), } diff --git a/src/stream/stream/find.rs b/src/stream/stream/find.rs index 014c8b78..175477fe 100644 --- a/src/stream/stream/find.rs +++ b/src/stream/stream/find.rs @@ -36,12 +36,10 @@ where let item = futures_core::ready!(Pin::new(&mut *self.stream).poll_next(cx)); match item { - Some(v) => match (&mut self.p)(&v) { - true => Poll::Ready(Some(v)), - false => { - cx.waker().wake_by_ref(); - Poll::Pending - } + Some(v) if (&mut self.p)(&v) => Poll::Ready(Some(v)), + Some(_) => { + cx.waker().wake_by_ref(); + Poll::Pending }, None => Poll::Ready(None), } diff --git a/src/stream/stream/skip_while.rs b/src/stream/stream/skip_while.rs index a54b0510..e3fe26be 100644 --- a/src/stream/stream/skip_while.rs +++ b/src/stream/stream/skip_while.rs @@ -38,12 +38,9 @@ where match next { Some(v) => match self.as_mut().predicate() { - Some(p) => match p(&v) { - true => (), - false => { - *self.as_mut().predicate() = None; - return Poll::Ready(Some(v)); - } + Some(p) => if !p(&v) { + *self.as_mut().predicate() = None; + return Poll::Ready(Some(v)); }, None => return Poll::Ready(Some(v)), }, From f08fcd0bbb0be46079df989d2210ad5d83967eca Mon Sep 17 00:00:00 2001 From: k-nasa Date: Tue, 1 Oct 2019 17:43:31 +0900 Subject: [PATCH 5/7] refactor --- src/sync/mutex.rs | 2 +- src/sync/rwlock.rs | 2 +- src/task/block_on.rs | 3 +-- src/task/blocking.rs | 4 ++-- src/task/builder.rs | 2 +- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/sync/mutex.rs b/src/sync/mutex.rs index 8ae51ad4..673eb830 100644 --- a/src/sync/mutex.rs +++ b/src/sync/mutex.rs @@ -10,7 +10,7 @@ use crate::future::Future; use crate::task::{Context, Poll, Waker}; /// Set if the mutex is locked. -const LOCK: usize = 1 << 0; +const LOCK: usize = 1; /// Set if there are tasks blocked on the mutex. const BLOCKED: usize = 1 << 1; diff --git a/src/sync/rwlock.rs b/src/sync/rwlock.rs index 36f475b5..55a29fc4 100644 --- a/src/sync/rwlock.rs +++ b/src/sync/rwlock.rs @@ -10,7 +10,7 @@ use crate::future::Future; use crate::task::{Context, Poll, Waker}; /// Set if a write lock is held. -const WRITE_LOCK: usize = 1 << 0; +const WRITE_LOCK: usize = 1; /// Set if there are read operations blocked on the lock. const BLOCKED_READS: usize = 1 << 1; diff --git a/src/task/block_on.rs b/src/task/block_on.rs index 2d49dcaa..f585693b 100644 --- a/src/task/block_on.rs +++ b/src/task/block_on.rs @@ -69,12 +69,11 @@ where let future = task_local::add_finalizer(future); let future = async move { - let res = future.await; + future.await; trace!("block_on completed", { parent_id: parent_id, child_id: child_id, }); - res }; // Pin the future onto the stack. diff --git a/src/task/blocking.rs b/src/task/blocking.rs index 41177bc9..8f4277c3 100644 --- a/src/task/blocking.rs +++ b/src/task/blocking.rs @@ -135,7 +135,7 @@ fn random(n: u32) -> u32 { use std::num::Wrapping; thread_local! { - static RNG: Cell> = Cell::new(Wrapping(1406868647)); + static RNG: Cell> = Cell::new(Wrapping(1_406_868_647)); } RNG.with(|rng| { @@ -152,6 +152,6 @@ fn random(n: u32) -> u32 { // // Author: Daniel Lemire // Source: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ - ((x.0 as u64).wrapping_mul(n as u64) >> 32) as u32 + ((u64::from(x.0)).wrapping_mul(u64::from(n)) >> 32) as u32 }) } diff --git a/src/task/builder.rs b/src/task/builder.rs index 630876c2..a43b42bc 100644 --- a/src/task/builder.rs +++ b/src/task/builder.rs @@ -4,7 +4,7 @@ use crate::future::Future; use crate::io; /// Task builder that configures the settings of a new task. -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Builder { pub(crate) name: Option, } From c31861aa6570ec2da59d32c60d75e3117f537acd Mon Sep 17 00:00:00 2001 From: k-nasa Date: Tue, 1 Oct 2019 17:43:47 +0900 Subject: [PATCH 6/7] rafactor if expression --- src/task/sleepers.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/task/sleepers.rs b/src/task/sleepers.rs index a9071758..b5902fbe 100644 --- a/src/task/sleepers.rs +++ b/src/task/sleepers.rs @@ -30,7 +30,7 @@ impl Sleepers { pub fn wait(&self) { let mut sleep = self.sleep.lock().unwrap(); - if self.notified.swap(false, Ordering::SeqCst) == false { + if !self.notified.swap(false, Ordering::SeqCst){ *sleep += 1; let _ = self.wake.wait(sleep).unwrap(); } @@ -38,7 +38,7 @@ impl Sleepers { /// Notifies one thread. pub fn notify_one(&self) { - if self.notified.load(Ordering::SeqCst) == false { + if !self.notified.load(Ordering::SeqCst) { let mut sleep = self.sleep.lock().unwrap(); if *sleep > 0 { From cc21bdf068949aab18474972f402c5d327ca4f3e Mon Sep 17 00:00:00 2001 From: k-nasa Date: Tue, 1 Oct 2019 17:49:55 +0900 Subject: [PATCH 7/7] $cargo fmt --- src/stream/stream/filter.rs | 4 ++-- src/stream/stream/find.rs | 4 ++-- src/stream/stream/skip_while.rs | 10 ++++++---- src/task/sleepers.rs | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/stream/stream/filter.rs b/src/stream/stream/filter.rs index ad7b6d20..8ed282ce 100644 --- a/src/stream/stream/filter.rs +++ b/src/stream/stream/filter.rs @@ -37,10 +37,10 @@ where match next { Some(v) if (self.as_mut().predicate())(&v) => Poll::Ready(Some(v)), - Some(_) => { + Some(_) => { cx.waker().wake_by_ref(); Poll::Pending - }, + } None => Poll::Ready(None), } } diff --git a/src/stream/stream/find.rs b/src/stream/stream/find.rs index 175477fe..93624c03 100644 --- a/src/stream/stream/find.rs +++ b/src/stream/stream/find.rs @@ -36,11 +36,11 @@ where let item = futures_core::ready!(Pin::new(&mut *self.stream).poll_next(cx)); match item { - Some(v) if (&mut self.p)(&v) => Poll::Ready(Some(v)), + Some(v) if (&mut self.p)(&v) => Poll::Ready(Some(v)), Some(_) => { cx.waker().wake_by_ref(); Poll::Pending - }, + } None => Poll::Ready(None), } } diff --git a/src/stream/stream/skip_while.rs b/src/stream/stream/skip_while.rs index e3fe26be..b1a8d9ea 100644 --- a/src/stream/stream/skip_while.rs +++ b/src/stream/stream/skip_while.rs @@ -38,10 +38,12 @@ where match next { Some(v) => match self.as_mut().predicate() { - Some(p) => if !p(&v) { - *self.as_mut().predicate() = None; - return Poll::Ready(Some(v)); - }, + Some(p) => { + if !p(&v) { + *self.as_mut().predicate() = None; + return Poll::Ready(Some(v)); + } + } None => return Poll::Ready(Some(v)), }, None => return Poll::Ready(None), diff --git a/src/task/sleepers.rs b/src/task/sleepers.rs index b5902fbe..4e701295 100644 --- a/src/task/sleepers.rs +++ b/src/task/sleepers.rs @@ -30,7 +30,7 @@ impl Sleepers { pub fn wait(&self) { let mut sleep = self.sleep.lock().unwrap(); - if !self.notified.swap(false, Ordering::SeqCst){ + if !self.notified.swap(false, Ordering::SeqCst) { *sleep += 1; let _ = self.wake.wait(sleep).unwrap(); }