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