Skip to content

Commit 8e736cd

Browse files
committed
Implement serialize/deserialize for core::net instead of std::net if running rust version newer than 1.77, where core::net was stabilized
1 parent 3aca38d commit 8e736cd

File tree

4 files changed

+76
-39
lines changed

4 files changed

+76
-39
lines changed

serde/build.rs

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn main() {
1515

1616
if minor >= 77 {
1717
println!("cargo:rustc-check-cfg=cfg(no_core_cstr)");
18+
println!("cargo:rustc-check-cfg=cfg(no_core_net)");
1819
println!("cargo:rustc-check-cfg=cfg(no_core_num_saturating)");
1920
println!("cargo:rustc-check-cfg=cfg(no_core_try_from)");
2021
println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)");
@@ -86,6 +87,12 @@ fn main() {
8687
println!("cargo:rustc-cfg=no_core_num_saturating");
8788
}
8889

90+
// Support for core::net stabilized in Rust 1.77.
91+
// https://blog.rust-lang.org/2024/03/21/Rust-1.77.0.html
92+
if minor < 77 {
93+
println!("cargo:rustc-cfg=no_core_net");
94+
}
95+
8996
// Support for the `#[diagnostic]` tool attribute namespace
9097
// https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html#diagnostic-attributes
9198
if minor < 78 {

serde/src/de/impls.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,7 @@ macro_rules! parse_ip_impl {
16041604
};
16051605
}
16061606

1607-
#[cfg(feature = "std")]
1607+
#[cfg(any(feature = "std", not(no_core_net)))]
16081608
macro_rules! variant_identifier {
16091609
(
16101610
$name_kind:ident ($($variant:ident; $bytes:expr; $index:expr),*)
@@ -1679,7 +1679,7 @@ macro_rules! variant_identifier {
16791679
}
16801680
}
16811681

1682-
#[cfg(feature = "std")]
1682+
#[cfg(any(feature = "std", not(no_core_net)))]
16831683
macro_rules! deserialize_enum {
16841684
(
16851685
$name:ident $name_kind:ident ($($variant:ident; $bytes:expr; $index:expr),*)
@@ -1716,8 +1716,8 @@ macro_rules! deserialize_enum {
17161716
}
17171717
}
17181718

1719-
#[cfg(feature = "std")]
1720-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
1719+
#[cfg(any(feature = "std", not(no_core_net)))]
1720+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
17211721
impl<'de> Deserialize<'de> for net::IpAddr {
17221722
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
17231723
where
@@ -1737,14 +1737,14 @@ impl<'de> Deserialize<'de> for net::IpAddr {
17371737
}
17381738

17391739
parse_ip_impl! {
1740-
#[cfg(feature = "std")]
1741-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
1740+
#[cfg(any(feature = "std", not(no_core_net)))]
1741+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
17421742
net::Ipv4Addr, "IPv4 address", 4
17431743
}
17441744

17451745
parse_ip_impl! {
1746-
#[cfg(feature = "std")]
1747-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
1746+
#[cfg(any(feature = "std", not(no_core_net)))]
1747+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
17481748
net::Ipv6Addr, "IPv6 address", 16
17491749
}
17501750

@@ -1770,8 +1770,8 @@ macro_rules! parse_socket_impl {
17701770
};
17711771
}
17721772

1773-
#[cfg(feature = "std")]
1774-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
1773+
#[cfg(any(feature = "std", not(no_core_net)))]
1774+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
17751775
impl<'de> Deserialize<'de> for net::SocketAddr {
17761776
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
17771777
where
@@ -1791,15 +1791,15 @@ impl<'de> Deserialize<'de> for net::SocketAddr {
17911791
}
17921792

17931793
parse_socket_impl! {
1794-
#[cfg(feature = "std")]
1795-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
1794+
#[cfg(any(feature = "std", not(no_core_net)))]
1795+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
17961796
net::SocketAddrV4, "IPv4 socket address",
17971797
|(ip, port)| net::SocketAddrV4::new(ip, port),
17981798
}
17991799

18001800
parse_socket_impl! {
1801-
#[cfg(feature = "std")]
1802-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
1801+
#[cfg(any(feature = "std", not(no_core_net)))]
1802+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
18031803
net::SocketAddrV6, "IPv6 socket address",
18041804
|(ip, port)| net::SocketAddrV6::new(ip, port, 0, 0),
18051805
}
@@ -3160,13 +3160,13 @@ atomic_impl! {
31603160
AtomicU64 "64"
31613161
}
31623162

3163-
#[cfg(feature = "std")]
3163+
#[cfg(any(feature = "std", not(no_core_net)))]
31643164
struct FromStrVisitor<T> {
31653165
expecting: &'static str,
31663166
ty: PhantomData<T>,
31673167
}
31683168

3169-
#[cfg(feature = "std")]
3169+
#[cfg(any(feature = "std", not(no_core_net)))]
31703170
impl<T> FromStrVisitor<T> {
31713171
fn new(expecting: &'static str) -> Self {
31723172
FromStrVisitor {
@@ -3176,7 +3176,7 @@ impl<T> FromStrVisitor<T> {
31763176
}
31773177
}
31783178

3179-
#[cfg(feature = "std")]
3179+
#[cfg(any(feature = "std", not(no_core_net)))]
31803180
impl<'de, T> Visitor<'de> for FromStrVisitor<T>
31813181
where
31823182
T: str::FromStr,

serde/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,13 @@ mod lib {
238238
#[cfg(feature = "std")]
239239
pub use std::ffi::CString;
240240

241+
#[cfg(all(not(no_core_net), not(feature = "std")))]
242+
pub use self::core::net;
241243
#[cfg(feature = "std")]
242-
pub use std::{error, net};
244+
pub use std::net;
245+
246+
#[cfg(feature = "std")]
247+
pub use std::error;
243248

244249
#[cfg(feature = "std")]
245250
pub use std::collections::{HashMap, HashSet};

serde/src/ser/impls.rs

+46-21
Original file line numberDiff line numberDiff line change
@@ -769,20 +769,45 @@ impl Serialize for SystemTime {
769769

770770
////////////////////////////////////////////////////////////////////////////////
771771

772+
struct Wrapper<'a> {
773+
pub buf: &'a mut [u8],
774+
pub offset: usize,
775+
}
776+
777+
impl<'a> fmt::Write for Wrapper<'a> {
778+
fn write_str(&mut self, s: &str) -> fmt::Result {
779+
if self.offset > self.buf.len() {
780+
return Err(fmt::Error);
781+
}
782+
let remaining_buf = &mut self.buf[self.offset..];
783+
let raw_s = s.as_bytes();
784+
let write_num = core::cmp::min(raw_s.len(), remaining_buf.len());
785+
remaining_buf[..write_num].copy_from_slice(&raw_s[..write_num]);
786+
self.offset += raw_s.len();
787+
if write_num < raw_s.len() {
788+
Err(fmt::Error)
789+
} else {
790+
Ok(())
791+
}
792+
}
793+
}
794+
772795
/// Serialize a value that implements `Display` as a string, when that string is
773796
/// statically known to never have more than a constant `MAX_LEN` bytes.
774797
///
775798
/// Panics if the `Display` impl tries to write more than `MAX_LEN` bytes.
776-
#[cfg(feature = "std")]
799+
#[cfg(any(feature = "std", not(no_core_net)))]
777800
macro_rules! serialize_display_bounded_length {
778801
($value:expr, $max:expr, $serializer:expr) => {{
779802
let mut buffer = [0u8; $max];
780-
let remaining_len = {
781-
let mut remaining = &mut buffer[..];
782-
write!(remaining, "{}", $value).unwrap();
783-
remaining.len()
803+
let written_len = {
804+
let mut w = Wrapper {
805+
buf: &mut buffer,
806+
offset: 0,
807+
};
808+
write!(&mut w, "{}", $value).unwrap();
809+
w.offset
784810
};
785-
let written_len = buffer.len() - remaining_len;
786811
let written = &buffer[..written_len];
787812

788813
// write! only provides fmt::Formatter to Display implementations, which
@@ -793,8 +818,8 @@ macro_rules! serialize_display_bounded_length {
793818
}};
794819
}
795820

796-
#[cfg(feature = "std")]
797-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
821+
#[cfg(any(feature = "std", not(no_core_net)))]
822+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
798823
impl Serialize for net::IpAddr {
799824
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
800825
where
@@ -818,15 +843,15 @@ impl Serialize for net::IpAddr {
818843
}
819844
}
820845

821-
#[cfg(feature = "std")]
846+
#[cfg(any(feature = "std", not(no_core_net)))]
822847
const DEC_DIGITS_LUT: &[u8] = b"\
823848
0001020304050607080910111213141516171819\
824849
2021222324252627282930313233343536373839\
825850
4041424344454647484950515253545556575859\
826851
6061626364656667686970717273747576777879\
827852
8081828384858687888990919293949596979899";
828853

829-
#[cfg(feature = "std")]
854+
#[cfg(any(feature = "std", not(no_core_net)))]
830855
#[inline]
831856
fn format_u8(mut n: u8, out: &mut [u8]) -> usize {
832857
if n >= 100 {
@@ -847,7 +872,7 @@ fn format_u8(mut n: u8, out: &mut [u8]) -> usize {
847872
}
848873
}
849874

850-
#[cfg(feature = "std")]
875+
#[cfg(any(feature = "std", not(no_core_net)))]
851876
#[test]
852877
fn test_format_u8() {
853878
let mut i = 0u8;
@@ -864,8 +889,8 @@ fn test_format_u8() {
864889
}
865890
}
866891

867-
#[cfg(feature = "std")]
868-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
892+
#[cfg(any(feature = "std", not(no_core_net)))]
893+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
869894
impl Serialize for net::Ipv4Addr {
870895
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
871896
where
@@ -889,8 +914,8 @@ impl Serialize for net::Ipv4Addr {
889914
}
890915
}
891916

892-
#[cfg(feature = "std")]
893-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
917+
#[cfg(any(feature = "std", not(no_core_net)))]
918+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
894919
impl Serialize for net::Ipv6Addr {
895920
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
896921
where
@@ -906,8 +931,8 @@ impl Serialize for net::Ipv6Addr {
906931
}
907932
}
908933

909-
#[cfg(feature = "std")]
910-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
934+
#[cfg(any(feature = "std", not(no_core_net)))]
935+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
911936
impl Serialize for net::SocketAddr {
912937
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
913938
where
@@ -931,8 +956,8 @@ impl Serialize for net::SocketAddr {
931956
}
932957
}
933958

934-
#[cfg(feature = "std")]
935-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
959+
#[cfg(any(feature = "std", not(no_core_net)))]
960+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
936961
impl Serialize for net::SocketAddrV4 {
937962
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
938963
where
@@ -948,8 +973,8 @@ impl Serialize for net::SocketAddrV4 {
948973
}
949974
}
950975

951-
#[cfg(feature = "std")]
952-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
976+
#[cfg(any(feature = "std", not(no_core_net)))]
977+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
953978
impl Serialize for net::SocketAddrV6 {
954979
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
955980
where

0 commit comments

Comments
 (0)