@@ -27,12 +27,33 @@ pub trait BytesEncode<'a> {
27
27
/// The error type to return when decoding goes wrong.
28
28
type Error : StdError + Send + Sync + ' static ;
29
29
30
+ /// This function can be used to hint callers of the
31
+ /// [`bytes_encode`][BytesEncode::bytes_encode] function to use
32
+ /// [`bytes_encode_into_writer`][BytesEncode::bytes_encode_into_writer] instead, if the latter
33
+ /// runs faster (for example if it needs less heap allocations).
34
+ ///
35
+ /// The default implementation returns `true` because the default implementation of
36
+ /// [`bytes_encode_into_writer`][BytesEncode::bytes_encode_into_writer] is to forward to
37
+ /// [`bytes_encode`][BytesEncode::bytes_encode].
38
+ fn zero_copy ( item : & Self :: EItem ) -> bool {
39
+ // This is preferred to renaming the function parameter (to _item) because IDEs can
40
+ // autofill trait implementations, which will default the paramter name to _item then and
41
+ // this could probably also mess with clippy's renamed_function_params lint.
42
+ let _ = item;
43
+
44
+ true
45
+ }
46
+
30
47
/// Encode the given item as bytes.
31
48
fn bytes_encode ( item : & ' a Self :: EItem ) -> Result < Self :: ReturnBytes , Self :: Error > ;
32
49
33
50
/// Encode the given item as bytes and write it into the writer. Returns the amount of bytes
34
- /// that were written. This function by default forwards to
35
- /// [`bytes_encode`][BytesEncode::bytes_encode].
51
+ /// that were written.
52
+ ///
53
+ /// When implementing this, also take a look at [`zero_copy`][BytesEncode::zero_copy]'s
54
+ /// documentation.
55
+ ///
56
+ /// The default implementation forwards to [`bytes_encode`][BytesEncode::bytes_encode].
36
57
fn bytes_encode_into_writer < W : io:: Write > (
37
58
item : & ' a Self :: EItem ,
38
59
writer : & mut W ,
0 commit comments