Skip to content

Commit b4f860e

Browse files
authored
Merge pull request #2816 from MathiasKoch/chore/core-net
Implement serialize/deserialize for core::net instead of std::net
2 parents 3aca38d + d940fe1 commit b4f860e

File tree

6 files changed

+53
-50
lines changed

6 files changed

+53
-50
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/de/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ use crate::lib::*;
118118

119119
pub mod value;
120120

121-
mod format;
122121
mod ignored_any;
123122
mod impls;
124123
pub(crate) mod size_hint;
@@ -1374,7 +1373,7 @@ pub trait Visitor<'de>: Sized {
13741373
E: Error,
13751374
{
13761375
let mut buf = [0u8; 58];
1377-
let mut writer = format::Buf::new(&mut buf);
1376+
let mut writer = crate::format::Buf::new(&mut buf);
13781377
fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as i128", v)).unwrap();
13791378
Err(Error::invalid_type(
13801379
Unexpected::Other(writer.as_str()),
@@ -1436,7 +1435,7 @@ pub trait Visitor<'de>: Sized {
14361435
E: Error,
14371436
{
14381437
let mut buf = [0u8; 57];
1439-
let mut writer = format::Buf::new(&mut buf);
1438+
let mut writer = crate::format::Buf::new(&mut buf);
14401439
fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as u128", v)).unwrap();
14411440
Err(Error::invalid_type(
14421441
Unexpected::Other(writer.as_str()),
File renamed without changes.

serde/src/lib.rs

+8-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};
@@ -305,6 +310,8 @@ mod integer128;
305310
pub mod de;
306311
pub mod ser;
307312

313+
mod format;
314+
308315
#[doc(inline)]
309316
pub use crate::de::{Deserialize, Deserializer};
310317
#[doc(inline)]

serde/src/ser/impls.rs

+19-29
Original file line numberDiff line numberDiff line change
@@ -773,28 +773,18 @@ impl Serialize for SystemTime {
773773
/// statically known to never have more than a constant `MAX_LEN` bytes.
774774
///
775775
/// Panics if the `Display` impl tries to write more than `MAX_LEN` bytes.
776-
#[cfg(feature = "std")]
776+
#[cfg(any(feature = "std", not(no_core_net)))]
777777
macro_rules! serialize_display_bounded_length {
778778
($value:expr, $max:expr, $serializer:expr) => {{
779779
let mut buffer = [0u8; $max];
780-
let remaining_len = {
781-
let mut remaining = &mut buffer[..];
782-
write!(remaining, "{}", $value).unwrap();
783-
remaining.len()
784-
};
785-
let written_len = buffer.len() - remaining_len;
786-
let written = &buffer[..written_len];
787-
788-
// write! only provides fmt::Formatter to Display implementations, which
789-
// has methods write_str and write_char but no method to write arbitrary
790-
// bytes. Therefore `written` must be valid UTF-8.
791-
let written_str = str::from_utf8(written).expect("must be valid UTF-8");
792-
$serializer.serialize_str(written_str)
780+
let mut writer = crate::format::Buf::new(&mut buffer);
781+
write!(&mut writer, "{}", $value).unwrap();
782+
$serializer.serialize_str(writer.as_str())
793783
}};
794784
}
795785

796-
#[cfg(feature = "std")]
797-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
786+
#[cfg(any(feature = "std", not(no_core_net)))]
787+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
798788
impl Serialize for net::IpAddr {
799789
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
800790
where
@@ -818,15 +808,15 @@ impl Serialize for net::IpAddr {
818808
}
819809
}
820810

821-
#[cfg(feature = "std")]
811+
#[cfg(any(feature = "std", not(no_core_net)))]
822812
const DEC_DIGITS_LUT: &[u8] = b"\
823813
0001020304050607080910111213141516171819\
824814
2021222324252627282930313233343536373839\
825815
4041424344454647484950515253545556575859\
826816
6061626364656667686970717273747576777879\
827817
8081828384858687888990919293949596979899";
828818

829-
#[cfg(feature = "std")]
819+
#[cfg(any(feature = "std", not(no_core_net)))]
830820
#[inline]
831821
fn format_u8(mut n: u8, out: &mut [u8]) -> usize {
832822
if n >= 100 {
@@ -847,7 +837,7 @@ fn format_u8(mut n: u8, out: &mut [u8]) -> usize {
847837
}
848838
}
849839

850-
#[cfg(feature = "std")]
840+
#[cfg(any(feature = "std", not(no_core_net)))]
851841
#[test]
852842
fn test_format_u8() {
853843
let mut i = 0u8;
@@ -864,8 +854,8 @@ fn test_format_u8() {
864854
}
865855
}
866856

867-
#[cfg(feature = "std")]
868-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
857+
#[cfg(any(feature = "std", not(no_core_net)))]
858+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
869859
impl Serialize for net::Ipv4Addr {
870860
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
871861
where
@@ -889,8 +879,8 @@ impl Serialize for net::Ipv4Addr {
889879
}
890880
}
891881

892-
#[cfg(feature = "std")]
893-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
882+
#[cfg(any(feature = "std", not(no_core_net)))]
883+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
894884
impl Serialize for net::Ipv6Addr {
895885
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
896886
where
@@ -906,8 +896,8 @@ impl Serialize for net::Ipv6Addr {
906896
}
907897
}
908898

909-
#[cfg(feature = "std")]
910-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
899+
#[cfg(any(feature = "std", not(no_core_net)))]
900+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
911901
impl Serialize for net::SocketAddr {
912902
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
913903
where
@@ -931,8 +921,8 @@ impl Serialize for net::SocketAddr {
931921
}
932922
}
933923

934-
#[cfg(feature = "std")]
935-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
924+
#[cfg(any(feature = "std", not(no_core_net)))]
925+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", not(no_core_net)))))]
936926
impl Serialize for net::SocketAddrV4 {
937927
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
938928
where
@@ -948,8 +938,8 @@ impl Serialize for net::SocketAddrV4 {
948938
}
949939
}
950940

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

0 commit comments

Comments
 (0)