Add <*const T>::align_offset and use it in memchr · rust-lang/rust@2787a28 (original) (raw)

`@@ -1064,7 +1064,43 @@ impl<T: ?Sized> *const T {

`

1064

1064

`copy_nonoverlapping(self, dest, count)

`

1065

1065

`}

`

1066

1066

``

1067

``

-

``

1067

`+

/// Computes the byte offset that needs to be applied in order to

`

``

1068

`` +

/// make the pointer aligned to align.

``

``

1069

`+

/// If it is not possible to align the pointer, the implementation returns

`

``

1070

`` +

/// usize::max_value().

``

``

1071

`+

///

`

``

1072

`+

/// There are no guarantees whatsover that offsetting the pointer will not

`

``

1073

`+

/// overflow or go beyond the allocation that the pointer points into.

`

``

1074

`+

/// It is up to the caller to ensure that the returned offset is correct

`

``

1075

`+

/// in all terms other than alignment.

`

``

1076

`+

///

`

``

1077

`+

/// # Examples

`

``

1078

`+

///

`

``

1079

`` +

/// Accessing adjacent u8 as u16

``

``

1080

`+

///

`

``

1081


/// ```

``

1082

`+

/// # #![feature(align_offset)]

`

``

1083

`+

/// # fn foo(n: usize) {

`

``

1084

`+

/// # use std::mem::align_of;

`

``

1085

`+

/// # unsafe {

`

``

1086

`+

/// let x = [5u8, 6u8, 7u8, 8u8, 9u8];

`

``

1087

`+

/// let ptr = &x[n] as *const u8;

`

``

1088

`+

/// let offset = ptr.align_offset(align_of::());

`

``

1089

`+

/// if offset < x.len() - n - 1 {

`

``

1090

`+

/// let u16_ptr = ptr.offset(offset as isize) as *const u16;

`

``

1091

`+

/// assert_ne!(*u16_ptr, 500);

`

``

1092

`+

/// } else {

`

``

1093

`` +

/// // while the pointer can be aligned via offset, it would point

``

``

1094

`+

/// // outside the allocation

`

``

1095

`+

/// }

`

``

1096

`+

/// # } }

`

``

1097


/// ```

``

1098

`+

#[unstable(feature = "align_offset", issue = "44488")]

`

``

1099

`+

pub fn align_offset(self, align: usize) -> usize {

`

``

1100

`+

unsafe {

`

``

1101

`+

intrinsics::align_offset(self as *const _, align)

`

``

1102

`+

}

`

``

1103

`+

}

`

1068

1104

`}

`

1069

1105

``

1070

1106

`#[lang = "mut_ptr"]

`

`@@ -1284,6 +1320,43 @@ impl<T: ?Sized> *mut T {

`

1284

1320

`}

`

1285

1321

`}

`

1286

1322

``

``

1323

`+

/// Computes the byte offset that needs to be applied in order to

`

``

1324

`` +

/// make the pointer aligned to align.

``

``

1325

`+

/// If it is not possible to align the pointer, the implementation returns

`

``

1326

`` +

/// usize::max_value().

``

``

1327

`+

///

`

``

1328

`+

/// There are no guarantees whatsover that offsetting the pointer will not

`

``

1329

`+

/// overflow or go beyond the allocation that the pointer points into.

`

``

1330

`+

/// It is up to the caller to ensure that the returned offset is correct

`

``

1331

`+

/// in all terms other than alignment.

`

``

1332

`+

///

`

``

1333

`+

/// # Examples

`

``

1334

`+

///

`

``

1335

`` +

/// Accessing adjacent u8 as u16

``

``

1336

`+

///

`

``

1337


/// ```

``

1338

`+

/// # #![feature(align_offset)]

`

``

1339

`+

/// # fn foo(n: usize) {

`

``

1340

`+

/// # use std::mem::align_of;

`

``

1341

`+

/// # unsafe {

`

``

1342

`+

/// let x = [5u8, 6u8, 7u8, 8u8, 9u8];

`

``

1343

`+

/// let ptr = &x[n] as *const u8;

`

``

1344

`+

/// let offset = ptr.align_offset(align_of::());

`

``

1345

`+

/// if offset < x.len() - n - 1 {

`

``

1346

`+

/// let u16_ptr = ptr.offset(offset as isize) as *const u16;

`

``

1347

`+

/// assert_ne!(*u16_ptr, 500);

`

``

1348

`+

/// } else {

`

``

1349

`` +

/// // while the pointer can be aligned via offset, it would point

``

``

1350

`+

/// // outside the allocation

`

``

1351

`+

/// }

`

``

1352

`+

/// # } }

`

``

1353


/// ```

``

1354

`+

#[unstable(feature = "align_offset", issue = "44488")]

`

``

1355

`+

pub fn align_offset(self, align: usize) -> usize {

`

``

1356

`+

unsafe {

`

``

1357

`+

intrinsics::align_offset(self as *const _, align)

`

``

1358

`+

}

`

``

1359

`+

}

`

1287

1360

``

1288

1361

`` /// Calculates the offset from a pointer (convenience for .offset(count as isize)).

``

1289

1362

`///

`