Nuke slice_as{,_mut}_ptr methods · rust-lang/rust@f2e9b40 (original) (raw)

`@@ -106,10 +106,7 @@ unsafe trait GenericRadix: Sized {

`

106

106

`` // SAFETY: The only chars in buf are created by Self::digit which are assumed to be

``

107

107

`// valid UTF-8

`

108

108

`let buf = unsafe {

`

109

``

`-

str::from_utf8_unchecked(slice::from_raw_parts(

`

110

``

`-

MaybeUninit::slice_as_ptr(buf),

`

111

``

`-

buf.len(),

`

112

``

`-

))

`

``

109

`+

str::from_utf8_unchecked(slice::from_raw_parts(buf.as_ptr().into_inner(), buf.len()))

`

113

110

`};

`

114

111

` f.pad_integral(is_nonnegative, Self::PREFIX, buf)

`

115

112

`}

`

`@@ -216,7 +213,7 @@ macro_rules! impl_Display {

`

216

213

`// 2^128 is about 3*10^38, so 39 gives an extra byte of space

`

217

214

`let mut buf = [MaybeUninit::::uninit(); 39];

`

218

215

`let mut curr = buf.len();

`

219

``

`-

let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);

`

``

216

`+

let buf_ptr = buf.as_mut_ptr().into_inner();

`

220

217

`let lut_ptr = DEC_DIGITS_LUT.as_ptr();

`

221

218

``

222

219

`` // SAFETY: Since d1 and d2 are always less than or equal to 198, we

``

`@@ -344,7 +341,7 @@ macro_rules! impl_Exp {

`

344

341

`` // that curr >= 0.

``

345

342

`let mut buf = [MaybeUninit::::uninit(); 40];

`

346

343

`let mut curr = buf.len(); //index for buf

`

347

``

`-

let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);

`

``

344

`+

let buf_ptr = buf.as_mut_ptr().into_inner();

`

348

345

`let lut_ptr = DEC_DIGITS_LUT.as_ptr();

`

349

346

``

350

347

`// decode 2 chars at a time

`

`@@ -392,20 +389,19 @@ macro_rules! impl_Exp {

`

392

389

``

393

390

`// stores 'e' (or 'E') and the up to 2-digit exponent

`

394

391

`let mut exp_buf = [MaybeUninit::::uninit(); 3];

`

395

``

`-

let exp_ptr = MaybeUninit::slice_as_mut_ptr(&mut exp_buf);

`

396

``

`` -

// SAFETY: In either case, exp_buf is written within bounds and exp_ptr[..len]

``

397

``

`` -

// is contained within exp_buf since len <= 3.

``

398

``

`-

let exp_slice = unsafe {

`

399

``

`-

*exp_ptr.add(0) = if upper { b'E' } else { b'e' };

`

``

392

`+

exp_buf[0].write(if upper { b'E' } else { b'e' });

`

``

393

`+

let exp_slice = {

`

400

394

`let len = if exponent < 10 {

`

401

``

`-

*exp_ptr.add(1) = (exponent as u8) + b'0';

`

``

395

`+

exp_buf[1].write((exponent as u8) + b'0');

`

402

396

`2

`

403

397

`} else {

`

404

398

`let off = exponent << 1;

`

405

``

`-

ptr::copy_nonoverlapping(lut_ptr.add(off), exp_ptr.add(1), 2);

`

``

399

`+

// SAFETY: 1 + 2 <= 3

`

``

400

`+

unsafe { ptr::copy_nonoverlapping(lut_ptr.add(off), exp_buf[1].as_mut_ptr(), 2); }

`

406

401

`3

`

407

402

`};

`

408

``

`-

slice::from_raw_parts(exp_ptr, len)

`

``

403

`+

// SAFETY: max(2, 3) <= 3

`

``

404

`+

unsafe { slice::from_raw_parts(exp_buf.as_mut_ptr().into_inner(), len) }

`

409

405

`};

`

410

406

``

411

407

`let parts = &[

`

`@@ -485,7 +481,7 @@ impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128);

`

485

481

``

486

482

`` /// Helper function for writing a u64 into buf going from last to first, with curr.

``

487

483

`fn parse_u64_into(mut n: u64, buf: &mut [MaybeUninit; N], curr: &mut usize) {

`

488

``

`-

let buf_ptr = MaybeUninit::slice_as_mut_ptr(buf);

`

``

484

`+

let buf_ptr = buf.as_mut_ptr().into_inner();

`

489

485

`let lut_ptr = DEC_DIGITS_LUT.as_ptr();

`

490

486

`assert!(*curr > 19);

`

491

487

``

`@@ -609,11 +605,7 @@ fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::R

`

609

605

`// SAFETY: Guaranteed that we wrote at most 19 bytes, and there must be space

`

610

606

`// remaining since it has length 39

`

611

607

`unsafe {

`

612

``

`-

ptr::write_bytes(

`

613

``

`-

MaybeUninit::slice_as_mut_ptr(&mut buf).add(target),

`

614

``

`-

b'0',

`

615

``

`-

curr - target,

`

616

``

`-

);

`

``

608

`+

ptr::write_bytes(buf[target].as_mut_ptr(), b'0', curr - target);

`

617

609

`}

`

618

610

` curr = target;

`

619

611

``

`@@ -622,24 +614,21 @@ fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::R

`

622

614

`// Should this following branch be annotated with unlikely?

`

623

615

`if n != 0 {

`

624

616

`let target = buf.len() - 38;

`

625

``

`` -

// The raw buf_ptr pointer is only valid until buf is used the next time,

``

626

``

`` -

// buf buf is not used in this scope so we are good.

``

627

``

`-

let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);

`

628

617

`// SAFETY: At this point we wrote at most 38 bytes, pad up to that point,

`

629

618

`// There can only be at most 1 digit remaining.

`

630

619

`unsafe {

`

631

``

`-

ptr::write_bytes(buf_ptr.add(target), b'0', curr - target);

`

632

``

`-

curr = target - 1;

`

633

``

`-

*buf_ptr.add(curr) = (n as u8) + b'0';

`

``

620

`+

ptr::write_bytes(buf[target].as_mut_ptr(), b'0', curr - target);

`

634

621

`}

`

``

622

`+

curr = target - 1;

`

``

623

`+

buf[curr].write((n as u8) + b'0');

`

635

624

`}

`

636

625

`}

`

637

626

``

638

627

`` // SAFETY: curr > 0 (since we made buf large enough), and all the chars are valid

``

639

628

`` // UTF-8 since DEC_DIGITS_LUT is

``

640

629

`let buf_slice = unsafe {

`

641

630

` str::from_utf8_unchecked(slice::from_raw_parts(

`

642

``

`-

MaybeUninit::slice_as_mut_ptr(&mut buf).add(curr),

`

``

631

`+

buf.get_unchecked_mut(curr).as_mut_ptr(),

`

643

632

` buf.len() - curr,

`

644

633

`))

`

645

634

`};

`