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
Redesign hex::BufEncoder
to accept owned arrays
#1273
Conversation
Note I found that |
Not being able to create an owned `BufEncoder` prevented returning it from functions which need to allocate the buffer on stack. Such is the case in WIP serde via consensus serialzation. This change refactors `OutBytes` to be unsized, adds an `AsOutBytes` trait and uses that one instead of `Into` to perform the conversion.
9a29b5b
to
1bf8855
Compare
This is really elegant! Out of curiosity where in std did you copy this from? concept ACK, even with the unsafety. Regarding xargo/miri, yes it's worth it!! We should MIRI-check a ton of stuff (annoyingly last I looked, anything that called into libsecp we couldn't do with miri which eliminates a lot of crypto..). But we should do this in a separate PR (even though initially they'll be "dumb" tests that don't even involve unsafety so theoretically miri should never get mad about). |
Same as before:
I'll try to add it. |
fn as_out_bytes(&self) -> &OutBytes; | ||
|
||
/// Performs the conversion. | ||
fn as_mut_out_bytes(&mut self) -> &mut OutBytes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note one would normally have two traits - one for mut
the other for non-mut
. The limitation is that immutable version of AsOutBytes
can not be implemented for &T where T: AsOutBytes
.
I think this is fine, we really don't want to deal with immutable slices beyond providing non-weird API of BufEncoder
I propose to add |
Sounds good. re-concept ACK -- I had an idea that I could evade the unsafety by clever use of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 9a29b5b
We can't use Indeed the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Except for the question about the out_bytes
module, ACK 1bf8855
/// This prevents the rest of the crate from accessing the field of `OutBytes`. | ||
mod out_bytes { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this comment (and module use), the OutBytes
inner array is private so cannot be accessed by other modules anyways? Or am I missing something? I would have expected to see OutBytes
as part of buf_encoder
module and private::Sealed
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it was in buf_encoder
it'd make the field accessible to the rest of the module which we don't want because it raises auditing cost. This is not critical now but if we ever switch to MaybeUninit
we won't forget about it and will have a much smaller diff. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, can we change it to "This prevents the rest of the module from accessing the field of OutBytes
" please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit only, of course :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and the crate :)
I'd prefer not to mess with a PR that has two ACKs for something this trivial.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 1bf8855
Not being able to create an owned
BufEncoder
prevented returning it from functions which need to allocate the buffer on stack. Such is the case in WIP serde via consensus serialzation.This change refactors
OutBytes
to be unsized, adds anAsOutBytes
trait and uses that one instead ofInto
to perform the conversion.Closes #1270
This is meant as potentially mergeable demonstration. Interestingly, it was easier than I expected.