winnow-rs / winnow

Making parsing a breeze
https://docs.rs/winnow
Other
525 stars 40 forks source link

impl Uint for usize #431

Closed jwodder closed 7 months ago

jwodder commented 8 months ago

Please complete the following tasks

winnow version

0.5.34

Describe your use case

I would like to use winnow to parse some unsigned decimal integers into usize, but winnow::ascii::dec_uint() does not support that, as usize does not implement winnow::ascii::Uint.

I do not know whether this was simply an oversight or whether usize was deliberately excluded from the trait, but as I can find no references to parsing usize in the issues or discussions, in the worst case scenario (for me), this issue can be used to document why it's not implemented.

(I am aware I can do digit1.parse_to(), but if I'm supposed to use that, then why have dec_uint() at all? I am also aware I can use dec_uint() to parse a fixed-size int and then try-convert that to usize, but that's less convenient than if dec_uint() supported usize in the first place.)

Describe the solution you'd like

impl Uint for usize

Alternatives, if applicable

No response

Additional Context

No response

epage commented 8 months ago

For context, I started off avoiding usize / isize because of their platform-specific nature, instead wanting people to pick a cross-platform data type to parse and then convert to what type is needed within their application. This is more important in winnow::binary but also carried over into winnow::ascii.

jwodder commented 8 months ago

So are you currently willing to add usize support to winnow::ascii? In the code I'm writing, the integers I'm parsing are solely going to be used for things that Rust requires usize for, such as Vec lengths & indices and take() arguments. I'd like to be able to parse exactly those integers that fit into a usize without having to convert through a fixed-size integer type, which wouldn't cover the full range on some platforms and/or (particularly if I went with u128) would be wasteful on most platforms.