paritytech / parity-scale-codec

Lightweight, efficient, binary serialization and deserialization codec
Apache License 2.0
257 stars 94 forks source link

Introduce `CountedInput`. a wrapper which counts the number of bytes successfully read #630

Closed gui1117 closed 2 months ago

gui1117 commented 2 months ago

I saw some code which tries to track the number of byte read with remaining_len. But remaining_len don't always give information.

I think it is better to provide an accurate counter.

I used u64 because it made the code simpler. And more general purpose.

Otherwise I was thinking using a type:

enum Count {
    Exact(u32),
    MaxReached,
}

and do the count in u32.

But most machine being 64bit, I think it should execute good.

shawntabrizi commented 2 months ago

fyi we also have saturated_inc() which mutates in place and adds one.

bkchr commented 2 months ago

fyi we also have saturated_inc() which mutates in place and adds one.

No in the standard library (which is only used in this crate)