Compare commits

...

10 Commits

Author SHA1 Message Date
Michael Pfaff e6cbf0b921 Merge remote-tracking branch 'origin/master' 2024-05-20 18:59:45 -04:00
Michael Pfaff 3ef6de9039 Expose some internals & make const 2024-05-20 18:58:40 -04:00
David Tolnay 945f297a24
Ignore cast_possible_wrap pedantic clippy lint
warning: casting `usize` to `isize` may wrap around the value
       --> src/lib.rs:144:32
        |
    144 |                   let mut curr = buf.len() as isize;
        |                                  ^^^^^^^^^^^^^^^^^^
    ...
    216 | / impl_Integer!(
    217 | |     I8_MAX_LEN => i8,
    218 | |     U8_MAX_LEN => u8,
    219 | |     I16_MAX_LEN => i16,
    ...   |
    222 | |     U32_MAX_LEN => u32
    223 | |     as u32);
        | |___________- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: `-W clippy::cast-possible-wrap` implied by `-W clippy::pedantic`
        = help: to override `-W clippy::pedantic` add `#[allow(clippy::cast_possible_wrap)]`
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `u32` to `isize` may wrap around the value on targets with 32-bit wide pointers
       --> src/lib.rs:152:35
        |
    152 |                           let rem = (n % 10000) as isize;
        |                                     ^^^^^^^^^^^^^^^^^^^^
    ...
    216 | / impl_Integer!(
    217 | |     I8_MAX_LEN => i8,
    218 | |     U8_MAX_LEN => u8,
    219 | |     I16_MAX_LEN => i16,
    ...   |
    222 | |     U32_MAX_LEN => u32
    223 | |     as u32);
        | |___________- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `u32` to `isize` may wrap around the value on targets with 32-bit wide pointers
       --> src/lib.rs:166:29
        |
    166 |                   let mut n = n as isize; // Possibly reduce 64-bit math.
        |                               ^^^^^^^^^^
    ...
    216 | / impl_Integer!(
    217 | |     I8_MAX_LEN => i8,
    218 | |     U8_MAX_LEN => u8,
    219 | |     I16_MAX_LEN => i16,
    ...   |
    222 | |     U32_MAX_LEN => u32
    223 | |     as u32);
        | |___________- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `usize` to `isize` may wrap around the value
       --> src/lib.rs:144:32
        |
    144 |                   let mut curr = buf.len() as isize;
        |                                  ^^^^^^^^^^^^^^^^^^
    ...
    216 | / impl_Integer!(
    217 | |     I8_MAX_LEN => i8,
    218 | |     U8_MAX_LEN => u8,
    219 | |     I16_MAX_LEN => i16,
    ...   |
    222 | |     U32_MAX_LEN => u32
    223 | |     as u32);
        | |___________- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `usize` to `isize` may wrap around the value
       --> src/lib.rs:144:32
        |
    144 |                 let mut curr = buf.len() as isize;
        |                                ^^^^^^^^^^^^^^^^^^
    ...
    225 | impl_Integer!(I64_MAX_LEN => i64, U64_MAX_LEN => u64 as u64);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `u64` to `isize` may wrap around the value on targets with 64-bit wide pointers
       --> src/lib.rs:152:35
        |
    152 |                         let rem = (n % 10000) as isize;
        |                                   ^^^^^^^^^^^^^^^^^^^^
    ...
    225 | impl_Integer!(I64_MAX_LEN => i64, U64_MAX_LEN => u64 as u64);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `u64` to `isize` may wrap around the value on targets with 64-bit wide pointers
       --> src/lib.rs:166:29
        |
    166 |                 let mut n = n as isize; // Possibly reduce 64-bit math.
        |                             ^^^^^^^^^^
    ...
    225 | impl_Integer!(I64_MAX_LEN => i64, U64_MAX_LEN => u64 as u64);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `usize` to `isize` may wrap around the value
       --> src/lib.rs:144:32
        |
    144 |                 let mut curr = buf.len() as isize;
        |                                ^^^^^^^^^^^^^^^^^^
    ...
    234 | impl_Integer!(I64_MAX_LEN => isize, U64_MAX_LEN => usize as u64);
        | ---------------------------------------------------------------- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `u64` to `isize` may wrap around the value on targets with 64-bit wide pointers
       --> src/lib.rs:152:35
        |
    152 |                         let rem = (n % 10000) as isize;
        |                                   ^^^^^^^^^^^^^^^^^^^^
    ...
    234 | impl_Integer!(I64_MAX_LEN => isize, U64_MAX_LEN => usize as u64);
        | ---------------------------------------------------------------- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `u64` to `isize` may wrap around the value on targets with 64-bit wide pointers
       --> src/lib.rs:166:29
        |
    166 |                 let mut n = n as isize; // Possibly reduce 64-bit math.
        |                             ^^^^^^^^^^
    ...
    234 | impl_Integer!(I64_MAX_LEN => isize, U64_MAX_LEN => usize as u64);
        | ---------------------------------------------------------------- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `usize` to `isize` may wrap around the value
       --> src/lib.rs:254:32
        |
    254 |                 let mut curr = buf.len() as isize;
        |                                ^^^^^^^^^^^^^^^^^^
    ...
    310 | impl_Integer128!(I128_MAX_LEN => i128, U128_MAX_LEN => u128);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer128` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `usize` to `isize` may wrap around the value
       --> src/lib.rs:259:59
        |
    259 |                 let buf1 = unsafe { buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [MaybeUninit<u8>; U64...
        |                                                           ^^^^^^^^^^^^^^^^^^^^
    ...
    310 | impl_Integer128!(I128_MAX_LEN => i128, U128_MAX_LEN => u128);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer128` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `usize` to `isize` may wrap around the value
       --> src/lib.rs:260:25
        |
    260 |                 curr -= rem.write(unsafe { &mut *buf1 }).len() as isize;
        |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...
    310 | impl_Integer128!(I128_MAX_LEN => i128, U128_MAX_LEN => u128);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer128` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `usize` to `isize` may wrap around the value
       --> src/lib.rs:264:34
        |
    264 |                     let target = buf.len() as isize - 19;
        |                                  ^^^^^^^^^^^^^^^^^^
    ...
    310 | impl_Integer128!(I128_MAX_LEN => i128, U128_MAX_LEN => u128);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer128` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `usize` to `isize` may wrap around the value
       --> src/lib.rs:272:63
        |
    272 |                     let buf2 = unsafe { buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [MaybeUninit<u8>;...
        |                                                               ^^^^^^^^^^^^^^^^^^^^
    ...
    310 | impl_Integer128!(I128_MAX_LEN => i128, U128_MAX_LEN => u128);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer128` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `usize` to `isize` may wrap around the value
       --> src/lib.rs:273:29
        |
    273 |                     curr -= rem.write(unsafe { &mut *buf2 }).len() as isize;
        |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...
    310 | impl_Integer128!(I128_MAX_LEN => i128, U128_MAX_LEN => u128);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer128` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `usize` to `isize` may wrap around the value
       --> src/lib.rs:277:38
        |
    277 |                         let target = buf.len() as isize - 38;
        |                                      ^^^^^^^^^^^^^^^^^^
    ...
    310 | impl_Integer128!(I128_MAX_LEN => i128, U128_MAX_LEN => u128);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
        = note: this warning originates in the macro `impl_Integer128` (in Nightly builds, run with -Z macro-backtrace for more info)
2024-04-19 20:42:48 -07:00
David Tolnay 3895493dc3
Ignore cast_sign_loss pedantic clippy lint
warning: casting `i8` to `u32` may lose the sign of the value
       --> src/lib.rs:138:21
        |
    138 |                       self as $conv_fn
        |                       ^^^^^^^^^^^^^^^^
    ...
    215 | / impl_Integer!(
    216 | |     I8_MAX_LEN => i8,
    217 | |     U8_MAX_LEN => u8,
    218 | |     I16_MAX_LEN => i16,
    ...   |
    221 | |     U32_MAX_LEN => u32
    222 | |     as u32);
        | |___________- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: `-W clippy::cast-sign-loss` implied by `-W clippy::pedantic`
        = help: to override `-W clippy::pedantic` add `#[allow(clippy::cast_sign_loss)]`
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `i8` to `u32` may lose the sign of the value
       --> src/lib.rs:141:23
        |
    141 |                       (!(self as $conv_fn)).wrapping_add(1)
        |                         ^^^^^^^^^^^^^^^^^^
    ...
    215 | / impl_Integer!(
    216 | |     I8_MAX_LEN => i8,
    217 | |     U8_MAX_LEN => u8,
    218 | |     I16_MAX_LEN => i16,
    ...   |
    221 | |     U32_MAX_LEN => u32
    222 | |     as u32);
        | |___________- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `isize` to `u8` may lose the sign of the value
       --> src/lib.rs:181:49
        |
    181 |                           *buf_ptr.offset(curr) = (n as u8) + b'0';
        |                                                   ^^^^^^^^^
    ...
    215 | / impl_Integer!(
    216 | |     I8_MAX_LEN => i8,
    217 | |     U8_MAX_LEN => u8,
    218 | |     I16_MAX_LEN => i16,
    ...   |
    221 | |     U32_MAX_LEN => u32
    222 | |     as u32);
        | |___________- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `isize` to `usize` may lose the sign of the value
       --> src/lib.rs:198:39
        |
    198 |                   let len = buf.len() - curr as usize;
        |                                         ^^^^^^^^^^^^^
    ...
    215 | / impl_Integer!(
    216 | |     I8_MAX_LEN => i8,
    217 | |     U8_MAX_LEN => u8,
    218 | |     I16_MAX_LEN => i16,
    ...   |
    221 | |     U32_MAX_LEN => u32
    222 | |     as u32);
        | |___________- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `i16` to `u32` may lose the sign of the value
       --> src/lib.rs:138:21
        |
    138 |                       self as $conv_fn
        |                       ^^^^^^^^^^^^^^^^
    ...
    215 | / impl_Integer!(
    216 | |     I8_MAX_LEN => i8,
    217 | |     U8_MAX_LEN => u8,
    218 | |     I16_MAX_LEN => i16,
    ...   |
    221 | |     U32_MAX_LEN => u32
    222 | |     as u32);
        | |___________- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `i16` to `u32` may lose the sign of the value
       --> src/lib.rs:141:23
        |
    141 |                       (!(self as $conv_fn)).wrapping_add(1)
        |                         ^^^^^^^^^^^^^^^^^^
    ...
    215 | / impl_Integer!(
    216 | |     I8_MAX_LEN => i8,
    217 | |     U8_MAX_LEN => u8,
    218 | |     I16_MAX_LEN => i16,
    ...   |
    221 | |     U32_MAX_LEN => u32
    222 | |     as u32);
        | |___________- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `i32` to `u32` may lose the sign of the value
       --> src/lib.rs:138:21
        |
    138 |                       self as $conv_fn
        |                       ^^^^^^^^^^^^^^^^
    ...
    215 | / impl_Integer!(
    216 | |     I8_MAX_LEN => i8,
    217 | |     U8_MAX_LEN => u8,
    218 | |     I16_MAX_LEN => i16,
    ...   |
    221 | |     U32_MAX_LEN => u32
    222 | |     as u32);
        | |___________- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `i32` to `u32` may lose the sign of the value
       --> src/lib.rs:141:23
        |
    141 |                       (!(self as $conv_fn)).wrapping_add(1)
        |                         ^^^^^^^^^^^^^^^^^^
    ...
    215 | / impl_Integer!(
    216 | |     I8_MAX_LEN => i8,
    217 | |     U8_MAX_LEN => u8,
    218 | |     I16_MAX_LEN => i16,
    ...   |
    221 | |     U32_MAX_LEN => u32
    222 | |     as u32);
        | |___________- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `i64` to `u64` may lose the sign of the value
       --> src/lib.rs:138:21
        |
    138 |                     self as $conv_fn
        |                     ^^^^^^^^^^^^^^^^
    ...
    224 | impl_Integer!(I64_MAX_LEN => i64, U64_MAX_LEN => u64 as u64);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `i64` to `u64` may lose the sign of the value
       --> src/lib.rs:141:23
        |
    141 |                     (!(self as $conv_fn)).wrapping_add(1)
        |                       ^^^^^^^^^^^^^^^^^^
    ...
    224 | impl_Integer!(I64_MAX_LEN => i64, U64_MAX_LEN => u64 as u64);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `isize` to `u8` may lose the sign of the value
       --> src/lib.rs:181:49
        |
    181 |                         *buf_ptr.offset(curr) = (n as u8) + b'0';
        |                                                 ^^^^^^^^^
    ...
    224 | impl_Integer!(I64_MAX_LEN => i64, U64_MAX_LEN => u64 as u64);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `isize` to `usize` may lose the sign of the value
       --> src/lib.rs:198:39
        |
    198 |                 let len = buf.len() - curr as usize;
        |                                       ^^^^^^^^^^^^^
    ...
    224 | impl_Integer!(I64_MAX_LEN => i64, U64_MAX_LEN => u64 as u64);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `isize` to `u64` may lose the sign of the value
       --> src/lib.rs:138:21
        |
    138 |                     self as $conv_fn
        |                     ^^^^^^^^^^^^^^^^
    ...
    233 | impl_Integer!(I64_MAX_LEN => isize, U64_MAX_LEN => usize as u64);
        | ---------------------------------------------------------------- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `isize` to `u64` may lose the sign of the value
       --> src/lib.rs:141:23
        |
    141 |                     (!(self as $conv_fn)).wrapping_add(1)
        |                       ^^^^^^^^^^^^^^^^^^
    ...
    233 | impl_Integer!(I64_MAX_LEN => isize, U64_MAX_LEN => usize as u64);
        | ---------------------------------------------------------------- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `isize` to `u8` may lose the sign of the value
       --> src/lib.rs:181:49
        |
    181 |                         *buf_ptr.offset(curr) = (n as u8) + b'0';
        |                                                 ^^^^^^^^^
    ...
    233 | impl_Integer!(I64_MAX_LEN => isize, U64_MAX_LEN => usize as u64);
        | ---------------------------------------------------------------- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `isize` to `usize` may lose the sign of the value
       --> src/lib.rs:198:39
        |
    198 |                 let len = buf.len() - curr as usize;
        |                                       ^^^^^^^^^^^^^
    ...
    233 | impl_Integer!(I64_MAX_LEN => isize, U64_MAX_LEN => usize as u64);
        | ---------------------------------------------------------------- in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `i128` to `u128` may lose the sign of the value
       --> src/lib.rs:248:21
        |
    248 |                     self as u128
        |                     ^^^^^^^^^^^^
    ...
    309 | impl_Integer128!(I128_MAX_LEN => i128, U128_MAX_LEN => u128);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer128` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `i128` to `u128` may lose the sign of the value
       --> src/lib.rs:251:23
        |
    251 |                     (!(self as u128)).wrapping_add(1)
        |                       ^^^^^^^^^^^^^^
    ...
    309 | impl_Integer128!(I128_MAX_LEN => i128, U128_MAX_LEN => u128);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer128` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `isize` to `usize` may lose the sign of the value
       --> src/lib.rs:265:72
        |
    265 |                         ptr::write_bytes(buf_ptr.offset(target), b'0', (curr - target) as usize);
        |                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^
    ...
    309 | impl_Integer128!(I128_MAX_LEN => i128, U128_MAX_LEN => u128);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer128` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `isize` to `usize` may lose the sign of the value
       --> src/lib.rs:278:76
        |
    278 |                             ptr::write_bytes(buf_ptr.offset(target), b'0', (curr - target) as usize);
        |                                                                            ^^^^^^^^^^^^^^^^^^^^^^^^
    ...
    309 | impl_Integer128!(I128_MAX_LEN => i128, U128_MAX_LEN => u128);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer128` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: casting `isize` to `usize` may lose the sign of the value
       --> src/lib.rs:298:39
        |
    298 |                 let len = buf.len() - curr as usize;
        |                                       ^^^^^^^^^^^^^
    ...
    309 | impl_Integer128!(I128_MAX_LEN => i128, U128_MAX_LEN => u128);
        | ------------------------------------------------------------ in this macro invocation
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_sign_loss
        = note: this warning originates in the macro `impl_Integer128` (in Nightly builds, run with -Z macro-backtrace for more info)
2024-04-19 20:41:43 -07:00
David Tolnay 0ce0ec2440
Revert "Temporarily disable miri on doctests"
This reverts commit d5e45f2273.
2024-04-08 11:52:12 -07:00
David Tolnay 8f392032fa
Release 1.0.11 2024-03-25 23:00:42 -07:00
David Tolnay eeca57ddcf
Touch up comments 2024-03-25 23:00:20 -07:00
David Tolnay ea29bbdbbf
Merge pull request #42 from anforowicz/more-granular-unsafe-blocks
More granular `unsafe` blocks.
2024-03-25 22:57:07 -07:00
David Tolnay 526d4e408c
Explicitly install a Rust toolchain for cargo-outdated job
Debugging a recent cargo-outdated bug, it would have been nice not to
wonder whether a rustc version change in GitHub's runner image was a
contributing factor.
2024-03-25 22:24:51 -07:00
Lukasz Anforowicz fde7fa5e2a More granular `unsafe` blocks.
There should be no functional changes from this commit - the same code
is used before and after this commit, except that `unsafe { ... }`
blocks wrap smaller pieces of code afterwards.  Having more granular
`unsafe` blocks hopefully makes `unsafe` audits a tiny bit easier.
2024-03-25 18:05:44 +00:00
4 changed files with 100 additions and 73 deletions

View File

@ -69,7 +69,7 @@ jobs:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@miri
- run: cargo miri setup
- run: cargo miri test --all-targets # exclude doctests https://github.com/rust-lang/miri/issues/3404
- run: cargo miri test
env:
MIRIFLAGS: -Zmiri-strict-provenance
@ -102,6 +102,7 @@ jobs:
timeout-minutes: 45
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/install@cargo-outdated
- run: cargo outdated --workspace --exit-code 1
- run: cargo outdated --manifest-path fuzz/Cargo.toml --exit-code 1

View File

@ -1,6 +1,6 @@
[package]
name = "itoa"
version = "1.0.10"
version = "1.0.11"
authors = ["David Tolnay <dtolnay@gmail.com>"]
categories = ["value-formatting", "no-std", "no-std::no-alloc"]
description = "Fast integer primitive to string conversion"

View File

@ -30,16 +30,23 @@
//!
//! ![performance](https://raw.githubusercontent.com/dtolnay/itoa/master/performance.png)
#![doc(html_root_url = "https://docs.rs/itoa/1.0.10")]
#![doc(html_root_url = "https://docs.rs/itoa/1.0.11")]
#![no_std]
#![allow(
clippy::cast_lossless,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
clippy::expl_impl_clone_on_copy,
clippy::must_use_candidate,
clippy::needless_doctest_main,
clippy::unreadable_literal
)]
#![feature(const_intrinsic_copy)]
#![feature(const_mut_refs)]
#![feature(const_ptr_write)]
#![feature(const_trait_impl)]
#![feature(effects)]
mod udiv128;
@ -84,7 +91,7 @@ impl Buffer {
/// for efficiency.
#[inline]
#[cfg_attr(feature = "no-panic", no_panic)]
pub fn new() -> Buffer {
pub const fn new() -> Buffer {
let bytes = [MaybeUninit::<u8>::uninit(); I128_MAX_LEN];
Buffer { bytes }
}
@ -92,10 +99,10 @@ impl Buffer {
/// Print an integer into this buffer and return a reference to its string
/// representation within the buffer.
#[cfg_attr(feature = "no-panic", no_panic)]
pub fn format<I: Integer>(&mut self, i: I) -> &str {
pub const fn format<I: ~const Integer>(&mut self, i: I) -> &str {
i.write(unsafe {
&mut *(&mut self.bytes as *mut [MaybeUninit<u8>; I128_MAX_LEN]
as *mut <I as private::Sealed>::Buffer)
as *mut I::Buffer)
})
}
}
@ -103,13 +110,18 @@ impl Buffer {
/// An integer that can be written into an [`itoa::Buffer`][Buffer].
///
/// This trait is sealed and cannot be implemented for types outside of itoa.
pub trait Integer: private::Sealed {}
#[const_trait]
pub trait Integer: private::Sealed {
#[doc(hidden)]
type Buffer: 'static;
#[doc(hidden)]
fn write(self, buf: &mut Self::Buffer) -> &str;
}
// Seal to prevent downstream implementations of the Integer trait.
mod private {
pub trait Sealed: Copy {
type Buffer: 'static;
fn write(self, buf: &mut Self::Buffer) -> &str;
}
}
@ -124,9 +136,7 @@ const DEC_DIGITS_LUT: &[u8] = b"\
// https://github.com/rust-lang/rust/blob/b8214dc6c6fc20d0a660fb5700dca9ebf51ebe89/src/libcore/fmt/num.rs#L188-L266
macro_rules! impl_Integer {
($($max_len:expr => $t:ident),* as $conv_fn:ident) => {$(
impl Integer for $t {}
impl private::Sealed for $t {
impl const Integer for $t {
type Buffer = [MaybeUninit<u8>; $max_len];
#[allow(unused_comparisons)]
@ -137,52 +147,60 @@ macro_rules! impl_Integer {
let mut n = if is_nonnegative {
self as $conv_fn
} else {
// convert the negative num to positive by summing 1 to it's 2 complement
// Convert negative number to positive by summing 1 to its two's complement.
(!(self as $conv_fn)).wrapping_add(1)
};
let mut curr = buf.len() as isize;
let buf_ptr = buf.as_mut_ptr() as *mut u8;
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
unsafe {
// need at least 16 bits for the 4-characters-at-a-time to work.
if mem::size_of::<$t>() >= 2 {
// eagerly decode 4 characters at a time
while n >= 10000 {
let rem = (n % 10000) as isize;
n /= 10000;
// Need at least 16 bits for the 4-digits-at-a-time to work.
if mem::size_of::<$t>() >= 2 {
// Eagerly decode 4 digits at a time.
while n >= 10000 {
let rem = (n % 10000) as isize;
n /= 10000;
let d1 = (rem / 100) << 1;
let d2 = (rem % 100) << 1;
curr -= 4;
let d1 = (rem / 100) << 1;
let d2 = (rem % 100) << 1;
curr -= 4;
unsafe {
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
ptr::copy_nonoverlapping(lut_ptr.offset(d2), buf_ptr.offset(curr + 2), 2);
}
}
}
// if we reach here numbers are <= 9999, so at most 4 chars long
let mut n = n as isize; // possibly reduce 64bit math
// If we reach here, numbers are <=9999 so at most 4 digits long.
let mut n = n as isize; // Possibly reduce 64-bit math.
// decode 2 more chars, if > 2 chars
if n >= 100 {
let d1 = (n % 100) << 1;
n /= 100;
curr -= 2;
// Decode 2 more digits, if >2 digits.
if n >= 100 {
let d1 = (n % 100) << 1;
n /= 100;
curr -= 2;
unsafe {
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
}
}
// decode last 1 or 2 chars
if n < 10 {
curr -= 1;
// Decode last 1 or 2 digits.
if n < 10 {
curr -= 1;
unsafe {
*buf_ptr.offset(curr) = (n as u8) + b'0';
} else {
let d1 = n << 1;
curr -= 2;
}
} else {
let d1 = n << 1;
curr -= 2;
unsafe {
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
}
}
if !is_nonnegative {
curr -= 1;
if !is_nonnegative {
curr -= 1;
unsafe {
*buf_ptr.offset(curr) = b'-';
}
}
@ -192,6 +210,8 @@ macro_rules! impl_Integer {
unsafe { str::from_utf8_unchecked(bytes) }
}
}
impl private::Sealed for $t {}
)*};
}
@ -226,9 +246,7 @@ impl_Integer!(I64_MAX_LEN => isize, U64_MAX_LEN => usize as u64);
macro_rules! impl_Integer128 {
($($max_len:expr => $t:ident),*) => {$(
impl Integer for $t {}
impl private::Sealed for $t {
impl const Integer for $t {
type Buffer = [MaybeUninit<u8>; $max_len];
#[allow(unused_comparisons)]
@ -239,53 +257,61 @@ macro_rules! impl_Integer128 {
let n = if is_nonnegative {
self as u128
} else {
// convert the negative num to positive by summing 1 to it's 2 complement
// Convert negative number to positive by summing 1 to its two's complement.
(!(self as u128)).wrapping_add(1)
};
let mut curr = buf.len() as isize;
let buf_ptr = buf.as_mut_ptr() as *mut u8;
unsafe {
// Divide by 10^19 which is the highest power less than 2^64.
// Divide by 10^19 which is the highest power less than 2^64.
let (n, rem) = udiv128::udivmod_1e19(n);
let buf1 = unsafe { buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [MaybeUninit<u8>; U64_MAX_LEN] };
curr -= rem.write(unsafe { &mut *buf1 }).len() as isize;
if n != 0 {
// Memset the base10 leading zeros of rem.
let target = buf.len() as isize - 19;
unsafe {
ptr::write_bytes(buf_ptr.offset(target), b'0', (curr - target) as usize);
}
curr = target;
// Divide by 10^19 again.
let (n, rem) = udiv128::udivmod_1e19(n);
let buf1 = buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [MaybeUninit<u8>; U64_MAX_LEN];
curr -= rem.write(&mut *buf1).len() as isize;
let buf2 = unsafe { buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [MaybeUninit<u8>; U64_MAX_LEN] };
curr -= rem.write(unsafe { &mut *buf2 }).len() as isize;
if n != 0 {
// Memset the base10 leading zeros of rem.
let target = buf.len() as isize - 19;
ptr::write_bytes(buf_ptr.offset(target), b'0', (curr - target) as usize);
// Memset the leading zeros.
let target = buf.len() as isize - 38;
unsafe {
ptr::write_bytes(buf_ptr.offset(target), b'0', (curr - target) as usize);
}
curr = target;
// Divide by 10^19 again.
let (n, rem) = udiv128::udivmod_1e19(n);
let buf2 = buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [MaybeUninit<u8>; U64_MAX_LEN];
curr -= rem.write(&mut *buf2).len() as isize;
if n != 0 {
// Memset the leading zeros.
let target = buf.len() as isize - 38;
ptr::write_bytes(buf_ptr.offset(target), b'0', (curr - target) as usize);
curr = target;
// There is at most one digit left
// because u128::max / 10^19 / 10^19 is 3.
curr -= 1;
// There is at most one digit left
// because u128::MAX / 10^19 / 10^19 is 3.
curr -= 1;
unsafe {
*buf_ptr.offset(curr) = (n as u8) + b'0';
}
}
}
if !is_nonnegative {
curr -= 1;
if !is_nonnegative {
curr -= 1;
unsafe {
*buf_ptr.offset(curr) = b'-';
}
let len = buf.len() - curr as usize;
let bytes = slice::from_raw_parts(buf_ptr.offset(curr), len);
str::from_utf8_unchecked(bytes)
}
let len = buf.len() - curr as usize;
let bytes = unsafe { slice::from_raw_parts(buf_ptr.offset(curr), len) };
unsafe { str::from_utf8_unchecked(bytes) }
}
}
impl private::Sealed for $t {}
)*};
}

View File

@ -4,7 +4,7 @@ use no_panic::no_panic;
/// Multiply unsigned 128 bit integers, return upper 128 bits of the result
#[inline]
#[cfg_attr(feature = "no-panic", no_panic)]
fn u128_mulhi(x: u128, y: u128) -> u128 {
const fn u128_mulhi(x: u128, y: u128) -> u128 {
let x_lo = x as u64;
let x_hi = (x >> 64) as u64;
let y_lo = y as u64;
@ -31,7 +31,7 @@ fn u128_mulhi(x: u128, y: u128) -> u128 {
///
#[inline]
#[cfg_attr(feature = "no-panic", no_panic)]
pub fn udivmod_1e19(n: u128) -> (u128, u64) {
pub const fn udivmod_1e19(n: u128) -> (u128, u64) {
let d = 10_000_000_000_000_000_000_u64; // 10^19
let quot = if n < 1 << 83 {
@ -41,8 +41,8 @@ pub fn udivmod_1e19(n: u128) -> (u128, u64) {
};
let rem = (n - quot * d as u128) as u64;
debug_assert_eq!(quot, n / d as u128);
debug_assert_eq!(rem as u128, n % d as u128);
debug_assert!(quot == n / d as u128);
debug_assert!(rem as u128 == n % d as u128);
(quot, rem)
}