@@ -873,54 +873,63 @@ pub mod string {
873
873
}
874
874
}
875
875
876
- pub trait BytesAdapter : sealed:: BytesAdapter + sealed:: Newable { }
876
+ pub trait BytesAdapter : Sized + ' static {
877
+ /// Create a new instance (required as `Default` is not available for all types)
878
+ fn new ( ) -> Self ;
877
879
878
- mod sealed {
879
- use super :: { Buf , BufMut } ;
880
-
881
- pub trait BytesAdapter : Sized + ' static {
882
- fn len ( & self ) -> usize ;
880
+ fn len ( & self ) -> usize ;
883
881
884
- /// Replace contents of this buffer with the contents of another buffer.
885
- fn replace_with < B > ( & mut self , buf : B )
886
- where
887
- B : Buf ;
882
+ /// Replace contents of this buffer with the contents of another buffer.
883
+ fn replace_with < B > ( & mut self , buf : B ) -> Result < ( ) , DecodeError >
884
+ where
885
+ B : Buf ;
888
886
889
- /// Appends this buffer to the (contents of) other buffer.
890
- fn append_to < B > ( & self , buf : & mut B )
891
- where
892
- B : BufMut ;
887
+ /// Appends this buffer to the (contents of) other buffer.
888
+ fn append_to < B > ( & self , buf : & mut B )
889
+ where
890
+ B : BufMut ;
893
891
894
- fn is_empty ( & self ) -> bool {
895
- self . len ( ) == 0
896
- }
892
+ fn is_empty ( & self ) -> bool {
893
+ self . len ( ) == 0
897
894
}
898
895
899
- /// Alternate to `Default` as this is not (yet?) implemented over [T; N]
900
- /// (and cannot be automatic as `Default` _is_ implemented for N < 32)
896
+ fn clear ( & mut self ) ;
897
+ }
898
+
899
+ mod sealed {
901
900
pub trait Newable : Sized {
902
901
fn new ( ) -> Self ;
903
902
}
904
- }
905
903
906
- impl BytesAdapter for Bytes { }
904
+ impl < T : super :: BytesAdapter > Newable for T {
905
+ fn new ( ) -> Self {
906
+ super :: BytesAdapter :: new ( )
907
+ }
908
+ }
909
+
910
+ impl Newable for String {
911
+ fn new ( ) -> Self {
912
+ Default :: default ( )
913
+ }
914
+ }
915
+ }
907
916
908
- impl sealed :: Newable for Bytes {
917
+ impl BytesAdapter for Bytes {
909
918
fn new ( ) -> Self {
910
919
Default :: default ( )
911
920
}
912
- }
913
921
914
- impl sealed:: BytesAdapter for Bytes {
915
922
fn len ( & self ) -> usize {
916
923
Buf :: remaining ( self )
917
924
}
918
925
919
- fn replace_with < B > ( & mut self , mut buf : B )
926
+ fn replace_with < B > ( & mut self , mut buf : B ) -> Result < ( ) , DecodeError >
920
927
where
921
928
B : Buf ,
922
929
{
923
930
* self = buf. copy_to_bytes ( buf. remaining ( ) ) ;
931
+
932
+ Ok ( ( ) )
924
933
}
925
934
926
935
fn append_to < B > ( & self , buf : & mut B )
@@ -929,28 +938,30 @@ impl sealed::BytesAdapter for Bytes {
929
938
{
930
939
buf. put ( self . clone ( ) )
931
940
}
932
- }
933
941
934
- impl BytesAdapter for Vec < u8 > { }
942
+ fn clear ( & mut self ) {
943
+ Bytes :: clear ( self )
944
+ }
945
+ }
935
946
936
- impl sealed :: Newable for Vec < u8 > {
947
+ impl BytesAdapter for Vec < u8 > {
937
948
fn new ( ) -> Self {
938
949
Default :: default ( )
939
950
}
940
- }
941
951
942
- impl sealed:: BytesAdapter for Vec < u8 > {
943
952
fn len ( & self ) -> usize {
944
953
Vec :: len ( self )
945
954
}
946
955
947
- fn replace_with < B > ( & mut self , buf : B )
956
+ fn replace_with < B > ( & mut self , buf : B ) -> Result < ( ) , DecodeError >
948
957
where
949
958
B : Buf ,
950
959
{
951
- self . clear ( ) ;
960
+ Vec :: clear ( self ) ;
952
961
self . reserve ( buf. remaining ( ) ) ;
953
962
self . put ( buf) ;
963
+
964
+ Ok ( ( ) )
954
965
}
955
966
956
967
fn append_to < B > ( & self , buf : & mut B )
@@ -959,26 +970,32 @@ impl sealed::BytesAdapter for Vec<u8> {
959
970
{
960
971
buf. put ( self . as_slice ( ) )
961
972
}
962
- }
963
973
964
- impl < const N : usize > BytesAdapter for [ u8 ; N ] { }
974
+ fn clear ( & mut self ) {
975
+ Vec :: clear ( self )
976
+ }
977
+ }
965
978
966
- impl < const N : usize > sealed :: Newable for [ u8 ; N ] {
979
+ impl < const N : usize > BytesAdapter for [ u8 ; N ] {
967
980
fn new ( ) -> Self {
968
981
[ 0u8 ; N ]
969
982
}
970
- }
971
983
972
- impl < const N : usize > sealed:: BytesAdapter for [ u8 ; N ] {
973
984
fn len ( & self ) -> usize {
974
985
N
975
986
}
976
987
977
- fn replace_with < B > ( & mut self , buf : B )
988
+ fn replace_with < B > ( & mut self , buf : B ) -> Result < ( ) , DecodeError >
978
989
where
979
990
B : Buf ,
980
991
{
981
- self . copy_from_slice ( buf. chunk ( ) )
992
+ if buf. remaining ( ) != N {
993
+ return Err ( DecodeError :: new ( "invalid byte array length" ) ) ;
994
+ }
995
+
996
+ self . copy_from_slice ( buf. chunk ( ) ) ;
997
+
998
+ Ok ( ( ) )
982
999
}
983
1000
984
1001
fn append_to < B > ( & self , buf : & mut B )
@@ -987,11 +1004,11 @@ impl<const N: usize> sealed::BytesAdapter for [u8; N] {
987
1004
{
988
1005
buf. put ( & self [ ..] )
989
1006
}
990
- }
991
1007
992
- impl sealed:: Newable for String {
993
- fn new ( ) -> Self {
994
- Default :: default ( )
1008
+ fn clear ( & mut self ) {
1009
+ for b in & mut self [ ..] {
1010
+ * b = 0 ;
1011
+ }
995
1012
}
996
1013
}
997
1014
@@ -1037,7 +1054,8 @@ pub mod bytes {
1037
1054
// This is intended for A and B both being Bytes so it is zero-copy.
1038
1055
// Some combinations of A and B types may cause a double-copy,
1039
1056
// in which case merge_one_copy() should be used instead.
1040
- value. replace_with ( buf. copy_to_bytes ( len) ) ;
1057
+ value. replace_with ( buf. copy_to_bytes ( len) ) ?;
1058
+
1041
1059
Ok ( ( ) )
1042
1060
}
1043
1061
@@ -1059,7 +1077,8 @@ pub mod bytes {
1059
1077
let len = len as usize ;
1060
1078
1061
1079
// If we must copy, make sure to copy only once.
1062
- value. replace_with ( buf. take ( len) ) ;
1080
+ value. replace_with ( buf. take ( len) ) ?;
1081
+
1063
1082
Ok ( ( ) )
1064
1083
}
1065
1084
0 commit comments