-
Notifications
You must be signed in to change notification settings - Fork 66
add new trait to encode values to bytes #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
4c13edb to
98c1031
Compare
|
Hey @antonilol 👋 Thank you again for your help and PR. I am wondering if we could follow a little more #257 and provide a "writer" (maybe start with a Would it be possible to:
|
This is certainly possible, what I have in mind right now is a method that by default forwards to to_bytes, but can be optionally implemented if desired.
With
I sure can, with the original names (BytesEncode, EItem, bytes_encode)? |
No, with the
Yup, would be perfect. |
Do you maybe mean the |
bee195f to
a5c96f1
Compare
|
In a5c96f1 I used Either (a generic 2 value enum) to return different error types without using dynamic dispatch ( A new Either like 2 value enum instead of Either will work too if the dependency is an issue. |
Thank you for that, but I would prefer we keep these PR changes only for trait improvement. So that it's easier to review as less code changes. I'm also not a huge fan of returning a
I was talking about the |
When accepting
Do you mean dynamically choosing to borrow or return an owned type? This is still possible: to implement the trait the return value ( |
Can you
In issue #257, we are talking about a |
Sure, done
Now I get it, thanks for the patience in explaining it :), added the method to the trait |
4536a2f to
3519c51
Compare
216f028 to
4bb0dfe
Compare
- allow any type that holds bytes instead of just `Cow<'_, [u8]>` - allow any type that holds an error instead of just `BoxedError` - add hint to use writer instead of returning bytes (zero_copy method), retuns BoxedError for now - hint to using writers for serde serialization types
4bb0dfe to
3bdb9ac
Compare
|
Should I add this, and if yes, how should that function be called? Thinking about pub enum EncodingHint {
PreferWriter { size_hint: usize },
PreferBytesEncode,
}Let me know what you think about this! |
allow choosing return type of BytesEncode
Cow<'_, [u8]>BoxedErrorretuns BoxedError for now
Pull Request
Related issues
#165, #257
What does this PR do?
Introduces a new trait that allows more efficient implementations of encoding values to bytes by allowing users to return any
AsRef<[u8]> + Into<Vec<u8>>type, possibly borrowed.TODOs
dyn std::error::Error(Make the error returned by the BytesDecode/BytesEncode trait customizable #166 uses a generic Error type, seems like the best solution)Database::rangewhere byte types are converted toVec<u8>, possibly unnecessary because the type maybe already is owned (does not depend on'a), like[u8; N]?Database::put(see "Example Usage" in Reduce allocations inBytesEncode#257 for an example)