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

feat: add core-net feature #30

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions crates/rlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
16 changes: 9 additions & 7 deletions crates/rlp/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -175,6 +178,11 @@ mod std_impl {
slice_to_array::<16>(bytes).map(Self::from)
}
}

#[inline]
fn slice_to_array<const N: usize>(slice: &[u8]) -> Result<[u8; N]> {
slice.try_into().map_err(|_| Error::UnexpectedLength)
}
}

/// Decodes the entire input, ensuring no trailing bytes remain.
Expand Down Expand Up @@ -223,12 +231,6 @@ pub(crate) fn static_left_pad<const N: usize>(data: &[u8]) -> Result<[u8; N]> {
Ok(v)
}

#[cfg(feature = "std")]
#[inline]
fn slice_to_array<const N: usize>(slice: &[u8]) -> Result<[u8; N]> {
slice.try_into().map_err(|_| Error::UnexpectedLength)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
5 changes: 4 additions & 1 deletion crates/rlp/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,12 @@ deref_impl! {
[T: ?Sized + Encodable] alloc::sync::Arc<T>,
}

#[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 {
Expand Down
Loading