Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core)!: remove EitherUpgrade #3339

Merged
merged 20 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3a84c14
Use single quotes to avoid running commands
thomaseizinger Jan 11, 2023
458aa53
Use intermediary variable
thomaseizinger Jan 13, 2023
341d096
Merge branch 'master' into no-run-title-as-command
thomaseizinger Jan 16, 2023
aed5363
refactor(relay): introduce `Handler::new` functions (#3326)
thomaseizinger Jan 16, 2023
5e97d0e
fix: revert "refactor(relay): introduce `Handler::new` functions" (#3…
thomaseizinger Jan 17, 2023
da97e8f
Replace `EitherError` with `Either`
thomaseizinger Jan 17, 2023
2ce805d
Add changelog entry
thomaseizinger Jan 17, 2023
0a2729d
Remove `EitherTransport` in favor of `Either`
thomaseizinger Jan 17, 2023
3397266
Update core/CHANGELOG.md
thomaseizinger Jan 17, 2023
5802177
Merge branch '2650-remove-either-error' into 2650-remove-either-trans…
thomaseizinger Jan 17, 2023
7060f4b
Add changelog entry
thomaseizinger Jan 17, 2023
82eb6c2
Merge branch 'no-run-title-as-command' into 2650-remove-either-error
thomaseizinger Jan 17, 2023
955ff80
Merge branch 'master' into no-run-title-as-command
thomaseizinger Jan 17, 2023
bb84906
Merge branch 'no-run-title-as-command' into 2650-remove-either-error
thomaseizinger Jan 17, 2023
fdfaf72
Merge branch '2650-remove-either-error' into 2650-remove-either-trans…
thomaseizinger Jan 17, 2023
59f6465
Update changelog entry
thomaseizinger Jan 17, 2023
8872039
Remove `EitherUpgrade`
thomaseizinger Jan 17, 2023
d3e79cf
Update core/CHANGELOG.md
thomaseizinger Jan 17, 2023
cefc1c7
Merge branch 'master' into 2650-remove-either-upgrade
thomaseizinger Jan 18, 2023
73666f1
Merge branch 'master' into 2650-remove-either-upgrade
mergify[bot] Jan 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,12 @@ jobs:
require_scope: false

- name: Check PR title length
env:
TITLE: ${{ github.event.pull_request.title }}
run: |
title="${{ github.event.pull_request.title }}"
title_length=${#title}
title_length=${#TITLE}
if [ $title_length -gt 72 ]
then
echo "PR title is too long (greater than 72 characters)"
exit 1
fi
fi
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ libp2p-gossipsub = { version = "0.44.0", path = "protocols/gossipsub", optional
[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
async-trait = "0.1"
either = "1.8.0"
env_logger = "0.10.0"
clap = { version = "4.0.13", features = ["derive"] }
tokio = { version = "1.15", features = ["io-util", "io-std", "macros", "rt", "rt-multi-thread"] }
Expand Down
9 changes: 9 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@

- Improve error messages in case keys cannot be decoded because of missing feature flags. See [PR 2972].

- Remove `EitherError` in favor of `either::Either`. See [PR 3337].

- Remove `EitherTransport` in favor of implementing `Transport` on `either::Either`. See [PR 3338].

- Remove `EitherUpgrade` in favor of implementing `UpgradeInfo`, `InboundUpgrade` and `OutboundUpgrade` on `either::Either`. See [PR XXXX].

[PR 3031]: https://github.com/libp2p/rust-libp2p/pull/3031
[PR 3058]: https://github.com/libp2p/rust-libp2p/pull/3058
[PR 3097]: https://github.com/libp2p/rust-libp2p/pull/3097
[PR 3090]: https://github.com/libp2p/rust-libp2p/pull/3090
[PR 2972]: https://github.com/libp2p/rust-libp2p/pull/2972
[PR 3337]: https://github.com/libp2p/rust-libp2p/pull/3337
[PR 3338]: https://github.com/libp2p/rust-libp2p/pull/3338
[PR XXXX]: https://github.com/libp2p/rust-libp2p/pull/XXXX

# 0.37.0

Expand Down
148 changes: 54 additions & 94 deletions core/src/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,13 @@ use crate::{
transport::{ListenerId, Transport, TransportError, TransportEvent},
Multiaddr, ProtocolName,
};
use either::Either;
use futures::{
io::{IoSlice, IoSliceMut},
prelude::*,
};
use pin_project::pin_project;
use std::{fmt, io, pin::Pin, task::Context, task::Poll};

#[derive(Debug, Copy, Clone)]
pub enum EitherError<A, B> {
A(A),
B(B),
}

impl<A, B> fmt::Display for EitherError<A, B>
where
A: fmt::Display,
B: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EitherError::A(a) => a.fmt(f),
EitherError::B(b) => b.fmt(f),
}
}
}

impl<A, B> std::error::Error for EitherError<A, B>
where
A: std::error::Error,
B: std::error::Error,
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
EitherError::A(a) => a.source(),
EitherError::B(b) => b.source(),
}
}
}
use std::{io, pin::Pin, task::Context, task::Poll};

/// Implements `AsyncRead` and `AsyncWrite` and dispatches all method calls to
/// either `First` or `Second`.
Expand Down Expand Up @@ -147,15 +116,15 @@ where
A: TryStream<Ok = I>,
B: TryStream<Ok = I>,
{
type Item = Result<I, EitherError<A::Error, B::Error>>;
type Item = Result<I, Either<A::Error, B::Error>>;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match self.project() {
EitherOutputProj::First(a) => {
TryStream::try_poll_next(a, cx).map(|v| v.map(|r| r.map_err(EitherError::A)))
TryStream::try_poll_next(a, cx).map(|v| v.map(|r| r.map_err(Either::Left)))
}
EitherOutputProj::Second(b) => {
TryStream::try_poll_next(b, cx).map(|v| v.map(|r| r.map_err(EitherError::B)))
TryStream::try_poll_next(b, cx).map(|v| v.map(|r| r.map_err(Either::Right)))
}
}
}
Expand All @@ -166,33 +135,33 @@ where
A: Sink<I>,
B: Sink<I>,
{
type Error = EitherError<A::Error, B::Error>;
type Error = Either<A::Error, B::Error>;

fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match self.project() {
EitherOutputProj::First(a) => Sink::poll_ready(a, cx).map_err(EitherError::A),
EitherOutputProj::Second(b) => Sink::poll_ready(b, cx).map_err(EitherError::B),
EitherOutputProj::First(a) => Sink::poll_ready(a, cx).map_err(Either::Left),
EitherOutputProj::Second(b) => Sink::poll_ready(b, cx).map_err(Either::Right),
}
}

fn start_send(self: Pin<&mut Self>, item: I) -> Result<(), Self::Error> {
match self.project() {
EitherOutputProj::First(a) => Sink::start_send(a, item).map_err(EitherError::A),
EitherOutputProj::Second(b) => Sink::start_send(b, item).map_err(EitherError::B),
EitherOutputProj::First(a) => Sink::start_send(a, item).map_err(Either::Left),
EitherOutputProj::Second(b) => Sink::start_send(b, item).map_err(Either::Right),
}
}

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match self.project() {
EitherOutputProj::First(a) => Sink::poll_flush(a, cx).map_err(EitherError::A),
EitherOutputProj::Second(b) => Sink::poll_flush(b, cx).map_err(EitherError::B),
EitherOutputProj::First(a) => Sink::poll_flush(a, cx).map_err(Either::Left),
EitherOutputProj::Second(b) => Sink::poll_flush(b, cx).map_err(Either::Right),
}
}

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match self.project() {
EitherOutputProj::First(a) => Sink::poll_close(a, cx).map_err(EitherError::A),
EitherOutputProj::Second(b) => Sink::poll_close(b, cx).map_err(EitherError::B),
EitherOutputProj::First(a) => Sink::poll_close(a, cx).map_err(Either::Left),
EitherOutputProj::Second(b) => Sink::poll_close(b, cx).map_err(Either::Right),
}
}
}
Expand All @@ -203,7 +172,7 @@ where
B: StreamMuxer,
{
type Substream = EitherOutput<A::Substream, B::Substream>;
type Error = EitherError<A::Error, B::Error>;
type Error = Either<A::Error, B::Error>;

fn poll_inbound(
self: Pin<&mut Self>,
Expand All @@ -213,11 +182,11 @@ where
EitherOutputProj::First(inner) => inner
.poll_inbound(cx)
.map_ok(EitherOutput::First)
.map_err(EitherError::A),
.map_err(Either::Left),
EitherOutputProj::Second(inner) => inner
.poll_inbound(cx)
.map_ok(EitherOutput::Second)
.map_err(EitherError::B),
.map_err(Either::Right),
}
}

Expand All @@ -229,18 +198,18 @@ where
EitherOutputProj::First(inner) => inner
.poll_outbound(cx)
.map_ok(EitherOutput::First)
.map_err(EitherError::A),
.map_err(Either::Left),
EitherOutputProj::Second(inner) => inner
.poll_outbound(cx)
.map_ok(EitherOutput::Second)
.map_err(EitherError::B),
.map_err(Either::Right),
}
}

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match self.project() {
EitherOutputProj::First(inner) => inner.poll_close(cx).map_err(EitherError::A),
EitherOutputProj::Second(inner) => inner.poll_close(cx).map_err(EitherError::B),
EitherOutputProj::First(inner) => inner.poll_close(cx).map_err(Either::Left),
EitherOutputProj::Second(inner) => inner.poll_close(cx).map_err(Either::Right),
}
}

Expand All @@ -249,8 +218,8 @@ where
cx: &mut Context<'_>,
) -> Poll<Result<StreamMuxerEvent, Self::Error>> {
match self.project() {
EitherOutputProj::First(inner) => inner.poll(cx).map_err(EitherError::A),
EitherOutputProj::Second(inner) => inner.poll(cx).map_err(EitherError::B),
EitherOutputProj::First(inner) => inner.poll(cx).map_err(Either::Left),
EitherOutputProj::Second(inner) => inner.poll(cx).map_err(Either::Right),
}
}
}
Expand All @@ -269,16 +238,16 @@ where
AFuture: TryFuture<Ok = AInner>,
BFuture: TryFuture<Ok = BInner>,
{
type Output = Result<EitherOutput<AInner, BInner>, EitherError<AFuture::Error, BFuture::Error>>;
type Output = Result<EitherOutput<AInner, BInner>, Either<AFuture::Error, BFuture::Error>>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.project() {
EitherFutureProj::First(a) => TryFuture::try_poll(a, cx)
.map_ok(EitherOutput::First)
.map_err(EitherError::A),
.map_err(Either::Left),
EitherFutureProj::Second(a) => TryFuture::try_poll(a, cx)
.map_ok(EitherOutput::Second)
.map_err(EitherError::B),
.map_err(Either::Right),
}
}
}
Expand All @@ -296,16 +265,16 @@ where
AFut: TryFuture<Ok = AItem, Error = AError>,
BFut: TryFuture<Ok = BItem, Error = BError>,
{
type Output = Result<EitherOutput<AItem, BItem>, EitherError<AError, BError>>;
type Output = Result<EitherOutput<AItem, BItem>, Either<AError, BError>>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.project() {
EitherFuture2Proj::A(a) => TryFuture::try_poll(a, cx)
.map_ok(EitherOutput::First)
.map_err(EitherError::A),
.map_err(Either::Left),
EitherFuture2Proj::B(a) => TryFuture::try_poll(a, cx)
.map_ok(EitherOutput::Second)
.map_err(EitherError::B),
.map_err(Either::Right),
}
}
}
Expand All @@ -324,81 +293,72 @@ impl<A: ProtocolName, B: ProtocolName> ProtocolName for EitherName<A, B> {
}
}
}
#[pin_project(project = EitherTransportProj)]
#[derive(Debug)]
#[must_use = "transports do nothing unless polled"]
pub enum EitherTransport<A, B> {
Left(#[pin] A),
Right(#[pin] B),
}

impl<A, B> Transport for EitherTransport<A, B>
impl<A, B> Transport for Either<A, B>
where
B: Transport,
A: Transport,
{
type Output = EitherOutput<A::Output, B::Output>;
type Error = EitherError<A::Error, B::Error>;
type Error = Either<A::Error, B::Error>;
type ListenerUpgrade = EitherFuture<A::ListenerUpgrade, B::ListenerUpgrade>;
type Dial = EitherFuture<A::Dial, B::Dial>;

fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<TransportEvent<Self::ListenerUpgrade, Self::Error>> {
match self.project() {
EitherTransportProj::Left(a) => match a.poll(cx) {
match self.as_pin_mut() {
Either::Left(a) => match a.poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(event) => Poll::Ready(
event
.map_upgrade(EitherFuture::First)
.map_err(EitherError::A),
),
Poll::Ready(event) => {
Poll::Ready(event.map_upgrade(EitherFuture::First).map_err(Either::Left))
}
},
EitherTransportProj::Right(b) => match b.poll(cx) {
Either::Right(b) => match b.poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(event) => Poll::Ready(
event
.map_upgrade(EitherFuture::Second)
.map_err(EitherError::B),
.map_err(Either::Right),
),
},
}
}

fn remove_listener(&mut self, id: ListenerId) -> bool {
match self {
EitherTransport::Left(t) => t.remove_listener(id),
EitherTransport::Right(t) => t.remove_listener(id),
Either::Left(t) => t.remove_listener(id),
Either::Right(t) => t.remove_listener(id),
}
}

fn listen_on(&mut self, addr: Multiaddr) -> Result<ListenerId, TransportError<Self::Error>> {
use TransportError::*;
match self {
EitherTransport::Left(a) => a.listen_on(addr).map_err(|e| match e {
Either::Left(a) => a.listen_on(addr).map_err(|e| match e {
MultiaddrNotSupported(addr) => MultiaddrNotSupported(addr),
Other(err) => Other(EitherError::A(err)),
Other(err) => Other(Either::Left(err)),
}),
EitherTransport::Right(b) => b.listen_on(addr).map_err(|e| match e {
Either::Right(b) => b.listen_on(addr).map_err(|e| match e {
MultiaddrNotSupported(addr) => MultiaddrNotSupported(addr),
Other(err) => Other(EitherError::B(err)),
Other(err) => Other(Either::Right(err)),
}),
}
}

fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
use TransportError::*;
match self {
EitherTransport::Left(a) => match a.dial(addr) {
Either::Left(a) => match a.dial(addr) {
Ok(connec) => Ok(EitherFuture::First(connec)),
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
Err(Other(err)) => Err(Other(EitherError::A(err))),
Err(Other(err)) => Err(Other(Either::Left(err))),
},
EitherTransport::Right(b) => match b.dial(addr) {
Either::Right(b) => match b.dial(addr) {
Ok(connec) => Ok(EitherFuture::Second(connec)),
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
Err(Other(err)) => Err(Other(EitherError::B(err))),
Err(Other(err)) => Err(Other(Either::Right(err))),
},
}
}
Expand All @@ -412,23 +372,23 @@ where
{
use TransportError::*;
match self {
EitherTransport::Left(a) => match a.dial_as_listener(addr) {
Either::Left(a) => match a.dial_as_listener(addr) {
Ok(connec) => Ok(EitherFuture::First(connec)),
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
Err(Other(err)) => Err(Other(EitherError::A(err))),
Err(Other(err)) => Err(Other(Either::Left(err))),
},
EitherTransport::Right(b) => match b.dial_as_listener(addr) {
Either::Right(b) => match b.dial_as_listener(addr) {
Ok(connec) => Ok(EitherFuture::Second(connec)),
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
Err(Other(err)) => Err(Other(EitherError::B(err))),
Err(Other(err)) => Err(Other(Either::Right(err))),
},
}
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
match self {
EitherTransport::Left(a) => a.address_translation(server, observed),
EitherTransport::Right(b) => b.address_translation(server, observed),
Either::Left(a) => a.address_translation(server, observed),
Either::Right(b) => b.address_translation(server, observed),
}
}
}
Loading