Closed jstarry closed 3 years ago
Thanks for bringing this topic! I have never evaluated this API. I will think through it once I have the capacity, meanwhile, I want to invite @evgenykuzyakov and @matklad to hear their thoughts.
Tough question! API guidelines recommend taking Read/Write by value in API:
However, as borsh impls are typically recursive, I am not entirely sure that it would be the right call in this case. Haven't thought too deeply about this, but it seems that taking an W
in serialize and passing &mut W
to recursive calls will cause excessive monomorphisations:
So, I'd say the current signature is the right one for implementing serialization, no need to change or deprecate it. However, it's a bad API for using serialization, as it requires a trait import. Better to follow serde_json / bincode and introduce convenience top-level functions:
fn bosh::to_vec(valueh: &impl BorshSerialize) -> Vec<u8> {}
fn bosh::to_writer(value: &impl BorshSerialize, w: impl Writer) -> Vec<u8> {}
// Maybe?
fn bosh::to_base58(value: &impl BorshSerialize) -> String {}
Ah, this was more nuanced than I expected. Additional high-level convenience functions would do the trick for us, thanks for looking into this!
@jstarry Would you like to contribute a PR?
@matklad Thanks for the detailed overview :+1:
@frol no problem, just opened a PR
Hello! First of all thanks for this library ❤️
Problem
BorshSerialize::serialize
method signature is not very ergonomic when using mutable slices. This is because a&mut [T]
already implementsWrite
but the method expects a&mut W
whereW
itself implementsWrite
. This means that in order to pass a mutable slice, you need to pass a mutable reference to the mutable slice:&mut &mut [T]
.Proposed Solution
Deprecate
serialize
and create a bincode inspiredserialize_into
method which looks like this: https://docs.rs/bincode/1.3.3/bincode/fn.serialize_into.htmlHowever this is a
0.x
library, so up to you 😉