99
1010#![ warn( missing_docs) ]
1111
12- use std:: borrow:: Cow ;
1312use std:: cmp:: { Ord , Ordering } ;
1413use std:: error:: Error as StdError ;
15- use std:: fmt;
1614
1715/// A boxed `Send + Sync + 'static` error.
1816pub type BoxedError = Box < dyn StdError + Send + Sync + ' static > ;
1917
20- /// A trait that represents an encoding structure.
21- #[ deprecated = "replaced by `ToBytes` to allow for more optimization" ]
22- #[ allow( deprecated) ] // deprecated BoxedErrorWrapper is used in a bound
23- pub trait BytesEncode < ' a > :
24- // TODO are these bound needed?
25- ToBytes < ' a , SelfType = Self :: EItem , ReturnBytes = Cow < ' a , [ u8 ] > , Error = BoxedErrorWrapper >
26- {
27- /// The type to encode.
28- type EItem : ?Sized + ' a ;
29-
30- /// Encode the given item as bytes.
31- fn bytes_encode ( item : & ' a Self :: EItem ) -> Result < Cow < ' a , [ u8 ] > , BoxedError > ;
32- }
33-
3418/// A trait that represents an encoding structure.
3519pub trait ToBytes < ' a > {
3620 /// The type to encode to bytes.
@@ -44,45 +28,15 @@ pub trait ToBytes<'a> {
4428
4529 /// Encode the given item as bytes.
4630 fn to_bytes ( item : & ' a Self :: SelfType ) -> Result < Self :: ReturnBytes , Self :: Error > ;
47- }
48-
49- #[ allow( deprecated) ]
50- impl < ' a , T : BytesEncode < ' a > > ToBytes < ' a > for T {
51- type SelfType = <Self as BytesEncode < ' a > >:: EItem ;
52-
53- type ReturnBytes = Cow < ' a , [ u8 ] > ;
54-
55- type Error = BoxedErrorWrapper ;
56-
57- fn to_bytes ( item : & ' a Self :: SelfType ) -> Result < Self :: ReturnBytes , Self :: Error > {
58- Self :: bytes_encode ( item) . map_err ( BoxedErrorWrapper )
59- }
60- }
61-
62- /// Wraps the [`BoxedError`] type alias because for complicated reasons it does not implement
63- /// [`Error`][StdError]. This wrapper forwards [`Debug`][fmt::Debug], [`Display`][fmt::Display]
64- /// and [`Error`][StdError] through the wrapper and the [`Box`].
65- #[ deprecated = "this wrapper was added for backwards compatibility of BytesEncode only" ]
66- pub struct BoxedErrorWrapper ( BoxedError ) ;
67-
68- #[ allow( deprecated) ]
69- impl fmt:: Debug for BoxedErrorWrapper {
70- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
71- <BoxedError as fmt:: Debug >:: fmt ( & self . 0 , f)
72- }
73- }
74-
75- #[ allow( deprecated) ]
76- impl fmt:: Display for BoxedErrorWrapper {
77- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
78- <BoxedError as fmt:: Display >:: fmt ( & self . 0 , f)
79- }
80- }
8131
82- #[ allow( deprecated) ]
83- impl StdError for BoxedErrorWrapper {
84- fn source ( & self ) -> Option < & ( dyn StdError + ' static ) > {
85- self . 0 . source ( )
32+ /// Encode the given item as bytes and write it into the writer. This function by default
33+ /// forwards to `to_bytes`.
34+ fn bytes_encode_into_writer (
35+ item : & ' a Self :: SelfType ,
36+ writer : & mut Vec < u8 > ,
37+ ) -> Result < ( ) , Self :: Error > {
38+ writer. extend_from_slice ( Self :: to_bytes ( item) ?. as_ref ( ) ) ;
39+ Ok ( ( ) )
8640 }
8741}
8842
0 commit comments