Skip to content

Commit fb6d9ab

Browse files
Replace ConnectionError in public API with io::Error
1 parent 0eb4f5b commit fb6d9ab

7 files changed

Lines changed: 48 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.11.0
2+
3+
- Remove `ConnectionError` from public API in favor `std::io::Error`. See [PR ]
4+
15
# 0.10.1
26

37
- Update `parking_lot` dependency. See [PR 126].

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "yamux"
3-
version = "0.10.1"
3+
version = "0.11.0"
44
authors = ["Parity Technologies <admin@parity.io>"]
55
license = "Apache-2.0 OR MIT"
66
description = "Multiplexer over reliable, ordered connections"

src/connection/control.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
// at https://opensource.org/licenses/MIT.
1010

1111
use super::ControlCommand;
12-
use crate::{error::ConnectionError, Stream};
12+
use crate::error::{into_io_error, ConnectionError};
13+
use crate::Stream;
1314
use futures::{
1415
channel::{mpsc, oneshot},
1516
prelude::*,
1617
ready,
1718
};
1819
use std::{
20+
io,
1921
pin::Pin,
2022
task::{Context, Poll},
2123
};
2224

23-
type Result<T> = std::result::Result<T, ConnectionError>;
24-
2525
/// The Yamux `Connection` controller.
2626
///
2727
/// While a Yamux connection makes progress via its `next_stream` method,
@@ -36,7 +36,7 @@ pub struct Control {
3636
/// Command channel to `Connection`.
3737
sender: mpsc::Sender<ControlCommand>,
3838
/// Pending state of `poll_open_stream`.
39-
pending_open: Option<oneshot::Receiver<Result<Stream>>>,
39+
pending_open: Option<oneshot::Receiver<Result<Stream, ConnectionError>>>,
4040
/// Pending state of `poll_close`.
4141
pending_close: Option<oneshot::Receiver<()>>,
4242
}
@@ -61,14 +61,19 @@ impl Control {
6161
}
6262

6363
/// Open a new stream to the remote.
64-
pub async fn open_stream(&mut self) -> Result<Stream> {
64+
pub async fn open_stream(&mut self) -> io::Result<Stream> {
6565
let (tx, rx) = oneshot::channel();
66-
self.sender.send(ControlCommand::OpenStream(tx)).await?;
67-
rx.await?
66+
self.sender
67+
.send(ControlCommand::OpenStream(tx))
68+
.await
69+
.map_err(into_io_error)?;
70+
let stream = rx.await.map_err(into_io_error)?.map_err(into_io_error)?;
71+
72+
Ok(stream)
6873
}
6974

7075
/// Close the connection.
71-
pub async fn close(&mut self) -> Result<()> {
76+
pub async fn close(&mut self) -> io::Result<()> {
7277
let (tx, rx) = oneshot::channel();
7378
if self
7479
.sender
@@ -86,17 +91,22 @@ impl Control {
8691
}
8792

8893
/// [`Poll`] based alternative to [`Control::open_stream`].
89-
pub fn poll_open_stream(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<Stream>> {
94+
pub fn poll_open_stream(
95+
mut self: Pin<&mut Self>,
96+
cx: &mut Context,
97+
) -> Poll<io::Result<Stream>> {
9098
loop {
9199
match self.pending_open.take() {
92100
None => {
93-
ready!(self.sender.poll_ready(cx)?);
101+
ready!(self.sender.poll_ready(cx).map_err(into_io_error)?);
94102
let (tx, rx) = oneshot::channel();
95-
self.sender.start_send(ControlCommand::OpenStream(tx))?;
103+
self.sender
104+
.start_send(ControlCommand::OpenStream(tx))
105+
.map_err(into_io_error)?;
96106
self.pending_open = Some(rx)
97107
}
98-
Some(mut rx) => match rx.poll_unpin(cx)? {
99-
Poll::Ready(result) => return Poll::Ready(result),
108+
Some(mut rx) => match rx.poll_unpin(cx).map_err(into_io_error)? {
109+
Poll::Ready(result) => return Poll::Ready(result.map_err(into_io_error)),
100110
Poll::Pending => {
101111
self.pending_open = Some(rx);
102112
return Poll::Pending;
@@ -112,7 +122,7 @@ impl Control {
112122
}
113123

114124
/// [`Poll`] based alternative to [`Control::close`].
115-
pub fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>> {
125+
pub fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
116126
loop {
117127
match self.pending_close.take() {
118128
None => {

src/error.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// at https://opensource.org/licenses/MIT.
1010

1111
use crate::frame::FrameDecodeError;
12+
use std::io;
1213

1314
/// The various error cases a connection may encounter.
1415
#[non_exhaustive]
@@ -63,6 +64,15 @@ impl std::error::Error for ConnectionError {
6364
}
6465
}
6566

67+
pub fn into_io_error<E: Into<ConnectionError>>(error: E) -> std::io::Error {
68+
let connection_error = error.into();
69+
70+
match connection_error {
71+
ConnectionError::Io(io) => io,
72+
other => io::Error::new(io::ErrorKind::Other, other),
73+
}
74+
}
75+
6676
impl From<std::io::Error> for ConnectionError {
6777
fn from(e: std::io::Error) -> Self {
6878
ConnectionError::Io(e)

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ mod tests;
3535
pub(crate) mod connection;
3636

3737
pub use crate::connection::{into_stream, Connection, Control, Mode, Packet, Stream};
38-
pub use crate::error::ConnectionError;
3938
pub use crate::frame::{
4039
header::{HeaderDecodeError, StreamId},
4140
FrameDecodeError,

src/tests.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// at https://opensource.org/licenses/MIT.
1010

1111
use crate::WindowUpdateMode;
12-
use crate::{connection::State, Config, Connection, ConnectionError, Control, Mode};
12+
use crate::{connection::State, error::ConnectionError, Config, Connection, Control, Mode};
1313
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
1414
use futures::executor::LocalPool;
1515
use futures::future::join;
@@ -164,7 +164,12 @@ fn prop_max_streams() {
164164
for _ in 0..max_streams {
165165
v.push(control.open_stream().await.expect("open_stream"))
166166
}
167-
if let Err(ConnectionError::TooManyStreams) = control.open_stream().await {
167+
if let Err(Some(Ok(ConnectionError::TooManyStreams))) =
168+
control.open_stream().await.map_err(|e| {
169+
e.into_inner()
170+
.map(|inner| inner.downcast::<ConnectionError>().map(|b| *b))
171+
})
172+
{
168173
true
169174
} else {
170175
false

tests/concurrent.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use futures::{channel::mpsc, prelude::*};
1212
use quickcheck::{Arbitrary, Gen, QuickCheck};
1313
use std::{
14+
io,
1415
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
1516
sync::Arc,
1617
};
@@ -106,7 +107,7 @@ async fn roundtrip(
106107
log::debug!("C: {}: read {} bytes", stream.id(), frame.len());
107108
assert_eq!(&data[..], &frame[..]);
108109
tx.unbounded_send(1).expect("unbounded_send");
109-
Ok::<(), yamux::ConnectionError>(())
110+
Ok::<(), io::Error>(())
110111
});
111112
}
112113
let n = rx

0 commit comments

Comments
 (0)