mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-07 00:00:08 +02:00
tokio: remove wildcard in match patterns (#5970)
This commit is contained in:
@@ -126,22 +126,22 @@ impl Configuration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn build(&self) -> Result<FinalConfig, syn::Error> {
|
fn build(&self) -> Result<FinalConfig, syn::Error> {
|
||||||
let flavor = self.flavor.unwrap_or(self.default_flavor);
|
use RuntimeFlavor as F;
|
||||||
use RuntimeFlavor::*;
|
|
||||||
|
|
||||||
|
let flavor = self.flavor.unwrap_or(self.default_flavor);
|
||||||
let worker_threads = match (flavor, self.worker_threads) {
|
let worker_threads = match (flavor, self.worker_threads) {
|
||||||
(CurrentThread, Some((_, worker_threads_span))) => {
|
(F::CurrentThread, Some((_, worker_threads_span))) => {
|
||||||
let msg = format!(
|
let msg = format!(
|
||||||
"The `worker_threads` option requires the `multi_thread` runtime flavor. Use `#[{}(flavor = \"multi_thread\")]`",
|
"The `worker_threads` option requires the `multi_thread` runtime flavor. Use `#[{}(flavor = \"multi_thread\")]`",
|
||||||
self.macro_name(),
|
self.macro_name(),
|
||||||
);
|
);
|
||||||
return Err(syn::Error::new(worker_threads_span, msg));
|
return Err(syn::Error::new(worker_threads_span, msg));
|
||||||
}
|
}
|
||||||
(CurrentThread, None) => None,
|
(F::CurrentThread, None) => None,
|
||||||
(Threaded, worker_threads) if self.rt_multi_thread_available => {
|
(F::Threaded, worker_threads) if self.rt_multi_thread_available => {
|
||||||
worker_threads.map(|(val, _span)| val)
|
worker_threads.map(|(val, _span)| val)
|
||||||
}
|
}
|
||||||
(Threaded, _) => {
|
(F::Threaded, _) => {
|
||||||
let msg = if self.flavor.is_none() {
|
let msg = if self.flavor.is_none() {
|
||||||
"The default runtime flavor is `multi_thread`, but the `rt-multi-thread` feature is disabled."
|
"The default runtime flavor is `multi_thread`, but the `rt-multi-thread` feature is disabled."
|
||||||
} else {
|
} else {
|
||||||
@@ -152,14 +152,14 @@ impl Configuration {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let start_paused = match (flavor, self.start_paused) {
|
let start_paused = match (flavor, self.start_paused) {
|
||||||
(Threaded, Some((_, start_paused_span))) => {
|
(F::Threaded, Some((_, start_paused_span))) => {
|
||||||
let msg = format!(
|
let msg = format!(
|
||||||
"The `start_paused` option requires the `current_thread` runtime flavor. Use `#[{}(flavor = \"current_thread\")]`",
|
"The `start_paused` option requires the `current_thread` runtime flavor. Use `#[{}(flavor = \"current_thread\")]`",
|
||||||
self.macro_name(),
|
self.macro_name(),
|
||||||
);
|
);
|
||||||
return Err(syn::Error::new(start_paused_span, msg));
|
return Err(syn::Error::new(start_paused_span, msg));
|
||||||
}
|
}
|
||||||
(CurrentThread, Some((start_paused, _))) => Some(start_paused),
|
(F::CurrentThread, Some((start_paused, _))) => Some(start_paused),
|
||||||
(_, None) => None,
|
(_, None) => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -66,25 +66,23 @@ where
|
|||||||
T: Stream,
|
T: Stream,
|
||||||
U: Stream<Item = T::Item>,
|
U: Stream<Item = T::Item>,
|
||||||
{
|
{
|
||||||
use Poll::*;
|
|
||||||
|
|
||||||
let mut done = true;
|
let mut done = true;
|
||||||
|
|
||||||
match first.poll_next(cx) {
|
match first.poll_next(cx) {
|
||||||
Ready(Some(val)) => return Ready(Some(val)),
|
Poll::Ready(Some(val)) => return Poll::Ready(Some(val)),
|
||||||
Ready(None) => {}
|
Poll::Ready(None) => {}
|
||||||
Pending => done = false,
|
Poll::Pending => done = false,
|
||||||
}
|
}
|
||||||
|
|
||||||
match second.poll_next(cx) {
|
match second.poll_next(cx) {
|
||||||
Ready(Some(val)) => return Ready(Some(val)),
|
Poll::Ready(Some(val)) => return Poll::Ready(Some(val)),
|
||||||
Ready(None) => {}
|
Poll::Ready(None) => {}
|
||||||
Pending => done = false,
|
Poll::Pending => done = false,
|
||||||
}
|
}
|
||||||
|
|
||||||
if done {
|
if done {
|
||||||
Ready(None)
|
Poll::Ready(None)
|
||||||
} else {
|
} else {
|
||||||
Pending
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -518,8 +518,6 @@ where
|
|||||||
{
|
{
|
||||||
/// Polls the next value, includes the vec entry index
|
/// Polls the next value, includes the vec entry index
|
||||||
fn poll_next_entry(&mut self, cx: &mut Context<'_>) -> Poll<Option<(usize, V::Item)>> {
|
fn poll_next_entry(&mut self, cx: &mut Context<'_>) -> Poll<Option<(usize, V::Item)>> {
|
||||||
use Poll::*;
|
|
||||||
|
|
||||||
let start = self::rand::thread_rng_n(self.entries.len() as u32) as usize;
|
let start = self::rand::thread_rng_n(self.entries.len() as u32) as usize;
|
||||||
let mut idx = start;
|
let mut idx = start;
|
||||||
|
|
||||||
@@ -527,8 +525,8 @@ where
|
|||||||
let (_, stream) = &mut self.entries[idx];
|
let (_, stream) = &mut self.entries[idx];
|
||||||
|
|
||||||
match Pin::new(stream).poll_next(cx) {
|
match Pin::new(stream).poll_next(cx) {
|
||||||
Ready(Some(val)) => return Ready(Some((idx, val))),
|
Poll::Ready(Some(val)) => return Poll::Ready(Some((idx, val))),
|
||||||
Ready(None) => {
|
Poll::Ready(None) => {
|
||||||
// Remove the entry
|
// Remove the entry
|
||||||
self.entries.swap_remove(idx);
|
self.entries.swap_remove(idx);
|
||||||
|
|
||||||
@@ -542,7 +540,7 @@ where
|
|||||||
idx = idx.wrapping_add(1) % self.entries.len();
|
idx = idx.wrapping_add(1) % self.entries.len();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Pending => {
|
Poll::Pending => {
|
||||||
idx = idx.wrapping_add(1) % self.entries.len();
|
idx = idx.wrapping_add(1) % self.entries.len();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -550,9 +548,9 @@ where
|
|||||||
|
|
||||||
// If the map is empty, then the stream is complete.
|
// If the map is empty, then the stream is complete.
|
||||||
if self.entries.is_empty() {
|
if self.entries.is_empty() {
|
||||||
Ready(None)
|
Poll::Ready(None)
|
||||||
} else {
|
} else {
|
||||||
Pending
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-12
@@ -22,17 +22,17 @@
|
|||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! assert_ready {
|
macro_rules! assert_ready {
|
||||||
($e:expr) => {{
|
($e:expr) => {{
|
||||||
use core::task::Poll::*;
|
use core::task::Poll;
|
||||||
match $e {
|
match $e {
|
||||||
Ready(v) => v,
|
Poll::Ready(v) => v,
|
||||||
Pending => panic!("pending"),
|
Poll::Pending => panic!("pending"),
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
($e:expr, $($msg:tt)+) => {{
|
($e:expr, $($msg:tt)+) => {{
|
||||||
use core::task::Poll::*;
|
use core::task::Poll;
|
||||||
match $e {
|
match $e {
|
||||||
Ready(v) => v,
|
Poll::Ready(v) => v,
|
||||||
Pending => {
|
Poll::Pending => {
|
||||||
panic!("pending; {}", format_args!($($msg)+))
|
panic!("pending; {}", format_args!($($msg)+))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,17 +127,17 @@ macro_rules! assert_ready_err {
|
|||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! assert_pending {
|
macro_rules! assert_pending {
|
||||||
($e:expr) => {{
|
($e:expr) => {{
|
||||||
use core::task::Poll::*;
|
use core::task::Poll;
|
||||||
match $e {
|
match $e {
|
||||||
Pending => {}
|
Poll::Pending => {}
|
||||||
Ready(v) => panic!("ready; value = {:?}", v),
|
Poll::Ready(v) => panic!("ready; value = {:?}", v),
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
($e:expr, $($msg:tt)+) => {{
|
($e:expr, $($msg:tt)+) => {{
|
||||||
use core::task::Poll::*;
|
use core::task::Poll;
|
||||||
match $e {
|
match $e {
|
||||||
Pending => {}
|
Poll::Pending => {}
|
||||||
Ready(v) => {
|
Poll::Ready(v) => {
|
||||||
panic!("ready; value = {:?}; {}", v, format_args!($($msg)+))
|
panic!("ready; value = {:?}; {}", v, format_args!($($msg)+))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ use futures::{pin_mut, Sink, Stream};
|
|||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::Poll::*;
|
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
macro_rules! mock {
|
macro_rules! mock {
|
||||||
@@ -39,10 +38,10 @@ macro_rules! assert_next_eq {
|
|||||||
macro_rules! assert_next_pending {
|
macro_rules! assert_next_pending {
|
||||||
($io:ident) => {{
|
($io:ident) => {{
|
||||||
task::spawn(()).enter(|cx, _| match $io.as_mut().poll_next(cx) {
|
task::spawn(()).enter(|cx, _| match $io.as_mut().poll_next(cx) {
|
||||||
Ready(Some(Ok(v))) => panic!("value = {:?}", v),
|
Poll::Ready(Some(Ok(v))) => panic!("value = {:?}", v),
|
||||||
Ready(Some(Err(e))) => panic!("error = {:?}", e),
|
Poll::Ready(Some(Err(e))) => panic!("error = {:?}", e),
|
||||||
Ready(None) => panic!("done"),
|
Poll::Ready(None) => panic!("done"),
|
||||||
Pending => {}
|
Poll::Pending => {}
|
||||||
});
|
});
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
@@ -50,10 +49,10 @@ macro_rules! assert_next_pending {
|
|||||||
macro_rules! assert_next_err {
|
macro_rules! assert_next_err {
|
||||||
($io:ident) => {{
|
($io:ident) => {{
|
||||||
task::spawn(()).enter(|cx, _| match $io.as_mut().poll_next(cx) {
|
task::spawn(()).enter(|cx, _| match $io.as_mut().poll_next(cx) {
|
||||||
Ready(Some(Ok(v))) => panic!("value = {:?}", v),
|
Poll::Ready(Some(Ok(v))) => panic!("value = {:?}", v),
|
||||||
Ready(Some(Err(_))) => {}
|
Poll::Ready(Some(Err(_))) => {}
|
||||||
Ready(None) => panic!("done"),
|
Poll::Ready(None) => panic!("done"),
|
||||||
Pending => panic!("pending"),
|
Poll::Pending => panic!("pending"),
|
||||||
});
|
});
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
@@ -186,11 +185,11 @@ fn read_single_frame_multi_packet_wait() {
|
|||||||
let io = FramedRead::new(
|
let io = FramedRead::new(
|
||||||
mock! {
|
mock! {
|
||||||
data(b"\x00\x00"),
|
data(b"\x00\x00"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
data(b"\x00\x09abc"),
|
data(b"\x00\x09abc"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
data(b"defghi"),
|
data(b"defghi"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
},
|
},
|
||||||
LengthDelimitedCodec::new(),
|
LengthDelimitedCodec::new(),
|
||||||
);
|
);
|
||||||
@@ -208,15 +207,15 @@ fn read_multi_frame_multi_packet_wait() {
|
|||||||
let io = FramedRead::new(
|
let io = FramedRead::new(
|
||||||
mock! {
|
mock! {
|
||||||
data(b"\x00\x00"),
|
data(b"\x00\x00"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
data(b"\x00\x09abc"),
|
data(b"\x00\x09abc"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
data(b"defghi"),
|
data(b"defghi"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
data(b"\x00\x00\x00\x0312"),
|
data(b"\x00\x00\x00\x0312"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
data(b"3\x00\x00\x00\x0bhello world"),
|
data(b"3\x00\x00\x00\x0bhello world"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
},
|
},
|
||||||
LengthDelimitedCodec::new(),
|
LengthDelimitedCodec::new(),
|
||||||
);
|
);
|
||||||
@@ -250,9 +249,9 @@ fn read_incomplete_head() {
|
|||||||
fn read_incomplete_head_multi() {
|
fn read_incomplete_head_multi() {
|
||||||
let io = FramedRead::new(
|
let io = FramedRead::new(
|
||||||
mock! {
|
mock! {
|
||||||
Pending,
|
Poll::Pending,
|
||||||
data(b"\x00"),
|
data(b"\x00"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
},
|
},
|
||||||
LengthDelimitedCodec::new(),
|
LengthDelimitedCodec::new(),
|
||||||
);
|
);
|
||||||
@@ -268,9 +267,9 @@ fn read_incomplete_payload() {
|
|||||||
let io = FramedRead::new(
|
let io = FramedRead::new(
|
||||||
mock! {
|
mock! {
|
||||||
data(b"\x00\x00\x00\x09ab"),
|
data(b"\x00\x00\x00\x09ab"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
data(b"cd"),
|
data(b"cd"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
},
|
},
|
||||||
LengthDelimitedCodec::new(),
|
LengthDelimitedCodec::new(),
|
||||||
);
|
);
|
||||||
@@ -310,7 +309,7 @@ fn read_update_max_frame_len_at_rest() {
|
|||||||
fn read_update_max_frame_len_in_flight() {
|
fn read_update_max_frame_len_in_flight() {
|
||||||
let io = length_delimited::Builder::new().new_read(mock! {
|
let io = length_delimited::Builder::new().new_read(mock! {
|
||||||
data(b"\x00\x00\x00\x09abcd"),
|
data(b"\x00\x00\x00\x09abcd"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
data(b"efghi"),
|
data(b"efghi"),
|
||||||
data(b"\x00\x00\x00\x09abcdefghi"),
|
data(b"\x00\x00\x00\x09abcdefghi"),
|
||||||
});
|
});
|
||||||
@@ -533,9 +532,9 @@ fn write_single_multi_frame_multi_packet() {
|
|||||||
fn write_single_frame_would_block() {
|
fn write_single_frame_would_block() {
|
||||||
let io = FramedWrite::new(
|
let io = FramedWrite::new(
|
||||||
mock! {
|
mock! {
|
||||||
Pending,
|
Poll::Pending,
|
||||||
data(b"\x00\x00"),
|
data(b"\x00\x00"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
data(b"\x00\x09"),
|
data(b"\x00\x09"),
|
||||||
data(b"abcdefghi"),
|
data(b"abcdefghi"),
|
||||||
flush(),
|
flush(),
|
||||||
@@ -640,7 +639,7 @@ fn write_update_max_frame_len_in_flight() {
|
|||||||
let io = length_delimited::Builder::new().new_write(mock! {
|
let io = length_delimited::Builder::new().new_write(mock! {
|
||||||
data(b"\x00\x00\x00\x06"),
|
data(b"\x00\x00\x00\x06"),
|
||||||
data(b"ab"),
|
data(b"ab"),
|
||||||
Pending,
|
Poll::Pending,
|
||||||
data(b"cdef"),
|
data(b"cdef"),
|
||||||
flush(),
|
flush(),
|
||||||
});
|
});
|
||||||
@@ -701,8 +700,6 @@ enum Op {
|
|||||||
Flush,
|
Flush,
|
||||||
}
|
}
|
||||||
|
|
||||||
use self::Op::*;
|
|
||||||
|
|
||||||
impl AsyncRead for Mock {
|
impl AsyncRead for Mock {
|
||||||
fn poll_read(
|
fn poll_read(
|
||||||
mut self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
@@ -710,15 +707,15 @@ impl AsyncRead for Mock {
|
|||||||
dst: &mut ReadBuf<'_>,
|
dst: &mut ReadBuf<'_>,
|
||||||
) -> Poll<io::Result<()>> {
|
) -> Poll<io::Result<()>> {
|
||||||
match self.calls.pop_front() {
|
match self.calls.pop_front() {
|
||||||
Some(Ready(Ok(Op::Data(data)))) => {
|
Some(Poll::Ready(Ok(Op::Data(data)))) => {
|
||||||
debug_assert!(dst.remaining() >= data.len());
|
debug_assert!(dst.remaining() >= data.len());
|
||||||
dst.put_slice(&data);
|
dst.put_slice(&data);
|
||||||
Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
Some(Ready(Ok(_))) => panic!(),
|
Some(Poll::Ready(Ok(_))) => panic!(),
|
||||||
Some(Ready(Err(e))) => Ready(Err(e)),
|
Some(Poll::Ready(Err(e))) => Poll::Ready(Err(e)),
|
||||||
Some(Pending) => Pending,
|
Some(Poll::Pending) => Poll::Pending,
|
||||||
None => Ready(Ok(())),
|
None => Poll::Ready(Ok(())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -730,31 +727,31 @@ impl AsyncWrite for Mock {
|
|||||||
src: &[u8],
|
src: &[u8],
|
||||||
) -> Poll<Result<usize, io::Error>> {
|
) -> Poll<Result<usize, io::Error>> {
|
||||||
match self.calls.pop_front() {
|
match self.calls.pop_front() {
|
||||||
Some(Ready(Ok(Op::Data(data)))) => {
|
Some(Poll::Ready(Ok(Op::Data(data)))) => {
|
||||||
let len = data.len();
|
let len = data.len();
|
||||||
assert!(src.len() >= len, "expect={:?}; actual={:?}", data, src);
|
assert!(src.len() >= len, "expect={:?}; actual={:?}", data, src);
|
||||||
assert_eq!(&data[..], &src[..len]);
|
assert_eq!(&data[..], &src[..len]);
|
||||||
Ready(Ok(len))
|
Poll::Ready(Ok(len))
|
||||||
}
|
}
|
||||||
Some(Ready(Ok(_))) => panic!(),
|
Some(Poll::Ready(Ok(_))) => panic!(),
|
||||||
Some(Ready(Err(e))) => Ready(Err(e)),
|
Some(Poll::Ready(Err(e))) => Poll::Ready(Err(e)),
|
||||||
Some(Pending) => Pending,
|
Some(Poll::Pending) => Poll::Pending,
|
||||||
None => Ready(Ok(0)),
|
None => Poll::Ready(Ok(0)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_flush(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
fn poll_flush(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
||||||
match self.calls.pop_front() {
|
match self.calls.pop_front() {
|
||||||
Some(Ready(Ok(Op::Flush))) => Ready(Ok(())),
|
Some(Poll::Ready(Ok(Op::Flush))) => Poll::Ready(Ok(())),
|
||||||
Some(Ready(Ok(_))) => panic!(),
|
Some(Poll::Ready(Ok(_))) => panic!(),
|
||||||
Some(Ready(Err(e))) => Ready(Err(e)),
|
Some(Poll::Ready(Err(e))) => Poll::Ready(Err(e)),
|
||||||
Some(Pending) => Pending,
|
Some(Poll::Pending) => Poll::Pending,
|
||||||
None => Ready(Ok(())),
|
None => Poll::Ready(Ok(())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
||||||
Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -771,9 +768,9 @@ impl From<Vec<u8>> for Op {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn data(bytes: &[u8]) -> Poll<io::Result<Op>> {
|
fn data(bytes: &[u8]) -> Poll<io::Result<Op>> {
|
||||||
Ready(Ok(bytes.into()))
|
Poll::Ready(Ok(bytes.into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush() -> Poll<io::Result<Op>> {
|
fn flush() -> Poll<io::Result<Op>> {
|
||||||
Ready(Ok(Flush))
|
Poll::Ready(Ok(Op::Flush))
|
||||||
}
|
}
|
||||||
|
|||||||
+42
-44
@@ -2,7 +2,6 @@
|
|||||||
//!
|
//!
|
||||||
//! [`File`]: File
|
//! [`File`]: File
|
||||||
|
|
||||||
use self::State::*;
|
|
||||||
use crate::fs::{asyncify, OpenOptions};
|
use crate::fs::{asyncify, OpenOptions};
|
||||||
use crate::io::blocking::Buf;
|
use crate::io::blocking::Buf;
|
||||||
use crate::io::{AsyncRead, AsyncSeek, AsyncWrite, ReadBuf};
|
use crate::io::{AsyncRead, AsyncSeek, AsyncWrite, ReadBuf};
|
||||||
@@ -17,7 +16,6 @@ use std::pin::Pin;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::task::Context;
|
use std::task::Context;
|
||||||
use std::task::Poll;
|
use std::task::Poll;
|
||||||
use std::task::Poll::*;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use super::mocks::JoinHandle;
|
use super::mocks::JoinHandle;
|
||||||
@@ -351,7 +349,7 @@ impl File {
|
|||||||
inner.complete_inflight().await;
|
inner.complete_inflight().await;
|
||||||
|
|
||||||
let mut buf = match inner.state {
|
let mut buf = match inner.state {
|
||||||
Idle(ref mut buf_cell) => buf_cell.take().unwrap(),
|
State::Idle(ref mut buf_cell) => buf_cell.take().unwrap(),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -363,7 +361,7 @@ impl File {
|
|||||||
|
|
||||||
let std = self.std.clone();
|
let std = self.std.clone();
|
||||||
|
|
||||||
inner.state = Busy(spawn_blocking(move || {
|
inner.state = State::Busy(spawn_blocking(move || {
|
||||||
let res = if let Some(seek) = seek {
|
let res = if let Some(seek) = seek {
|
||||||
(&*std).seek(seek).and_then(|_| std.set_len(size))
|
(&*std).seek(seek).and_then(|_| std.set_len(size))
|
||||||
} else {
|
} else {
|
||||||
@@ -376,11 +374,11 @@ impl File {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
let (op, buf) = match inner.state {
|
let (op, buf) = match inner.state {
|
||||||
Idle(_) => unreachable!(),
|
State::Idle(_) => unreachable!(),
|
||||||
Busy(ref mut rx) => rx.await?,
|
State::Busy(ref mut rx) => rx.await?,
|
||||||
};
|
};
|
||||||
|
|
||||||
inner.state = Idle(Some(buf));
|
inner.state = State::Idle(Some(buf));
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
Operation::Seek(res) => res.map(|pos| {
|
Operation::Seek(res) => res.map(|pos| {
|
||||||
@@ -532,51 +530,51 @@ impl AsyncRead for File {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
match inner.state {
|
match inner.state {
|
||||||
Idle(ref mut buf_cell) => {
|
State::Idle(ref mut buf_cell) => {
|
||||||
let mut buf = buf_cell.take().unwrap();
|
let mut buf = buf_cell.take().unwrap();
|
||||||
|
|
||||||
if !buf.is_empty() {
|
if !buf.is_empty() {
|
||||||
buf.copy_to(dst);
|
buf.copy_to(dst);
|
||||||
*buf_cell = Some(buf);
|
*buf_cell = Some(buf);
|
||||||
return Ready(Ok(()));
|
return Poll::Ready(Ok(()));
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.ensure_capacity_for(dst);
|
buf.ensure_capacity_for(dst);
|
||||||
let std = me.std.clone();
|
let std = me.std.clone();
|
||||||
|
|
||||||
inner.state = Busy(spawn_blocking(move || {
|
inner.state = State::Busy(spawn_blocking(move || {
|
||||||
let res = buf.read_from(&mut &*std);
|
let res = buf.read_from(&mut &*std);
|
||||||
(Operation::Read(res), buf)
|
(Operation::Read(res), buf)
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
Busy(ref mut rx) => {
|
State::Busy(ref mut rx) => {
|
||||||
let (op, mut buf) = ready!(Pin::new(rx).poll(cx))?;
|
let (op, mut buf) = ready!(Pin::new(rx).poll(cx))?;
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
Operation::Read(Ok(_)) => {
|
Operation::Read(Ok(_)) => {
|
||||||
buf.copy_to(dst);
|
buf.copy_to(dst);
|
||||||
inner.state = Idle(Some(buf));
|
inner.state = State::Idle(Some(buf));
|
||||||
return Ready(Ok(()));
|
return Poll::Ready(Ok(()));
|
||||||
}
|
}
|
||||||
Operation::Read(Err(e)) => {
|
Operation::Read(Err(e)) => {
|
||||||
assert!(buf.is_empty());
|
assert!(buf.is_empty());
|
||||||
|
|
||||||
inner.state = Idle(Some(buf));
|
inner.state = State::Idle(Some(buf));
|
||||||
return Ready(Err(e));
|
return Poll::Ready(Err(e));
|
||||||
}
|
}
|
||||||
Operation::Write(Ok(_)) => {
|
Operation::Write(Ok(_)) => {
|
||||||
assert!(buf.is_empty());
|
assert!(buf.is_empty());
|
||||||
inner.state = Idle(Some(buf));
|
inner.state = State::Idle(Some(buf));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Operation::Write(Err(e)) => {
|
Operation::Write(Err(e)) => {
|
||||||
assert!(inner.last_write_err.is_none());
|
assert!(inner.last_write_err.is_none());
|
||||||
inner.last_write_err = Some(e.kind());
|
inner.last_write_err = Some(e.kind());
|
||||||
inner.state = Idle(Some(buf));
|
inner.state = State::Idle(Some(buf));
|
||||||
}
|
}
|
||||||
Operation::Seek(result) => {
|
Operation::Seek(result) => {
|
||||||
assert!(buf.is_empty());
|
assert!(buf.is_empty());
|
||||||
inner.state = Idle(Some(buf));
|
inner.state = State::Idle(Some(buf));
|
||||||
if let Ok(pos) = result {
|
if let Ok(pos) = result {
|
||||||
inner.pos = pos;
|
inner.pos = pos;
|
||||||
}
|
}
|
||||||
@@ -595,11 +593,11 @@ impl AsyncSeek for File {
|
|||||||
let inner = me.inner.get_mut();
|
let inner = me.inner.get_mut();
|
||||||
|
|
||||||
match inner.state {
|
match inner.state {
|
||||||
Busy(_) => Err(io::Error::new(
|
State::Busy(_) => Err(io::Error::new(
|
||||||
io::ErrorKind::Other,
|
io::ErrorKind::Other,
|
||||||
"other file operation is pending, call poll_complete before start_seek",
|
"other file operation is pending, call poll_complete before start_seek",
|
||||||
)),
|
)),
|
||||||
Idle(ref mut buf_cell) => {
|
State::Idle(ref mut buf_cell) => {
|
||||||
let mut buf = buf_cell.take().unwrap();
|
let mut buf = buf_cell.take().unwrap();
|
||||||
|
|
||||||
// Factor in any unread data from the buf
|
// Factor in any unread data from the buf
|
||||||
@@ -613,7 +611,7 @@ impl AsyncSeek for File {
|
|||||||
|
|
||||||
let std = me.std.clone();
|
let std = me.std.clone();
|
||||||
|
|
||||||
inner.state = Busy(spawn_blocking(move || {
|
inner.state = State::Busy(spawn_blocking(move || {
|
||||||
let res = (&*std).seek(pos);
|
let res = (&*std).seek(pos);
|
||||||
(Operation::Seek(res), buf)
|
(Operation::Seek(res), buf)
|
||||||
}));
|
}));
|
||||||
@@ -628,10 +626,10 @@ impl AsyncSeek for File {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
match inner.state {
|
match inner.state {
|
||||||
Idle(_) => return Poll::Ready(Ok(inner.pos)),
|
State::Idle(_) => return Poll::Ready(Ok(inner.pos)),
|
||||||
Busy(ref mut rx) => {
|
State::Busy(ref mut rx) => {
|
||||||
let (op, buf) = ready!(Pin::new(rx).poll(cx))?;
|
let (op, buf) = ready!(Pin::new(rx).poll(cx))?;
|
||||||
inner.state = Idle(Some(buf));
|
inner.state = State::Idle(Some(buf));
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
Operation::Read(_) => {}
|
Operation::Read(_) => {}
|
||||||
@@ -644,7 +642,7 @@ impl AsyncSeek for File {
|
|||||||
if let Ok(pos) = res {
|
if let Ok(pos) = res {
|
||||||
inner.pos = pos;
|
inner.pos = pos;
|
||||||
}
|
}
|
||||||
return Ready(res);
|
return Poll::Ready(res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -664,12 +662,12 @@ impl AsyncWrite for File {
|
|||||||
let inner = me.inner.get_mut();
|
let inner = me.inner.get_mut();
|
||||||
|
|
||||||
if let Some(e) = inner.last_write_err.take() {
|
if let Some(e) = inner.last_write_err.take() {
|
||||||
return Ready(Err(e.into()));
|
return Poll::Ready(Err(e.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match inner.state {
|
match inner.state {
|
||||||
Idle(ref mut buf_cell) => {
|
State::Idle(ref mut buf_cell) => {
|
||||||
let mut buf = buf_cell.take().unwrap();
|
let mut buf = buf_cell.take().unwrap();
|
||||||
|
|
||||||
let seek = if !buf.is_empty() {
|
let seek = if !buf.is_empty() {
|
||||||
@@ -694,13 +692,13 @@ impl AsyncWrite for File {
|
|||||||
io::Error::new(io::ErrorKind::Other, "background task failed")
|
io::Error::new(io::ErrorKind::Other, "background task failed")
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
inner.state = Busy(blocking_task_join_handle);
|
inner.state = State::Busy(blocking_task_join_handle);
|
||||||
|
|
||||||
return Ready(Ok(n));
|
return Poll::Ready(Ok(n));
|
||||||
}
|
}
|
||||||
Busy(ref mut rx) => {
|
State::Busy(ref mut rx) => {
|
||||||
let (op, buf) = ready!(Pin::new(rx).poll(cx))?;
|
let (op, buf) = ready!(Pin::new(rx).poll(cx))?;
|
||||||
inner.state = Idle(Some(buf));
|
inner.state = State::Idle(Some(buf));
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
Operation::Read(_) => {
|
Operation::Read(_) => {
|
||||||
@@ -735,12 +733,12 @@ impl AsyncWrite for File {
|
|||||||
let inner = me.inner.get_mut();
|
let inner = me.inner.get_mut();
|
||||||
|
|
||||||
if let Some(e) = inner.last_write_err.take() {
|
if let Some(e) = inner.last_write_err.take() {
|
||||||
return Ready(Err(e.into()));
|
return Poll::Ready(Err(e.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match inner.state {
|
match inner.state {
|
||||||
Idle(ref mut buf_cell) => {
|
State::Idle(ref mut buf_cell) => {
|
||||||
let mut buf = buf_cell.take().unwrap();
|
let mut buf = buf_cell.take().unwrap();
|
||||||
|
|
||||||
let seek = if !buf.is_empty() {
|
let seek = if !buf.is_empty() {
|
||||||
@@ -765,13 +763,13 @@ impl AsyncWrite for File {
|
|||||||
io::Error::new(io::ErrorKind::Other, "background task failed")
|
io::Error::new(io::ErrorKind::Other, "background task failed")
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
inner.state = Busy(blocking_task_join_handle);
|
inner.state = State::Busy(blocking_task_join_handle);
|
||||||
|
|
||||||
return Ready(Ok(n));
|
return Poll::Ready(Ok(n));
|
||||||
}
|
}
|
||||||
Busy(ref mut rx) => {
|
State::Busy(ref mut rx) => {
|
||||||
let (op, buf) = ready!(Pin::new(rx).poll(cx))?;
|
let (op, buf) = ready!(Pin::new(rx).poll(cx))?;
|
||||||
inner.state = Idle(Some(buf));
|
inner.state = State::Idle(Some(buf));
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
Operation::Read(_) => {
|
Operation::Read(_) => {
|
||||||
@@ -896,21 +894,21 @@ impl Inner {
|
|||||||
|
|
||||||
fn poll_flush(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
fn poll_flush(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
|
||||||
if let Some(e) = self.last_write_err.take() {
|
if let Some(e) = self.last_write_err.take() {
|
||||||
return Ready(Err(e.into()));
|
return Poll::Ready(Err(e.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let (op, buf) = match self.state {
|
let (op, buf) = match self.state {
|
||||||
Idle(_) => return Ready(Ok(())),
|
State::Idle(_) => return Poll::Ready(Ok(())),
|
||||||
Busy(ref mut rx) => ready!(Pin::new(rx).poll(cx))?,
|
State::Busy(ref mut rx) => ready!(Pin::new(rx).poll(cx))?,
|
||||||
};
|
};
|
||||||
|
|
||||||
// The buffer is not used here
|
// The buffer is not used here
|
||||||
self.state = Idle(Some(buf));
|
self.state = State::Idle(Some(buf));
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
Operation::Read(_) => Ready(Ok(())),
|
Operation::Read(_) => Poll::Ready(Ok(())),
|
||||||
Operation::Write(res) => Ready(res),
|
Operation::Write(res) => Poll::Ready(res),
|
||||||
Operation::Seek(_) => Ready(Ok(())),
|
Operation::Seek(_) => Poll::Ready(Ok(())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -124,12 +124,12 @@ impl<T> Future for JoinHandle<T> {
|
|||||||
type Output = Result<T, io::Error>;
|
type Output = Result<T, io::Error>;
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
use std::task::Poll::*;
|
use std::task::Poll;
|
||||||
|
|
||||||
match Pin::new(&mut self.rx).poll(cx) {
|
match Pin::new(&mut self.rx).poll(cx) {
|
||||||
Ready(Ok(v)) => Ready(Ok(v)),
|
Poll::Ready(Ok(v)) => Poll::Ready(Ok(v)),
|
||||||
Ready(Err(e)) => panic!("error = {:?}", e),
|
Poll::Ready(Err(e)) => panic!("error = {:?}", e),
|
||||||
Pending => Pending,
|
Poll::Pending => Poll::Pending,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-21
@@ -6,11 +6,8 @@ use std::future::Future;
|
|||||||
use std::io;
|
use std::io;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::Poll::*;
|
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use self::State::*;
|
|
||||||
|
|
||||||
/// `T` should not implement _both_ Read and Write.
|
/// `T` should not implement _both_ Read and Write.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct Blocking<T> {
|
pub(crate) struct Blocking<T> {
|
||||||
@@ -58,38 +55,38 @@ where
|
|||||||
) -> Poll<io::Result<()>> {
|
) -> Poll<io::Result<()>> {
|
||||||
loop {
|
loop {
|
||||||
match self.state {
|
match self.state {
|
||||||
Idle(ref mut buf_cell) => {
|
State::Idle(ref mut buf_cell) => {
|
||||||
let mut buf = buf_cell.take().unwrap();
|
let mut buf = buf_cell.take().unwrap();
|
||||||
|
|
||||||
if !buf.is_empty() {
|
if !buf.is_empty() {
|
||||||
buf.copy_to(dst);
|
buf.copy_to(dst);
|
||||||
*buf_cell = Some(buf);
|
*buf_cell = Some(buf);
|
||||||
return Ready(Ok(()));
|
return Poll::Ready(Ok(()));
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.ensure_capacity_for(dst);
|
buf.ensure_capacity_for(dst);
|
||||||
let mut inner = self.inner.take().unwrap();
|
let mut inner = self.inner.take().unwrap();
|
||||||
|
|
||||||
self.state = Busy(sys::run(move || {
|
self.state = State::Busy(sys::run(move || {
|
||||||
let res = buf.read_from(&mut inner);
|
let res = buf.read_from(&mut inner);
|
||||||
(res, buf, inner)
|
(res, buf, inner)
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
Busy(ref mut rx) => {
|
State::Busy(ref mut rx) => {
|
||||||
let (res, mut buf, inner) = ready!(Pin::new(rx).poll(cx))?;
|
let (res, mut buf, inner) = ready!(Pin::new(rx).poll(cx))?;
|
||||||
self.inner = Some(inner);
|
self.inner = Some(inner);
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
buf.copy_to(dst);
|
buf.copy_to(dst);
|
||||||
self.state = Idle(Some(buf));
|
self.state = State::Idle(Some(buf));
|
||||||
return Ready(Ok(()));
|
return Poll::Ready(Ok(()));
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
assert!(buf.is_empty());
|
assert!(buf.is_empty());
|
||||||
|
|
||||||
self.state = Idle(Some(buf));
|
self.state = State::Idle(Some(buf));
|
||||||
return Ready(Err(e));
|
return Poll::Ready(Err(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -109,7 +106,7 @@ where
|
|||||||
) -> Poll<io::Result<usize>> {
|
) -> Poll<io::Result<usize>> {
|
||||||
loop {
|
loop {
|
||||||
match self.state {
|
match self.state {
|
||||||
Idle(ref mut buf_cell) => {
|
State::Idle(ref mut buf_cell) => {
|
||||||
let mut buf = buf_cell.take().unwrap();
|
let mut buf = buf_cell.take().unwrap();
|
||||||
|
|
||||||
assert!(buf.is_empty());
|
assert!(buf.is_empty());
|
||||||
@@ -117,7 +114,7 @@ where
|
|||||||
let n = buf.copy_from(src);
|
let n = buf.copy_from(src);
|
||||||
let mut inner = self.inner.take().unwrap();
|
let mut inner = self.inner.take().unwrap();
|
||||||
|
|
||||||
self.state = Busy(sys::run(move || {
|
self.state = State::Busy(sys::run(move || {
|
||||||
let n = buf.len();
|
let n = buf.len();
|
||||||
let res = buf.write_to(&mut inner).map(|_| n);
|
let res = buf.write_to(&mut inner).map(|_| n);
|
||||||
|
|
||||||
@@ -125,11 +122,11 @@ where
|
|||||||
}));
|
}));
|
||||||
self.need_flush = true;
|
self.need_flush = true;
|
||||||
|
|
||||||
return Ready(Ok(n));
|
return Poll::Ready(Ok(n));
|
||||||
}
|
}
|
||||||
Busy(ref mut rx) => {
|
State::Busy(ref mut rx) => {
|
||||||
let (res, buf, inner) = ready!(Pin::new(rx).poll(cx))?;
|
let (res, buf, inner) = ready!(Pin::new(rx).poll(cx))?;
|
||||||
self.state = Idle(Some(buf));
|
self.state = State::Idle(Some(buf));
|
||||||
self.inner = Some(inner);
|
self.inner = Some(inner);
|
||||||
|
|
||||||
// If error, return
|
// If error, return
|
||||||
@@ -144,24 +141,24 @@ where
|
|||||||
let need_flush = self.need_flush;
|
let need_flush = self.need_flush;
|
||||||
match self.state {
|
match self.state {
|
||||||
// The buffer is not used here
|
// The buffer is not used here
|
||||||
Idle(ref mut buf_cell) => {
|
State::Idle(ref mut buf_cell) => {
|
||||||
if need_flush {
|
if need_flush {
|
||||||
let buf = buf_cell.take().unwrap();
|
let buf = buf_cell.take().unwrap();
|
||||||
let mut inner = self.inner.take().unwrap();
|
let mut inner = self.inner.take().unwrap();
|
||||||
|
|
||||||
self.state = Busy(sys::run(move || {
|
self.state = State::Busy(sys::run(move || {
|
||||||
let res = inner.flush().map(|_| 0);
|
let res = inner.flush().map(|_| 0);
|
||||||
(res, buf, inner)
|
(res, buf, inner)
|
||||||
}));
|
}));
|
||||||
|
|
||||||
self.need_flush = false;
|
self.need_flush = false;
|
||||||
} else {
|
} else {
|
||||||
return Ready(Ok(()));
|
return Poll::Ready(Ok(()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Busy(ref mut rx) => {
|
State::Busy(ref mut rx) => {
|
||||||
let (res, buf, inner) = ready!(Pin::new(rx).poll(cx))?;
|
let (res, buf, inner) = ready!(Pin::new(rx).poll(cx))?;
|
||||||
self.state = Idle(Some(buf));
|
self.state = State::Idle(Some(buf));
|
||||||
self.inner = Some(inner);
|
self.inner = Some(inner);
|
||||||
|
|
||||||
// If error, return
|
// If error, return
|
||||||
|
|||||||
@@ -604,20 +604,19 @@ enum TryCurrentErrorKind {
|
|||||||
|
|
||||||
impl fmt::Debug for TryCurrentErrorKind {
|
impl fmt::Debug for TryCurrentErrorKind {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
use TryCurrentErrorKind::*;
|
|
||||||
match self {
|
match self {
|
||||||
NoContext => f.write_str("NoContext"),
|
TryCurrentErrorKind::NoContext => f.write_str("NoContext"),
|
||||||
ThreadLocalDestroyed => f.write_str("ThreadLocalDestroyed"),
|
TryCurrentErrorKind::ThreadLocalDestroyed => f.write_str("ThreadLocalDestroyed"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for TryCurrentError {
|
impl fmt::Display for TryCurrentError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
use TryCurrentErrorKind::*;
|
use TryCurrentErrorKind as E;
|
||||||
match self.kind {
|
match self.kind {
|
||||||
NoContext => f.write_str(CONTEXT_MISSING_ERROR),
|
E::NoContext => f.write_str(CONTEXT_MISSING_ERROR),
|
||||||
ThreadLocalDestroyed => f.write_str(THREAD_LOCAL_DESTROYED_ERROR),
|
E::ThreadLocalDestroyed => f.write_str(THREAD_LOCAL_DESTROYED_ERROR),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ use std::marker::PhantomPinned;
|
|||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::ptr::NonNull;
|
use std::ptr::NonNull;
|
||||||
use std::sync::atomic::Ordering::*;
|
use std::sync::atomic::Ordering::*;
|
||||||
use std::task::Poll::*;
|
|
||||||
use std::task::{Context, Poll, Waker};
|
use std::task::{Context, Poll, Waker};
|
||||||
use std::{cmp, fmt};
|
use std::{cmp, fmt};
|
||||||
|
|
||||||
@@ -391,7 +390,7 @@ impl Semaphore {
|
|||||||
let mut waiters = loop {
|
let mut waiters = loop {
|
||||||
// Has the semaphore closed?
|
// Has the semaphore closed?
|
||||||
if curr & Self::CLOSED > 0 {
|
if curr & Self::CLOSED > 0 {
|
||||||
return Ready(Err(AcquireError::closed()));
|
return Poll::Ready(Err(AcquireError::closed()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut remaining = 0;
|
let mut remaining = 0;
|
||||||
@@ -436,7 +435,7 @@ impl Semaphore {
|
|||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
return Ready(Ok(()));
|
return Poll::Ready(Ok(()));
|
||||||
} else if lock.is_none() {
|
} else if lock.is_none() {
|
||||||
break self.waiters.lock();
|
break self.waiters.lock();
|
||||||
}
|
}
|
||||||
@@ -448,7 +447,7 @@ impl Semaphore {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if waiters.closed {
|
if waiters.closed {
|
||||||
return Ready(Err(AcquireError::closed()));
|
return Poll::Ready(Err(AcquireError::closed()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(tokio_unstable, feature = "tracing"))]
|
#[cfg(all(tokio_unstable, feature = "tracing"))]
|
||||||
@@ -462,7 +461,7 @@ impl Semaphore {
|
|||||||
|
|
||||||
if node.assign_permits(&mut acquired) {
|
if node.assign_permits(&mut acquired) {
|
||||||
self.add_permits_locked(acquired, waiters);
|
self.add_permits_locked(acquired, waiters);
|
||||||
return Ready(Ok(()));
|
return Poll::Ready(Ok(()));
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(acquired, 0);
|
assert_eq!(acquired, 0);
|
||||||
@@ -494,7 +493,7 @@ impl Semaphore {
|
|||||||
drop(waiters);
|
drop(waiters);
|
||||||
drop(old_waker);
|
drop(old_waker);
|
||||||
|
|
||||||
Pending
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -572,15 +571,15 @@ impl Future for Acquire<'_> {
|
|||||||
let coop = ready!(crate::runtime::coop::poll_proceed(cx));
|
let coop = ready!(crate::runtime::coop::poll_proceed(cx));
|
||||||
|
|
||||||
let result = match semaphore.poll_acquire(cx, needed, node, *queued) {
|
let result = match semaphore.poll_acquire(cx, needed, node, *queued) {
|
||||||
Pending => {
|
Poll::Pending => {
|
||||||
*queued = true;
|
*queued = true;
|
||||||
Pending
|
Poll::Pending
|
||||||
}
|
}
|
||||||
Ready(r) => {
|
Poll::Ready(r) => {
|
||||||
coop.made_progress();
|
coop.made_progress();
|
||||||
r?;
|
r?;
|
||||||
*queued = false;
|
*queued = false;
|
||||||
Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -241,7 +241,7 @@ impl<T, S: Semaphore> Rx<T, S> {
|
|||||||
|
|
||||||
/// Receive the next value
|
/// Receive the next value
|
||||||
pub(crate) fn recv(&mut self, cx: &mut Context<'_>) -> Poll<Option<T>> {
|
pub(crate) fn recv(&mut self, cx: &mut Context<'_>) -> Poll<Option<T>> {
|
||||||
use super::block::Read::*;
|
use super::block::Read;
|
||||||
|
|
||||||
ready!(crate::trace::trace_leaf(cx));
|
ready!(crate::trace::trace_leaf(cx));
|
||||||
|
|
||||||
@@ -254,12 +254,12 @@ impl<T, S: Semaphore> Rx<T, S> {
|
|||||||
macro_rules! try_recv {
|
macro_rules! try_recv {
|
||||||
() => {
|
() => {
|
||||||
match rx_fields.list.pop(&self.inner.tx) {
|
match rx_fields.list.pop(&self.inner.tx) {
|
||||||
Some(Value(value)) => {
|
Some(Read::Value(value)) => {
|
||||||
self.inner.semaphore.add_permit();
|
self.inner.semaphore.add_permit();
|
||||||
coop.made_progress();
|
coop.made_progress();
|
||||||
return Ready(Some(value));
|
return Ready(Some(value));
|
||||||
}
|
}
|
||||||
Some(Closed) => {
|
Some(Read::Closed) => {
|
||||||
// TODO: This check may not be required as it most
|
// TODO: This check may not be required as it most
|
||||||
// likely can only return `true` at this point. A
|
// likely can only return `true` at this point. A
|
||||||
// channel is closed when all tx handles are
|
// channel is closed when all tx handles are
|
||||||
|
|||||||
+11
-15
@@ -888,13 +888,11 @@ impl Notified<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn poll_notified(self: Pin<&mut Self>, waker: Option<&Waker>) -> Poll<()> {
|
fn poll_notified(self: Pin<&mut Self>, waker: Option<&Waker>) -> Poll<()> {
|
||||||
use State::*;
|
|
||||||
|
|
||||||
let (notify, state, notify_waiters_calls, waiter) = self.project();
|
let (notify, state, notify_waiters_calls, waiter) = self.project();
|
||||||
|
|
||||||
'outer_loop: loop {
|
'outer_loop: loop {
|
||||||
match *state {
|
match *state {
|
||||||
Init => {
|
State::Init => {
|
||||||
let curr = notify.state.load(SeqCst);
|
let curr = notify.state.load(SeqCst);
|
||||||
|
|
||||||
// Optimistically try acquiring a pending notification
|
// Optimistically try acquiring a pending notification
|
||||||
@@ -907,7 +905,7 @@ impl Notified<'_> {
|
|||||||
|
|
||||||
if res.is_ok() {
|
if res.is_ok() {
|
||||||
// Acquired the notification
|
// Acquired the notification
|
||||||
*state = Done;
|
*state = State::Done;
|
||||||
continue 'outer_loop;
|
continue 'outer_loop;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -925,7 +923,7 @@ impl Notified<'_> {
|
|||||||
// if notify_waiters has been called after the future
|
// if notify_waiters has been called after the future
|
||||||
// was created, then we are done
|
// was created, then we are done
|
||||||
if get_num_notify_waiters_calls(curr) != *notify_waiters_calls {
|
if get_num_notify_waiters_calls(curr) != *notify_waiters_calls {
|
||||||
*state = Done;
|
*state = State::Done;
|
||||||
continue 'outer_loop;
|
continue 'outer_loop;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -961,7 +959,7 @@ impl Notified<'_> {
|
|||||||
match res {
|
match res {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
// Acquired the notification
|
// Acquired the notification
|
||||||
*state = Done;
|
*state = State::Done;
|
||||||
continue 'outer_loop;
|
continue 'outer_loop;
|
||||||
}
|
}
|
||||||
Err(actual) => {
|
Err(actual) => {
|
||||||
@@ -989,14 +987,14 @@ impl Notified<'_> {
|
|||||||
// Insert the waiter into the linked list
|
// Insert the waiter into the linked list
|
||||||
waiters.push_front(NonNull::from(waiter));
|
waiters.push_front(NonNull::from(waiter));
|
||||||
|
|
||||||
*state = Waiting;
|
*state = State::Waiting;
|
||||||
|
|
||||||
drop(waiters);
|
drop(waiters);
|
||||||
drop(old_waker);
|
drop(old_waker);
|
||||||
|
|
||||||
return Poll::Pending;
|
return Poll::Pending;
|
||||||
}
|
}
|
||||||
Waiting => {
|
State::Waiting => {
|
||||||
#[cfg(tokio_taskdump)]
|
#[cfg(tokio_taskdump)]
|
||||||
if let Some(waker) = waker {
|
if let Some(waker) = waker {
|
||||||
let mut ctx = Context::from_waker(waker);
|
let mut ctx = Context::from_waker(waker);
|
||||||
@@ -1009,7 +1007,7 @@ impl Notified<'_> {
|
|||||||
drop(unsafe { waiter.waker.with_mut(|waker| (*waker).take()) });
|
drop(unsafe { waiter.waker.with_mut(|waker| (*waker).take()) });
|
||||||
|
|
||||||
waiter.notification.clear();
|
waiter.notification.clear();
|
||||||
*state = Done;
|
*state = State::Done;
|
||||||
return Poll::Ready(());
|
return Poll::Ready(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1034,7 +1032,7 @@ impl Notified<'_> {
|
|||||||
drop(waiters);
|
drop(waiters);
|
||||||
drop(old_waker);
|
drop(old_waker);
|
||||||
|
|
||||||
*state = Done;
|
*state = State::Done;
|
||||||
return Poll::Ready(());
|
return Poll::Ready(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1056,7 +1054,7 @@ impl Notified<'_> {
|
|||||||
// The list is used in `notify_waiters`, so it must be guarded.
|
// The list is used in `notify_waiters`, so it must be guarded.
|
||||||
unsafe { waiters.remove(NonNull::from(waiter)) };
|
unsafe { waiters.remove(NonNull::from(waiter)) };
|
||||||
|
|
||||||
*state = Done;
|
*state = State::Done;
|
||||||
} else {
|
} else {
|
||||||
// Safety: we hold the lock, so we can modify the waker.
|
// Safety: we hold the lock, so we can modify the waker.
|
||||||
unsafe {
|
unsafe {
|
||||||
@@ -1090,7 +1088,7 @@ impl Notified<'_> {
|
|||||||
// Drop the old waker after releasing the lock.
|
// Drop the old waker after releasing the lock.
|
||||||
drop(old_waker);
|
drop(old_waker);
|
||||||
}
|
}
|
||||||
Done => {
|
State::Done => {
|
||||||
#[cfg(tokio_taskdump)]
|
#[cfg(tokio_taskdump)]
|
||||||
if let Some(waker) = waker {
|
if let Some(waker) = waker {
|
||||||
let mut ctx = Context::from_waker(waker);
|
let mut ctx = Context::from_waker(waker);
|
||||||
@@ -1113,15 +1111,13 @@ impl Future for Notified<'_> {
|
|||||||
|
|
||||||
impl Drop for Notified<'_> {
|
impl Drop for Notified<'_> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
use State::*;
|
|
||||||
|
|
||||||
// Safety: The type only transitions to a "Waiting" state when pinned.
|
// Safety: The type only transitions to a "Waiting" state when pinned.
|
||||||
let (notify, state, _, waiter) = unsafe { Pin::new_unchecked(self).project() };
|
let (notify, state, _, waiter) = unsafe { Pin::new_unchecked(self).project() };
|
||||||
|
|
||||||
// This is where we ensure safety. The `Notified` value is being
|
// This is where we ensure safety. The `Notified` value is being
|
||||||
// dropped, which means we must ensure that the waiter entry is no
|
// dropped, which means we must ensure that the waiter entry is no
|
||||||
// longer stored in the linked list.
|
// longer stored in the linked list.
|
||||||
if matches!(*state, Waiting) {
|
if matches!(*state, State::Waiting) {
|
||||||
let mut waiters = notify.waiters.lock();
|
let mut waiters = notify.waiters.lock();
|
||||||
let mut notify_state = notify.state.load(SeqCst);
|
let mut notify_state = notify.state.load(SeqCst);
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn smoke() {
|
fn smoke() {
|
||||||
use crate::sync::mpsc::block::Read::*;
|
use crate::sync::mpsc::block::Read;
|
||||||
|
|
||||||
const NUM_TX: usize = 2;
|
const NUM_TX: usize = 2;
|
||||||
const NUM_MSG: usize = 2;
|
const NUM_MSG: usize = 2;
|
||||||
@@ -28,7 +28,7 @@ fn smoke() {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
match rx.pop(&tx) {
|
match rx.pop(&tx) {
|
||||||
Some(Value((th, v))) => {
|
Some(Read::Value((th, v))) => {
|
||||||
assert_eq!(v, next[th]);
|
assert_eq!(v, next[th]);
|
||||||
next[th] += 1;
|
next[th] += 1;
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ fn smoke() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(Closed) => {
|
Some(Read::Closed) => {
|
||||||
panic!();
|
panic!();
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
//! Time error types.
|
//! Time error types.
|
||||||
|
|
||||||
use self::Kind::*;
|
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
@@ -57,7 +56,7 @@ pub(crate) enum InsertError {
|
|||||||
impl Error {
|
impl Error {
|
||||||
/// Creates an error representing a shutdown timer.
|
/// Creates an error representing a shutdown timer.
|
||||||
pub fn shutdown() -> Error {
|
pub fn shutdown() -> Error {
|
||||||
Error(Shutdown)
|
Error(Kind::Shutdown)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the error was caused by the timer being shutdown.
|
/// Returns `true` if the error was caused by the timer being shutdown.
|
||||||
@@ -67,7 +66,7 @@ impl Error {
|
|||||||
|
|
||||||
/// Creates an error representing a timer at capacity.
|
/// Creates an error representing a timer at capacity.
|
||||||
pub fn at_capacity() -> Error {
|
pub fn at_capacity() -> Error {
|
||||||
Error(AtCapacity)
|
Error(Kind::AtCapacity)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the error was caused by the timer being at capacity.
|
/// Returns `true` if the error was caused by the timer being at capacity.
|
||||||
@@ -77,7 +76,7 @@ impl Error {
|
|||||||
|
|
||||||
/// Creates an error representing a misconfigured timer.
|
/// Creates an error representing a misconfigured timer.
|
||||||
pub fn invalid() -> Error {
|
pub fn invalid() -> Error {
|
||||||
Error(Invalid)
|
Error(Kind::Invalid)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the error was caused by the timer being misconfigured.
|
/// Returns `true` if the error was caused by the timer being misconfigured.
|
||||||
@@ -90,11 +89,12 @@ impl error::Error for Error {}
|
|||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
use self::Kind::*;
|
|
||||||
let descr = match self.0 {
|
let descr = match self.0 {
|
||||||
Shutdown => "the timer is shutdown, must be called from the context of Tokio runtime",
|
Kind::Shutdown => {
|
||||||
AtCapacity => "timer is at capacity and cannot create a new entry",
|
"the timer is shutdown, must be called from the context of Tokio runtime"
|
||||||
Invalid => "timer duration exceeds maximum duration",
|
}
|
||||||
|
Kind::AtCapacity => "timer is at capacity and cannot create a new entry",
|
||||||
|
Kind::Invalid => "timer duration exceeds maximum duration",
|
||||||
};
|
};
|
||||||
write!(fmt, "{}", descr)
|
write!(fmt, "{}", descr)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user