From 4761299a1b249237bf9b3075f7c4560b77a6ae65 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 6 Dec 2024 21:23:59 +0100 Subject: [PATCH] feat: add core-net feature (#30) * feat: add core-net feature * chore: rustfmt --- crates/rlp/Cargo.toml | 3 +++ crates/rlp/src/decode.rs | 16 +++++++++------- crates/rlp/src/encode.rs | 5 ++++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/crates/rlp/Cargo.toml b/crates/rlp/Cargo.toml index 3352711..4566314 100644 --- a/crates/rlp/Cargo.toml +++ b/crates/rlp/Cargo.toml @@ -33,6 +33,9 @@ hex-literal.workspace = true default = ["std"] std = ["bytes/std", "arrayvec?/std"] derive = ["dep:alloy-rlp-derive"] +# Enables `core::net::` implementations always instead of conditionally through `std`. +# Requires Rust 1.77 or newer. +core-net = [] arrayvec = ["dep:arrayvec"] diff --git a/crates/rlp/src/decode.rs b/crates/rlp/src/decode.rs index e668fce..9c9d801 100644 --- a/crates/rlp/src/decode.rs +++ b/crates/rlp/src/decode.rs @@ -142,9 +142,12 @@ where } } -#[cfg(feature = "std")] +#[cfg(any(feature = "std", feature = "core-net"))] mod std_impl { use super::*; + #[cfg(all(feature = "core-net", not(feature = "std")))] + use core::net::{IpAddr, Ipv4Addr, Ipv6Addr}; + #[cfg(feature = "std")] use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; impl Decodable for IpAddr { @@ -175,6 +178,11 @@ mod std_impl { slice_to_array::<16>(bytes).map(Self::from) } } + + #[inline] + fn slice_to_array(slice: &[u8]) -> Result<[u8; N]> { + slice.try_into().map_err(|_| Error::UnexpectedLength) + } } /// Decodes the entire input, ensuring no trailing bytes remain. @@ -223,12 +231,6 @@ pub(crate) fn static_left_pad(data: &[u8]) -> Result<[u8; N]> { Ok(v) } -#[cfg(feature = "std")] -#[inline] -fn slice_to_array(slice: &[u8]) -> Result<[u8; N]> { - slice.try_into().map_err(|_| Error::UnexpectedLength) -} - #[cfg(test)] mod tests { use super::*; diff --git a/crates/rlp/src/encode.rs b/crates/rlp/src/encode.rs index 0fcb0a5..bff533f 100644 --- a/crates/rlp/src/encode.rs +++ b/crates/rlp/src/encode.rs @@ -237,9 +237,12 @@ deref_impl! { [T: ?Sized + Encodable] alloc::sync::Arc, } -#[cfg(feature = "std")] +#[cfg(any(feature = "std", feature = "core-net"))] mod std_support { use super::*; + #[cfg(all(feature = "core-net", not(feature = "std")))] + use core::net::{IpAddr, Ipv4Addr, Ipv6Addr}; + #[cfg(feature = "std")] use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; impl Encodable for IpAddr {