nom::ToUsize is a portability footgun
I just discovered after upgrading a crate to nom 5.0.1 and switching to the function based API it would no longer compile on wasm32-unknown-unknown with the error the trait `nom::ToUsize` is not implemented for `u64` from trying to use nom::bytes::complete::take.
I was able to workaround this by converting from u64 to usize myself and handling any potential error, but because I wasn't testing 32-bit builds in CI I didn't notice this until I went to use the library in a WASM build.
Example program, this will run successfully on a 64-bit machine, but attempting to compile for a 32-bit target will fail:
fn main() {
let bytes = &b"test case"[..];
let result: nom::IResult<&[u8], &[u8]> = nom::bytes::complete::take(5u64)(bytes);
assert_eq!(result.unwrap(), (&bytes[5..], &bytes[..5]));
}
> cargo build --target wasm32-unknown-unknown
[...]
error[E0277]: the trait bound `u64: nom::traits::ToUsize` is not satisfied
--> src/main.rs:3:46
|
3 | let result: nom::IResult<&[u8], &[u8]> = nom::bytes::complete::take(5u64)(bytes);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `nom::traits::ToUsize` is not implemented for `u64`
|
= note: required by `nom::bytes::complete::take`
being careful with portability is exactly the point of ToUsize. take will use Rust's slice indexing, which uses usize, and usize is the target arch pointer size. So it should not compile if we try to use a u64 with take on a 32 bit machine.
So it should not compile if we try to use a u64 with take on a 32 bit machine.
This is what I'm calling a portability issue. My expectation of Rust libraries is that they provide the same API on all targets, other than platform specific libraries, or clearly called out sections like std::{os,arch}::*.
If take took in usize or impl Into<usize> then I would have noticed the potential issue even while developing on a 64bit machine and performed the checked conversion from u64 -> usize straight away, instead of only noticing that once I went to use the library on a 32bit target.
Unfortunately, Into<usize> is not enough because it's not implemented for enough types. And take already makes ToUsize explicit: https://docs.rs/nom/5.0.1/nom/bytes/complete/fn.take.html
If take was using usize directly I'd see a lot of code with nb as usize which is a lot more dangerous because it will cause silent portability issues and even security issues (overflows, etc).
(and I'm saying that because I did that specific mistake too, inside nom even)
I think the issue is that Nemo157 is wanting it to fail to compile every time.. or not. Which would mean just using the std Into/From impls. Because Rust wants to allow pointer sizes as small as possibly u16.
One can argue either way about the ergonomics of not having to do the conversion manually on 64-bit systems, as opposed to it being handled by conditional compilation when appropriate. Here, std chose one tradeoff and nom has chosen another.
I see that, as you say, C is already marked as ToUSize - maybe we could write another line or two talking about the implications without forcing them to follow the where clause link?
An ergonomic API to allow passing in arbitrary integers without platform dependence could be to take TryInto<usize> and produce an error from take if the value is out of range (which is basically what I ended up doing in my code before calling into take). On 64-bit that would have the same result as using IntoUsize, a u64 would be allowed and would always work (and the error cases would be trivially eliminated by the optimizer), while on 32-bit it would have the potential to return a runtime error for corrupt (or too large to parse) data that specifies a length > usize::max_value(), but work perfectly for valid data. The main downside being that it would be a breaking change, and the trait is not available with nom's minimum Rust version policy.