Skip to content

Commit

Permalink
feat: add core-net feature (#30)
Browse files Browse the repository at this point in the history
* feat: add core-net feature

* chore: rustfmt
  • Loading branch information
mattsse authored Dec 6, 2024
1 parent 5dc5c87 commit 4761299
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
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

0 comments on commit 4761299

Please sign in to comment.