@@ -769,20 +769,47 @@ impl Serialize for SystemTime {
769
769
770
770
////////////////////////////////////////////////////////////////////////////////
771
771
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
+
772
797
/// Serialize a value that implements `Display` as a string, when that string is
773
798
/// statically known to never have more than a constant `MAX_LEN` bytes.
774
799
///
775
800
/// 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 ) ) ) ]
777
802
macro_rules! serialize_display_bounded_length {
778
803
( $value: expr, $max: expr, $serializer: expr) => { {
779
804
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
784
812
} ;
785
- let written_len = buffer. len( ) - remaining_len;
786
813
let written = & buffer[ ..written_len] ;
787
814
788
815
// write! only provides fmt::Formatter to Display implementations, which
@@ -793,8 +820,8 @@ macro_rules! serialize_display_bounded_length {
793
820
} } ;
794
821
}
795
822
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 ) ) ) ) ) ]
798
825
impl Serialize for net:: IpAddr {
799
826
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
800
827
where
@@ -818,15 +845,15 @@ impl Serialize for net::IpAddr {
818
845
}
819
846
}
820
847
821
- #[ cfg( feature = "std" ) ]
848
+ #[ cfg( any ( feature = "std" , not ( no_core_net ) ) ) ]
822
849
const DEC_DIGITS_LUT : & [ u8 ] = b"\
823
850
0001020304050607080910111213141516171819\
824
851
2021222324252627282930313233343536373839\
825
852
4041424344454647484950515253545556575859\
826
853
6061626364656667686970717273747576777879\
827
854
8081828384858687888990919293949596979899";
828
855
829
- #[ cfg( feature = "std" ) ]
856
+ #[ cfg( any ( feature = "std" , not ( no_core_net ) ) ) ]
830
857
#[ inline]
831
858
fn format_u8 ( mut n : u8 , out : & mut [ u8 ] ) -> usize {
832
859
if n >= 100 {
@@ -847,7 +874,7 @@ fn format_u8(mut n: u8, out: &mut [u8]) -> usize {
847
874
}
848
875
}
849
876
850
- #[ cfg( feature = "std" ) ]
877
+ #[ cfg( any ( feature = "std" , not ( no_core_net ) ) ) ]
851
878
#[ test]
852
879
fn test_format_u8 ( ) {
853
880
let mut i = 0u8 ;
@@ -864,8 +891,8 @@ fn test_format_u8() {
864
891
}
865
892
}
866
893
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 ) ) ) ) ) ]
869
896
impl Serialize for net:: Ipv4Addr {
870
897
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
871
898
where
@@ -889,8 +916,8 @@ impl Serialize for net::Ipv4Addr {
889
916
}
890
917
}
891
918
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 ) ) ) ) ) ]
894
921
impl Serialize for net:: Ipv6Addr {
895
922
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
896
923
where
@@ -906,8 +933,8 @@ impl Serialize for net::Ipv6Addr {
906
933
}
907
934
}
908
935
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 ) ) ) ) ) ]
911
938
impl Serialize for net:: SocketAddr {
912
939
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
913
940
where
@@ -931,8 +958,8 @@ impl Serialize for net::SocketAddr {
931
958
}
932
959
}
933
960
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 ) ) ) ) ) ]
936
963
impl Serialize for net:: SocketAddrV4 {
937
964
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
938
965
where
@@ -948,8 +975,8 @@ impl Serialize for net::SocketAddrV4 {
948
975
}
949
976
}
950
977
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 ) ) ) ) ) ]
953
980
impl Serialize for net:: SocketAddrV6 {
954
981
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
955
982
where
0 commit comments