-
Notifications
You must be signed in to change notification settings - Fork 20
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
No alloc #34
No alloc #34
Conversation
CI for no_std needs to be updated as well, as no default features implies no_alloc And I meant to open this as a draft, whoops |
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.
Thanks. It looks good so far!
@vmx Errors are handled by a custom result type, which doesn't play well with no_alloc as it implies from std::Error which currently requires an allocator we could try moving to error enums but that would make it more brittle. Other crates don't offer great options either for this sort of thing and the community discussion around it is decently heated and currently inconclusive. pyfisch/cbor#79 |
After looking into implementing decode_mut etc for the |
@mriise I don't fully understand the problem with the errors. The current code has an |
@vmx nothing actually, I was the one who was missing things. I just need to add the extra error types from data_encoding to the enum. |
In retrospect I should've ran clippy myself instead of using build server time... |
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.
New changes came in while I was reviewing, so some things might not apply anymore.
src/impls.rs
Outdated
|
||
fn encode_mut<I: AsRef<[u8]>>(input: I, output: &mut [u8]){ | ||
let out = base_x::encode($encoding.0, input.as_ref()); | ||
output.copy_from_slice(out.as_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.
This could panic in case the sliced differ in size. This either needs to be documented or we check for the size. I lean towards checking for the size and returning an error if it doesn't match. What do you think?
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.
New error type MismatchedSizes
or just the plain old DecodeError
?
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.
MismatchSizes
sounds good as a user of the library might want to deal with it in a special way, e.g. increasing the size of the slice and trying it again.
Not much was changed, just formatting and removing the unneeded log10 method |
Here's a personal list not peticularly including things from the review,
Looks like one or two more rounds of review and this should be ready to go! |
@vmx Im thinking that just re-exporting the data-encoding errors and using them would be the better option here, it's a lot better to conform less informative errors into more complex errors than it is to transform complex and informative errors into less informative ones. I may try to copy the error format from data-encoding to base-x as it could definitely use it. |
I agree with that. I just don't want to leak internal dependencies to the public API. But if we wrap (i guess re-exporting is fine as well) those errors that's fine. I just don't want to force us to break our API in case we decide to use other libraries for base encoding in the future. |
/// math comes from here https://github.com/bitcoin/bitcoin/blob/f1e2f2a85962c1664e4e55471061af0eaa798d40/src/base58.cpp#L48 | ||
pub(crate) fn calc_decoded_size(base: usize, input_byte_size: usize) -> usize { | ||
f64::ceil(input_byte_size as f64 * (f64::log10(base as f64) / f64::log10(256.0)) + 1.0) as usize | ||
// this shouldn't need an extra one or ceiling, something is wrong somewhere |
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.
Agreed. This should ideally be:
(input_byte_size as f64 * (f64::log2(base as f64) / 8.0))) as usize
// or
(input_byte_size as f64 / (8.0 / f64::log2(base as f64))) as usize
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 definitely agree, but for whatever reason it fails all test cases without the round up and added 1...
Just rounding up gives one short for base10 in the preserves_two_leading_zeroes
test
decode len 11>11 base Base10
12
[0, 0, 121, 101, 115, 32, 109, 97, 110, 105, 32, 33]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
thread 'preserves_two_leading_zeroes' panicked at 'range end index 12 out of range for slice of length 11'
I will double check to make sure I am doing the tests correctly before I think about possible issues with base-x
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.
It's good that we have that test :) Your code is using .len_utf8()
, that should just be .len()
as for base encoding it's really about the bits and bytes and not about utf8 characters.
Closing as this doesnt have any changes that are relevant anymore (new PR should reference this) |
Low effort no_alloc, still missing tests and public methods for the crate like
multibase::decode_mut()
.Also has a few minor changes to README.
fixes #34