Skip to content

Commit 1c0fae6

Browse files
committed
add zero_copy method
1 parent f9a7684 commit 1c0fae6

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

Diff for: heed-traits/src/lib.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,33 @@ pub trait BytesEncode<'a> {
2727
/// The error type to return when decoding goes wrong.
2828
type Error: StdError + Send + Sync + 'static;
2929

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+
3047
/// Encode the given item as bytes.
3148
fn bytes_encode(item: &'a Self::EItem) -> Result<Self::ReturnBytes, Self::Error>;
3249

3350
/// 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].
3657
fn bytes_encode_into_writer<W: io::Write>(
3758
item: &'a Self::EItem,
3859
writer: &mut W,

0 commit comments

Comments
 (0)