Skip to content

Commit

Permalink
chore: deprecate void crate (libp2p#5676)
Browse files Browse the repository at this point in the history
## Description

<!--
Please write a summary of your changes and why you made them.
This section will appear as the commit message after merging.
Please craft it accordingly.
For a quick primer on good commit messages, check out this blog post:
https://cbea.ms/git-commit/

Please include any relevant issues in here, for example:

Related https://github.com/libp2p/rust-libp2p/issues/ABCD.
Fixes https://github.com/libp2p/rust-libp2p/issues/XYZ.
-->

The `void` crate provides a `Void` type that is conceptually equivalent
to the [`never`
type(!)](https://doc.rust-lang.org/std/primitive.never.html). This PR
tries to remove `void` crate from the dependency tree by replacing
`void::Void` with
[`std::convert::Infallible`](https://doc.rust-lang.org/std/convert/enum.Infallible.html)
that will eventually become an alias of the `never` type(!)

> This enum has the same role as [the ! “never”
type](https://doc.rust-lang.org/std/primitive.never.html), which is
unstable in this version of Rust. When ! is stabilized, we plan to make
Infallible a type alias to it:

## Notes & open questions

<!--
Any notes, remarks or open questions you have to make about the PR which
don't need to go into the final commit message.
-->

## Change checklist

<!-- Please add a Changelog entry in the appropriate crates and bump the
crate versions if needed. See
<https://github.com/libp2p/rust-libp2p/blob/master/docs/release.md#development-between-releases>-->

- [x] I have performed a self-review of my own code
- [ ] I have made corresponding changes to the documentation
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] A changelog entry has been made in the appropriate crates
  • Loading branch information
hanabi1224 authored Nov 14, 2024
1 parent 4192fc3 commit 0c34d9f
Show file tree
Hide file tree
Showing 65 changed files with 168 additions and 193 deletions.
26 changes: 0 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ smallvec = "1.13.2"
thiserror = "1.0"
tracing = { workspace = true }
unsigned-varint = { workspace = true }
void = "1"

[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
Expand Down
10 changes: 10 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ pub use upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct DecodeError(quick_protobuf::Error);

pub mod util {
use std::convert::Infallible;

/// A safe version of [`std::intrinsics::unreachable`].
#[inline(always)]
pub fn unreachable(x: Infallible) -> ! {
match x {}
}
}
10 changes: 5 additions & 5 deletions core/src/upgrade/denied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use futures::future;
use std::convert::Infallible;
use std::iter;
use void::Void;

/// Dummy implementation of `UpgradeInfo`/`InboundUpgrade`/`OutboundUpgrade` that doesn't support
/// any protocol.
Expand All @@ -38,8 +38,8 @@ impl UpgradeInfo for DeniedUpgrade {
}

impl<C> InboundUpgrade<C> for DeniedUpgrade {
type Output = Void;
type Error = Void;
type Output = Infallible;
type Error = Infallible;
type Future = future::Pending<Result<Self::Output, Self::Error>>;

fn upgrade_inbound(self, _: C, _: Self::Info) -> Self::Future {
Expand All @@ -48,8 +48,8 @@ impl<C> InboundUpgrade<C> for DeniedUpgrade {
}

impl<C> OutboundUpgrade<C> for DeniedUpgrade {
type Output = Void;
type Error = Void;
type Output = Infallible;
type Error = Infallible;
type Future = future::Pending<Result<Self::Output, Self::Error>>;

fn upgrade_outbound(self, _: C, _: Self::Info) -> Self::Future {
Expand Down
10 changes: 5 additions & 5 deletions core/src/upgrade/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use futures::future;
use std::convert::Infallible;
use std::iter;
use void::Void;

/// Implementation of [`UpgradeInfo`], [`InboundUpgrade`] and [`OutboundUpgrade`] that always
/// returns a pending upgrade.
Expand Down Expand Up @@ -53,8 +53,8 @@ impl<C, P> InboundUpgrade<C> for PendingUpgrade<P>
where
P: AsRef<str> + Clone,
{
type Output = Void;
type Error = Void;
type Output = Infallible;
type Error = Infallible;
type Future = future::Pending<Result<Self::Output, Self::Error>>;

fn upgrade_inbound(self, _: C, _: Self::Info) -> Self::Future {
Expand All @@ -66,8 +66,8 @@ impl<C, P> OutboundUpgrade<C> for PendingUpgrade<P>
where
P: AsRef<str> + Clone,
{
type Output = Void;
type Error = Void;
type Output = Infallible;
type Error = Infallible;
type Future = future::Pending<Result<Self::Output, Self::Error>>;

fn upgrade_outbound(self, _: C, _: Self::Info) -> Self::Future {
Expand Down
6 changes: 3 additions & 3 deletions core/src/upgrade/ready.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use futures::future;
use std::convert::Infallible;
use std::iter;
use void::Void;

/// Implementation of [`UpgradeInfo`], [`InboundUpgrade`] and [`OutboundUpgrade`] that directly yields the substream.
#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -53,7 +53,7 @@ where
P: AsRef<str> + Clone,
{
type Output = C;
type Error = Void;
type Error = Infallible;
type Future = future::Ready<Result<Self::Output, Self::Error>>;

fn upgrade_inbound(self, stream: C, _: Self::Info) -> Self::Future {
Expand All @@ -66,7 +66,7 @@ where
P: AsRef<str> + Clone,
{
type Output = C;
type Error = Void;
type Error = Infallible;
type Future = future::Ready<Result<Self::Output, Self::Error>>;

fn upgrade_outbound(self, stream: C, _: Self::Info) -> Self::Future {
Expand Down
1 change: 0 additions & 1 deletion examples/file-sharing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ futures = { workspace = true }
libp2p = { path = "../../libp2p", features = [ "tokio", "cbor", "dns", "kad", "noise", "macros", "request-response", "tcp", "websocket", "yamux"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
void = "1.0.2"

[lints]
workspace = true
3 changes: 1 addition & 2 deletions identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ sec1 = { version = "0.7", default-features = false, optional = true }
serde = { version = "1", optional = true, features = ["derive"] }
sha2 = { version = "0.10.8", optional = true }
thiserror = { version = "1.0", optional = true }
void = { version = "1.0", optional = true }
zeroize = { version = "1.8", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ring = { workspace = true, features = ["alloc", "std"], optional = true }

[features]
secp256k1 = ["dep:libsecp256k1", "dep:asn1_der", "dep:sha2", "dep:hkdf", "dep:zeroize"]
ecdsa = ["dep:p256", "dep:void", "dep:zeroize", "dep:sec1", "dep:sha2", "dep:hkdf"]
ecdsa = ["dep:p256", "dep:zeroize", "dep:sec1", "dep:sha2", "dep:hkdf"]
rsa = ["dep:ring", "dep:asn1_der", "dep:rand", "dep:zeroize"]
ed25519 = ["dep:ed25519-dalek", "dep:zeroize", "dep:sha2", "dep:hkdf"]
peerid = ["dep:multihash", "dep:bs58", "dep:thiserror", "dep:sha2", "dep:hkdf"]
Expand Down
7 changes: 5 additions & 2 deletions identity/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use p256::{
EncodedPoint,
};
use sec1::{DecodeEcPrivateKey, EncodeEcPrivateKey};
use void::Void;
use std::convert::Infallible;
use zeroize::Zeroize;

/// An ECDSA keypair generated using `secp256r1` curve.
Expand Down Expand Up @@ -182,7 +182,10 @@ impl PublicKey {
/// Try to decode a public key from a DER encoded byte buffer as defined by SEC1 standard.
pub fn try_decode_der(k: &[u8]) -> Result<PublicKey, DecodingError> {
let buf = Self::del_asn1_header(k).ok_or_else(|| {
DecodingError::failed_to_parse::<Void, _>("ASN.1-encoded ecdsa p256 public key", None)
DecodingError::failed_to_parse::<Infallible, _>(
"ASN.1-encoded ecdsa p256 public key",
None,
)
})?;
Self::try_from_bytes(buf)
}
Expand Down
1 change: 0 additions & 1 deletion misc/allow-block-list/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ categories = ["network-programming", "asynchronous"]
libp2p-core = { workspace = true }
libp2p-swarm = { workspace = true }
libp2p-identity = { workspace = true, features = ["peerid"] }
void = "1"

[dev-dependencies]
async-std = { version = "1.12.0", features = ["attributes"] }
Expand Down
6 changes: 3 additions & 3 deletions misc/allow-block-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ use libp2p_swarm::{
THandlerInEvent, THandlerOutEvent, ToSwarm,
};
use std::collections::{HashSet, VecDeque};
use std::convert::Infallible;
use std::fmt;
use std::task::{Context, Poll, Waker};
use void::Void;

/// A [`NetworkBehaviour`] that can act as an allow or block list.
#[derive(Default, Debug)]
Expand Down Expand Up @@ -222,7 +222,7 @@ where
S: Enforce,
{
type ConnectionHandler = dummy::ConnectionHandler;
type ToSwarm = Void;
type ToSwarm = Infallible;

fn handle_established_inbound_connection(
&mut self,
Expand Down Expand Up @@ -273,7 +273,7 @@ where
) {
// TODO: remove when Rust 1.82 is MSRV
#[allow(unreachable_patterns)]
void::unreachable(event)
libp2p_core::util::unreachable(event)
}

fn poll(
Expand Down
1 change: 0 additions & 1 deletion misc/connection-limits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ categories = ["network-programming", "asynchronous"]
libp2p-core = { workspace = true }
libp2p-swarm = { workspace = true }
libp2p-identity = { workspace = true, features = ["peerid"] }
void = "1"

[dev-dependencies]
async-std = { version = "1.12.0", features = ["attributes"] }
Expand Down
Loading

0 comments on commit 0c34d9f

Please sign in to comment.