From 3101467676ab5a303de7f9a52c86bc5185bd9a80 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 11 Dec 2021 00:24:49 -0800 Subject: [PATCH] Replace deprecated mem::uninitialized with MaybeUninit --- .clippy.toml | 2 +- .github/workflows/ci.yml | 2 +- Cargo.toml | 2 +- README.md | 2 +- src/lib.rs | 33 +++++++++++++++++---------------- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/.clippy.toml b/.clippy.toml index 3d30690..0a54853 100644 --- a/.clippy.toml +++ b/.clippy.toml @@ -1 +1 @@ -msrv = "1.31.0" +msrv = "1.36.0" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 09d6846..386b76a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: strategy: fail-fast: false matrix: - rust: [nightly, beta, stable, 1.31.0] + rust: [nightly, beta, stable, 1.36.0] steps: - uses: actions/checkout@v2 - uses: dtolnay/rust-toolchain@master diff --git a/Cargo.toml b/Cargo.toml index e927756..35c352f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "itoa" version = "0.4.8" # remember to update html_root_url authors = ["David Tolnay "] -rust-version = "1.31" +rust-version = "1.36" license = "MIT OR Apache-2.0" description = "Fast functions for printing integer primitives to an io::Write" repository = "https://github.com/dtolnay/itoa" diff --git a/README.md b/README.md index 298983e..6a3044b 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ This crate provides fast functions for printing integer primitives to an See also [`dtoa`] for printing floating point primitives. -*Version requirement: rustc 1.31+* +*Version requirement: rustc 1.36+* [`io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html [`fmt::Write`]: https://doc.rust-lang.org/core/fmt/trait.Write.html diff --git a/src/lib.rs b/src/lib.rs index e1fee40..b68b937 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,6 +75,8 @@ use std::{fmt, io, mem, ptr, slice, str}; #[cfg(not(feature = "std"))] use core::{fmt, mem, ptr, slice, str}; +use self::mem::MaybeUninit; + /// Write integer to an `io::Write`. #[cfg(feature = "std")] #[inline] @@ -105,7 +107,7 @@ pub fn fmt(mut wr: W, value: V) -> fmt::Result { /// ``` #[derive(Copy)] pub struct Buffer { - bytes: [u8; I128_MAX_LEN], + bytes: [MaybeUninit; I128_MAX_LEN], } impl Default for Buffer { @@ -126,11 +128,9 @@ impl Buffer { /// This is a cheap operation; you don't need to worry about reusing buffers /// for efficiency. #[inline] - #[allow(deprecated)] pub fn new() -> Buffer { - Buffer { - bytes: unsafe { mem::uninitialized() }, - } + let bytes = [MaybeUninit::::uninit(); I128_MAX_LEN]; + Buffer { bytes } } /// Print an integer into this buffer and return a reference to its string representation @@ -174,9 +174,10 @@ macro_rules! impl_IntegerCommon { fn write(self, buf: &mut Buffer) -> &str { unsafe { debug_assert!($max_len <= I128_MAX_LEN); - let buf = mem::transmute::<&mut [u8; I128_MAX_LEN], &mut [u8; $max_len]>( - &mut buf.bytes, - ); + let buf = mem::transmute::< + &mut [MaybeUninit; I128_MAX_LEN], + &mut [MaybeUninit; $max_len], + >(&mut buf.bytes); let bytes = self.write_to(buf); str::from_utf8_unchecked(bytes) } @@ -191,10 +192,10 @@ macro_rules! impl_Integer { ($($max_len:expr => $t:ident),* as $conv_fn:ident) => {$( impl_IntegerCommon!($max_len, $t); - impl IntegerPrivate<[u8; $max_len]> for $t { + impl IntegerPrivate<[MaybeUninit; $max_len]> for $t { #[allow(unused_comparisons)] #[inline] - fn write_to(self, buf: &mut [u8; $max_len]) -> &[u8] { + fn write_to(self, buf: &mut [MaybeUninit; $max_len]) -> &[u8] { let is_nonnegative = self >= 0; let mut n = if is_nonnegative { self as $conv_fn @@ -203,7 +204,7 @@ macro_rules! impl_Integer { (!(self as $conv_fn)).wrapping_add(1) }; let mut curr = buf.len() as isize; - let buf_ptr = buf.as_mut_ptr(); + let buf_ptr = buf.as_mut_ptr() as *mut u8; let lut_ptr = DEC_DIGITS_LUT.as_ptr(); unsafe { @@ -290,10 +291,10 @@ macro_rules! impl_Integer128 { ($($max_len:expr => $t:ident),*) => {$( impl_IntegerCommon!($max_len, $t); - impl IntegerPrivate<[u8; $max_len]> for $t { + impl IntegerPrivate<[MaybeUninit; $max_len]> for $t { #[allow(unused_comparisons)] #[inline] - fn write_to(self, buf: &mut [u8; $max_len]) -> &[u8] { + fn write_to(self, buf: &mut [MaybeUninit; $max_len]) -> &[u8] { let is_nonnegative = self >= 0; let n = if is_nonnegative { self as u128 @@ -302,12 +303,12 @@ macro_rules! impl_Integer128 { (!(self as u128)).wrapping_add(1) }; let mut curr = buf.len() as isize; - let buf_ptr = buf.as_mut_ptr(); + let buf_ptr = buf.as_mut_ptr() as *mut u8; unsafe { // Divide by 10^19 which is the highest power less than 2^64. let (n, rem) = udiv128::udivmod_1e19(n); - let buf1 = buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [u8; U64_MAX_LEN]; + let buf1 = buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [MaybeUninit; U64_MAX_LEN]; curr -= rem.write_to(&mut *buf1).len() as isize; if n != 0 { @@ -318,7 +319,7 @@ macro_rules! impl_Integer128 { // 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 [u8; U64_MAX_LEN]; + let buf2 = buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [MaybeUninit; U64_MAX_LEN]; curr -= rem.write_to(&mut *buf2).len() as isize; if n != 0 {