Skip to content

Commit 371f602

Browse files
committed
implement ptr::is_aligned_for and NonNull::is_aligned_for.
1 parent 59372f2 commit 371f602

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

library/core/src/ptr/const_ptr.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,37 @@ impl<T: ?Sized> *const T {
14381438
self.is_aligned_to(align_of::<T>())
14391439
}
14401440

1441+
/// Returns whether the pointer is properly aligned for `U`.
1442+
///
1443+
/// # Examples
1444+
///
1445+
/// ```
1446+
/// #![feature(pointer_is_aligned_for)]
1447+
///
1448+
/// // On some platforms, the alignment of i32 is less than 4.
1449+
/// #[repr(align(4))]
1450+
/// struct AlignedI32(i32);
1451+
///
1452+
/// // On some platforms, the alignment of u32 is less than 4.
1453+
/// #[repr(align(4))]
1454+
/// struct AlignedU32(u32);
1455+
///
1456+
/// let data = AlignedI32(42);
1457+
/// let ptr = &data as *const AlignedI32;
1458+
///
1459+
/// assert!(ptr.is_aligned_for::<AlignedU32>());
1460+
/// assert!(!ptr.wrapping_byte_add(1).is_aligned_for::<AlignedU32>());
1461+
/// ```
1462+
#[must_use]
1463+
#[inline]
1464+
#[unstable(feature = "pointer_is_aligned_for", issue = "140980")]
1465+
pub fn is_aligned_for<U: Sized>(self) -> bool
1466+
where
1467+
T: Sized,
1468+
{
1469+
self.is_aligned_to(align_of::<U>())
1470+
}
1471+
14411472
/// Returns whether the pointer is aligned to `align`.
14421473
///
14431474
/// For non-`Sized` pointees this operation considers only the data pointer,

library/core/src/ptr/mut_ptr.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,37 @@ impl<T: ?Sized> *mut T {
16931693
self.is_aligned_to(align_of::<T>())
16941694
}
16951695

1696+
/// Returns whether the pointer is properly aligned for `U`.
1697+
///
1698+
/// # Examples
1699+
///
1700+
/// ```
1701+
/// #![feature(pointer_is_aligned_for)]
1702+
///
1703+
/// // On some platforms, the alignment of i32 is less than 4.
1704+
/// #[repr(align(4))]
1705+
/// struct AlignedI32(i32);
1706+
///
1707+
/// // On some platforms, the alignment of u32 is less than 4.
1708+
/// #[repr(align(4))]
1709+
/// struct AlignedU32(u32);
1710+
///
1711+
/// let mut data = AlignedI32(42);
1712+
/// let ptr = &mut data as *mut AlignedI32;
1713+
///
1714+
/// assert!(ptr.is_aligned_for::<AlignedU32>());
1715+
/// assert!(!ptr.wrapping_byte_add(1).is_aligned_for::<AlignedU32>());
1716+
/// ```
1717+
#[must_use]
1718+
#[inline]
1719+
#[unstable(feature = "pointer_is_aligned_for", issue = "140980")]
1720+
pub fn is_aligned_for<U: Sized>(self) -> bool
1721+
where
1722+
T: Sized,
1723+
{
1724+
self.is_aligned_to(align_of::<U>())
1725+
}
1726+
16961727
/// Returns whether the pointer is aligned to `align`.
16971728
///
16981729
/// For non-`Sized` pointees this operation considers only the data pointer,

library/core/src/ptr/non_null.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,37 @@ impl<T: ?Sized> NonNull<T> {
12851285
self.as_ptr().is_aligned()
12861286
}
12871287

1288+
/// Returns whether the pointer is properly aligned for `T`.
1289+
///
1290+
/// # Examples
1291+
///
1292+
/// ```
1293+
/// use std::ptr::NonNull;
1294+
///
1295+
/// // On some platforms, the alignment of i32 is less than 4.
1296+
/// #[repr(align(4))]
1297+
/// struct AlignedI32(i32);
1298+
///
1299+
/// // On some platforms, the alignment of u32 is less than 4.
1300+
/// #[repr(align(4))]
1301+
/// struct AlignedU32(u32);
1302+
///
1303+
/// let data = AlignedI32(42);
1304+
/// let ptr = NonNull::<AlignedI32>::from(&data);
1305+
///
1306+
/// assert!(ptr.is_aligned_for::<AlignedU32>());
1307+
/// assert!(!NonNull::new(ptr.as_ptr().wrapping_byte_add(1)).unwrap().is_aligned_for::<AlignedU32>());
1308+
/// ```
1309+
#[must_use]
1310+
#[inline]
1311+
#[unstable(feature = "pointer_is_aligned_for", issue = "140980")]
1312+
pub fn is_aligned_for<U: Sized>(self) -> bool
1313+
where
1314+
T: Sized,
1315+
{
1316+
self.as_ptr().is_aligned_for::<U>()
1317+
}
1318+
12881319
/// Returns whether the pointer is aligned to `align`.
12891320
///
12901321
/// For non-`Sized` pointees this operation considers only the data pointer,

0 commit comments

Comments
 (0)