Skip to content

Commit 4761299

Browse files
authored
feat: add core-net feature (#30)
* feat: add core-net feature * chore: rustfmt
1 parent 5dc5c87 commit 4761299

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

crates/rlp/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ hex-literal.workspace = true
3333
default = ["std"]
3434
std = ["bytes/std", "arrayvec?/std"]
3535
derive = ["dep:alloy-rlp-derive"]
36+
# Enables `core::net::` implementations always instead of conditionally through `std`.
37+
# Requires Rust 1.77 or newer.
38+
core-net = []
3639

3740
arrayvec = ["dep:arrayvec"]
3841

crates/rlp/src/decode.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,12 @@ where
142142
}
143143
}
144144

145-
#[cfg(feature = "std")]
145+
#[cfg(any(feature = "std", feature = "core-net"))]
146146
mod std_impl {
147147
use super::*;
148+
#[cfg(all(feature = "core-net", not(feature = "std")))]
149+
use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
150+
#[cfg(feature = "std")]
148151
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
149152

150153
impl Decodable for IpAddr {
@@ -175,6 +178,11 @@ mod std_impl {
175178
slice_to_array::<16>(bytes).map(Self::from)
176179
}
177180
}
181+
182+
#[inline]
183+
fn slice_to_array<const N: usize>(slice: &[u8]) -> Result<[u8; N]> {
184+
slice.try_into().map_err(|_| Error::UnexpectedLength)
185+
}
178186
}
179187

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

226-
#[cfg(feature = "std")]
227-
#[inline]
228-
fn slice_to_array<const N: usize>(slice: &[u8]) -> Result<[u8; N]> {
229-
slice.try_into().map_err(|_| Error::UnexpectedLength)
230-
}
231-
232234
#[cfg(test)]
233235
mod tests {
234236
use super::*;

crates/rlp/src/encode.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,12 @@ deref_impl! {
237237
[T: ?Sized + Encodable] alloc::sync::Arc<T>,
238238
}
239239

240-
#[cfg(feature = "std")]
240+
#[cfg(any(feature = "std", feature = "core-net"))]
241241
mod std_support {
242242
use super::*;
243+
#[cfg(all(feature = "core-net", not(feature = "std")))]
244+
use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
245+
#[cfg(feature = "std")]
243246
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
244247

245248
impl Encodable for IpAddr {

0 commit comments

Comments
 (0)