Skip to content

Commit f2899a9

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 f2899a9

File tree

4 files changed

+78
-39
lines changed

4 files changed

+78
-39
lines changed

serde/build.rs

Lines changed: 7 additions & 0 deletions
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

Lines changed: 17 additions & 17 deletions
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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -769,20 +769,47 @@ impl Serialize for SystemTime {
769769

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

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

788815
// write! only provides fmt::Formatter to Display implementations, which
@@ -793,8 +820,8 @@ macro_rules! serialize_display_bounded_length {
793820
}};
794821
}
795822

796-
#[cfg(feature = "std")]
797-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
823+
#[cfg(any(feature = "std", not(no_core_net)))]
824+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
798825
impl Serialize for net::IpAddr {
799826
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
800827
where
@@ -818,15 +845,15 @@ impl Serialize for net::IpAddr {
818845
}
819846
}
820847

821-
#[cfg(feature = "std")]
848+
#[cfg(any(feature = "std", not(no_core_net)))]
822849
const DEC_DIGITS_LUT: &[u8] = b"\
823850
0001020304050607080910111213141516171819\
824851
2021222324252627282930313233343536373839\
825852
4041424344454647484950515253545556575859\
826853
6061626364656667686970717273747576777879\
827854
8081828384858687888990919293949596979899";
828855

829-
#[cfg(feature = "std")]
856+
#[cfg(any(feature = "std", not(no_core_net)))]
830857
#[inline]
831858
fn format_u8(mut n: u8, out: &mut [u8]) -> usize {
832859
if n >= 100 {
@@ -847,7 +874,7 @@ fn format_u8(mut n: u8, out: &mut [u8]) -> usize {
847874
}
848875
}
849876

850-
#[cfg(feature = "std")]
877+
#[cfg(any(feature = "std", not(no_core_net)))]
851878
#[test]
852879
fn test_format_u8() {
853880
let mut i = 0u8;
@@ -864,8 +891,8 @@ fn test_format_u8() {
864891
}
865892
}
866893

867-
#[cfg(feature = "std")]
868-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
894+
#[cfg(any(feature = "std", not(no_core_net)))]
895+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
869896
impl Serialize for net::Ipv4Addr {
870897
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
871898
where
@@ -889,8 +916,8 @@ impl Serialize for net::Ipv4Addr {
889916
}
890917
}
891918

892-
#[cfg(feature = "std")]
893-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
919+
#[cfg(any(feature = "std", not(no_core_net)))]
920+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
894921
impl Serialize for net::Ipv6Addr {
895922
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
896923
where
@@ -906,8 +933,8 @@ impl Serialize for net::Ipv6Addr {
906933
}
907934
}
908935

909-
#[cfg(feature = "std")]
910-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
936+
#[cfg(any(feature = "std", not(no_core_net)))]
937+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
911938
impl Serialize for net::SocketAddr {
912939
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
913940
where
@@ -931,8 +958,8 @@ impl Serialize for net::SocketAddr {
931958
}
932959
}
933960

934-
#[cfg(feature = "std")]
935-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
961+
#[cfg(any(feature = "std", not(no_core_net)))]
962+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
936963
impl Serialize for net::SocketAddrV4 {
937964
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
938965
where
@@ -948,8 +975,8 @@ impl Serialize for net::SocketAddrV4 {
948975
}
949976
}
950977

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

0 commit comments

Comments
 (0)