File tree Expand file tree Collapse file tree 3 files changed +93
-0
lines changed Expand file tree Collapse file tree 3 files changed +93
-0
lines changed Original file line number Diff line number Diff line change @@ -1438,6 +1438,37 @@ impl<T: ?Sized> *const T {
1438
1438
self . is_aligned_to ( align_of :: < T > ( ) )
1439
1439
}
1440
1440
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
+
1441
1472
/// Returns whether the pointer is aligned to `align`.
1442
1473
///
1443
1474
/// For non-`Sized` pointees this operation considers only the data pointer,
Original file line number Diff line number Diff line change @@ -1693,6 +1693,37 @@ impl<T: ?Sized> *mut T {
1693
1693
self . is_aligned_to ( align_of :: < T > ( ) )
1694
1694
}
1695
1695
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
+
1696
1727
/// Returns whether the pointer is aligned to `align`.
1697
1728
///
1698
1729
/// For non-`Sized` pointees this operation considers only the data pointer,
Original file line number Diff line number Diff line change @@ -1285,6 +1285,37 @@ impl<T: ?Sized> NonNull<T> {
1285
1285
self . as_ptr ( ) . is_aligned ( )
1286
1286
}
1287
1287
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
+
1288
1319
/// Returns whether the pointer is aligned to `align`.
1289
1320
///
1290
1321
/// For non-`Sized` pointees this operation considers only the data pointer,
You can’t perform that action at this time.
0 commit comments