Skip to content

Commit 5d1c54c

Browse files
authored
New core (#568)
* New core * Fix lifetime requirements * Remove identify transport * Address &mut & ref ref mut * Fix whitespaces
1 parent 724d0f5 commit 5d1c54c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+850
-1493
lines changed

core/src/either.rs

+45-52
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
use futures::{prelude::*, future};
21+
use futures::prelude::*;
2222
use muxing::{Shutdown, StreamMuxer};
2323
use std::io::{Error as IoError, Read, Write};
2424
use tokio_io::{AsyncRead, AsyncWrite};
25+
use Multiaddr;
2526

2627
/// Implements `AsyncRead` and `AsyncWrite` and dispatches all method calls to
2728
/// either `First` or `Second`.
@@ -39,8 +40,8 @@ where
3940
#[inline]
4041
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
4142
match self {
42-
&EitherOutput::First(ref a) => a.prepare_uninitialized_buffer(buf),
43-
&EitherOutput::Second(ref b) => b.prepare_uninitialized_buffer(buf),
43+
EitherOutput::First(a) => a.prepare_uninitialized_buffer(buf),
44+
EitherOutput::Second(b) => b.prepare_uninitialized_buffer(buf),
4445
}
4546
}
4647
}
@@ -53,8 +54,8 @@ where
5354
#[inline]
5455
fn read(&mut self, buf: &mut [u8]) -> Result<usize, IoError> {
5556
match self {
56-
&mut EitherOutput::First(ref mut a) => a.read(buf),
57-
&mut EitherOutput::Second(ref mut b) => b.read(buf),
57+
EitherOutput::First(a) => a.read(buf),
58+
EitherOutput::Second(b) => b.read(buf),
5859
}
5960
}
6061
}
@@ -67,8 +68,8 @@ where
6768
#[inline]
6869
fn shutdown(&mut self) -> Poll<(), IoError> {
6970
match self {
70-
&mut EitherOutput::First(ref mut a) => a.shutdown(),
71-
&mut EitherOutput::Second(ref mut b) => b.shutdown(),
71+
EitherOutput::First(a) => a.shutdown(),
72+
EitherOutput::Second(b) => b.shutdown(),
7273
}
7374
}
7475
}
@@ -81,16 +82,16 @@ where
8182
#[inline]
8283
fn write(&mut self, buf: &[u8]) -> Result<usize, IoError> {
8384
match self {
84-
&mut EitherOutput::First(ref mut a) => a.write(buf),
85-
&mut EitherOutput::Second(ref mut b) => b.write(buf),
85+
EitherOutput::First(a) => a.write(buf),
86+
EitherOutput::Second(b) => b.write(buf),
8687
}
8788
}
8889

8990
#[inline]
9091
fn flush(&mut self) -> Result<(), IoError> {
9192
match self {
92-
&mut EitherOutput::First(ref mut a) => a.flush(),
93-
&mut EitherOutput::Second(ref mut b) => b.flush(),
93+
EitherOutput::First(a) => a.flush(),
94+
EitherOutput::Second(b) => b.flush(),
9495
}
9596
}
9697
}
@@ -104,16 +105,16 @@ where
104105
type OutboundSubstream = EitherOutbound<A, B>;
105106

106107
fn poll_inbound(&self) -> Poll<Option<Self::Substream>, IoError> {
107-
match *self {
108-
EitherOutput::First(ref inner) => inner.poll_inbound().map(|p| p.map(|o| o.map(EitherOutput::First))),
109-
EitherOutput::Second(ref inner) => inner.poll_inbound().map(|p| p.map(|o| o.map(EitherOutput::Second))),
108+
match self {
109+
EitherOutput::First(inner) => inner.poll_inbound().map(|p| p.map(|o| o.map(EitherOutput::First))),
110+
EitherOutput::Second(inner) => inner.poll_inbound().map(|p| p.map(|o| o.map(EitherOutput::Second))),
110111
}
111112
}
112113

113114
fn open_outbound(&self) -> Self::OutboundSubstream {
114-
match *self {
115-
EitherOutput::First(ref inner) => EitherOutbound::A(inner.open_outbound()),
116-
EitherOutput::Second(ref inner) => EitherOutbound::B(inner.open_outbound()),
115+
match self {
116+
EitherOutput::First(inner) => EitherOutbound::A(inner.open_outbound()),
117+
EitherOutput::Second(inner) => EitherOutbound::B(inner.open_outbound()),
117118
}
118119
}
119120

@@ -130,14 +131,14 @@ where
130131
}
131132

132133
fn destroy_outbound(&self, substream: Self::OutboundSubstream) {
133-
match *self {
134-
EitherOutput::First(ref inner) => {
134+
match self {
135+
EitherOutput::First(inner) => {
135136
match substream {
136137
EitherOutbound::A(substream) => inner.destroy_outbound(substream),
137138
_ => panic!("Wrong API usage")
138139
}
139140
},
140-
EitherOutput::Second(ref inner) => {
141+
EitherOutput::Second(inner) => {
141142
match substream {
142143
EitherOutbound::B(substream) => inner.destroy_outbound(substream),
143144
_ => panic!("Wrong API usage")
@@ -195,14 +196,14 @@ where
195196
}
196197

197198
fn destroy_substream(&self, substream: Self::Substream) {
198-
match *self {
199-
EitherOutput::First(ref inner) => {
199+
match self {
200+
EitherOutput::First(inner) => {
200201
match substream {
201202
EitherOutput::First(substream) => inner.destroy_substream(substream),
202203
_ => panic!("Wrong API usage")
203204
}
204205
},
205-
EitherOutput::Second(ref inner) => {
206+
EitherOutput::Second(inner) => {
206207
match substream {
207208
EitherOutput::Second(substream) => inner.destroy_substream(substream),
208209
_ => panic!("Wrong API usage")
@@ -212,16 +213,16 @@ where
212213
}
213214

214215
fn shutdown(&self, kind: Shutdown) -> Poll<(), IoError> {
215-
match *self {
216-
EitherOutput::First(ref inner) => inner.shutdown(kind),
217-
EitherOutput::Second(ref inner) => inner.shutdown(kind)
216+
match self {
217+
EitherOutput::First(inner) => inner.shutdown(kind),
218+
EitherOutput::Second(inner) => inner.shutdown(kind)
218219
}
219220
}
220221

221222
fn flush_all(&self) -> Poll<(), IoError> {
222-
match *self {
223-
EitherOutput::First(ref inner) => inner.flush_all(),
224-
EitherOutput::Second(ref inner) => inner.flush_all()
223+
match self {
224+
EitherOutput::First(inner) => inner.flush_all(),
225+
EitherOutput::Second(inner) => inner.flush_all()
225226
}
226227
}
227228
}
@@ -243,52 +244,44 @@ pub enum EitherListenStream<A, B> {
243244

244245
impl<AStream, BStream, AInner, BInner> Stream for EitherListenStream<AStream, BStream>
245246
where
246-
AStream: Stream<Item = AInner, Error = IoError>,
247-
BStream: Stream<Item = BInner, Error = IoError>,
247+
AStream: Stream<Item = (AInner, Multiaddr), Error = IoError>,
248+
BStream: Stream<Item = (BInner, Multiaddr), Error = IoError>,
248249
{
249-
type Item = EitherListenUpgrade<AInner, BInner>;
250+
type Item = (EitherFuture<AInner, BInner>, Multiaddr);
250251
type Error = IoError;
251252

252253
#[inline]
253254
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
254255
match self {
255-
&mut EitherListenStream::First(ref mut a) => a.poll()
256-
.map(|i| i.map(|v| v.map(EitherListenUpgrade::First))),
257-
&mut EitherListenStream::Second(ref mut a) => a.poll()
258-
.map(|i| i.map(|v| v.map(EitherListenUpgrade::Second))),
256+
EitherListenStream::First(a) => a.poll()
257+
.map(|i| (i.map(|v| (v.map(|(o, addr)| (EitherFuture::First(o), addr)))))),
258+
EitherListenStream::Second(a) => a.poll()
259+
.map(|i| (i.map(|v| (v.map(|(o, addr)| (EitherFuture::Second(o), addr)))))),
259260
}
260261
}
261262
}
262263

263-
// TODO: This type is needed because of the lack of `impl Trait` in stable Rust.
264-
// If Rust had impl Trait we could use the Either enum from the futures crate and add some
265-
// modifiers to it. This custom enum is a combination of Either and these modifiers.
264+
/// Implements `Future` and dispatches all method calls to either `First` or `Second`.
266265
#[derive(Debug, Copy, Clone)]
267266
#[must_use = "futures do nothing unless polled"]
268-
pub enum EitherListenUpgrade<A, B> {
267+
pub enum EitherFuture<A, B> {
269268
First(A),
270269
Second(B),
271270
}
272271

273-
impl<A, B, Ao, Bo, Af, Bf> Future for EitherListenUpgrade<A, B>
272+
impl<AFuture, BFuture, AInner, BInner> Future for EitherFuture<AFuture, BFuture>
274273
where
275-
A: Future<Item = (Ao, Af), Error = IoError>,
276-
B: Future<Item = (Bo, Bf), Error = IoError>,
274+
AFuture: Future<Item = AInner, Error = IoError>,
275+
BFuture: Future<Item = BInner, Error = IoError>,
277276
{
278-
type Item = (EitherOutput<Ao, Bo>, future::Either<Af, Bf>);
277+
type Item = EitherOutput<AInner, BInner>;
279278
type Error = IoError;
280279

281280
#[inline]
282281
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
283282
match self {
284-
&mut EitherListenUpgrade::First(ref mut a) => {
285-
let (item, addr) = try_ready!(a.poll());
286-
Ok(Async::Ready((EitherOutput::First(item), future::Either::A(addr))))
287-
}
288-
&mut EitherListenUpgrade::Second(ref mut b) => {
289-
let (item, addr) = try_ready!(b.poll());
290-
Ok(Async::Ready((EitherOutput::Second(item), future::Either::B(addr))))
291-
}
283+
EitherFuture::First(a) => a.poll().map(|v| v.map(EitherOutput::First)),
284+
EitherFuture::Second(a) => a.poll().map(|v| v.map(EitherOutput::Second)),
292285
}
293286
}
294287
}

core/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
//! // TODO: right now the only available protocol is ping, but we want to replace it with
143143
//! // something that is more simple to use
144144
//! .dial("127.0.0.1:12345".parse::<libp2p_core::Multiaddr>().unwrap()).unwrap_or_else(|_| panic!())
145-
//! .and_then(|(out, _)| {
145+
//! .and_then(|out| {
146146
//! match out {
147147
//! PingOutput::Ponger(processing) => Box::new(processing) as Box<Future<Item = _, Error = _>>,
148148
//! PingOutput::Pinger(mut pinger) => {
@@ -220,5 +220,5 @@ pub use self::multiaddr::Multiaddr;
220220
pub use self::muxing::StreamMuxer;
221221
pub use self::peer_id::PeerId;
222222
pub use self::public_key::PublicKey;
223-
pub use self::transport::{MuxedTransport, Transport};
223+
pub use self::transport::Transport;
224224
pub use self::upgrade::{ConnectionUpgrade, Endpoint};

0 commit comments

Comments
 (0)