@@ -1364,8 +1364,72 @@ impl<T: ?Sized> *const T {
13641364 }
13651365
13661366 /// Returns whether the pointer is properly aligned for `T`.
1367- // #[cfg(not(bootstrap))] -- Calling this function in a const context from the bootstrap
1368- // compiler will always return false.
1367+ ///
1368+ /// # Examples
1369+ ///
1370+ /// Basic usage:
1371+ /// ```
1372+ /// #![feature(pointer_is_aligned)]
1373+ /// #![feature(pointer_byte_offsets)]
1374+ ///
1375+ /// let data: i32 = 42;
1376+ /// let ptr: *const i32 = &data;
1377+ ///
1378+ /// assert!(ptr.is_aligned());
1379+ /// assert!(!ptr.wrapping_byte_add(1).is_aligned());
1380+ /// ```
1381+ ///
1382+ /// # At compiletime
1383+ /// **Note: Alignment at compiletime is experimental and subject to change. See the
1384+ /// [tracking issue] for details.**
1385+ ///
1386+ /// At compiletime, the compiler may not know where a value will end up in memory.
1387+ /// Calling this function on a pointer created from a reference at compiletime will only
1388+ /// return `true` if the pointer is guaranteed to be aligned. This means that the pointer
1389+ /// is never aligned if cast to a type with a stricter alignment than the reference's
1390+ /// underlying allocation.
1391+ ///
1392+ #[ cfg_attr( bootstrap, doc = "```ignore" ) ]
1393+ #[ cfg_attr( not( bootstrap) , doc = "```" ) ]
1394+ /// #![feature(pointer_is_aligned)]
1395+ /// #![feature(const_pointer_is_aligned)]
1396+ ///
1397+ /// const _: () = {
1398+ /// let data: i32 = 42;
1399+ /// let ptr: *const i32 = &data;
1400+ /// assert!(ptr.is_aligned());
1401+ ///
1402+ /// // At runtime either `ptr1` or `ptr2` would be aligned,
1403+ /// // but at compiletime neither is aligned.
1404+ /// let ptr1: *const i64 = ptr.cast();
1405+ /// let ptr2: *const i64 = ptr.wrapping_add(1).cast();
1406+ /// assert!(!ptr1.is_aligned());
1407+ /// assert!(!ptr2.is_aligned());
1408+ /// };
1409+ /// ```
1410+ ///
1411+ /// If a pointer is created from a fixed address, this function behaves the same during
1412+ /// runtime and compiletime.
1413+ ///
1414+ #[ cfg_attr( bootstrap, doc = "```ignore" ) ]
1415+ #[ cfg_attr( not( bootstrap) , doc = "```" ) ]
1416+ /// #![feature(pointer_is_aligned)]
1417+ /// #![feature(const_pointer_is_aligned)]
1418+ ///
1419+ /// const _: () = {
1420+ /// let ptr = 40 as *const i32;
1421+ /// assert!(ptr.is_aligned());
1422+ ///
1423+ /// // For pointers with a known address, runtime and
1424+ /// // compiletime behavior are identical.
1425+ /// let ptr1: *const i64 = ptr.cast();
1426+ /// let ptr2: *const i64 = ptr.wrapping_add(1).cast();
1427+ /// assert!(ptr1.is_aligned());
1428+ /// assert!(!ptr2.is_aligned());
1429+ /// };
1430+ /// ```
1431+ ///
1432+ /// [tracking issue]: https://github.com/rust-lang/rust/issues/comming-soon
13691433 #[ must_use]
13701434 #[ inline]
13711435 #[ unstable( feature = "pointer_is_aligned" , issue = "96284" ) ]
@@ -1385,8 +1449,74 @@ impl<T: ?Sized> *const T {
13851449 /// # Panics
13861450 ///
13871451 /// The function panics if `align` is not a power-of-two (this includes 0).
1388- // #[cfg(not(bootstrap))] -- Calling this function in a const context from the bootstrap
1389- // compiler will always return false.
1452+ ///
1453+ /// # Examples
1454+ ///
1455+ /// Basic usage:
1456+ /// ```
1457+ /// #![feature(pointer_is_aligned)]
1458+ /// #![feature(pointer_byte_offsets)]
1459+ ///
1460+ /// let data: i32 = 42;
1461+ /// let ptr: *const i32 = &data;
1462+ ///
1463+ /// assert!(ptr.is_aligned_to(1));
1464+ /// assert!(ptr.is_aligned_to(2));
1465+ /// assert!(ptr.is_aligned_to(4));
1466+ ///
1467+ /// assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
1468+ /// assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
1469+ ///
1470+ /// assert_ne!(ptr.is_aligned_to(8), ptr.wrapping_add(1).is_aligned_to(8));
1471+ /// ```
1472+ ///
1473+ /// # At compiletime
1474+ /// **Note: Alignment at compiletime is experimental and subject to change. See the
1475+ /// [tracking issue] for details.**
1476+ ///
1477+ /// At compiletime, the compiler may not know where a value will end up in memory.
1478+ /// Calling this function on a pointer created from a reference at compiletime will only
1479+ /// return `true` if the pointer is guaranteed to be aligned. This means that the pointer
1480+ /// cannot be stricter aligned than the reference's underlying allocation.
1481+ ///
1482+ #[ cfg_attr( bootstrap, doc = "```ignore" ) ]
1483+ #[ cfg_attr( not( bootstrap) , doc = "```" ) ]
1484+ /// #![feature(pointer_is_aligned)]
1485+ /// #![feature(const_pointer_is_aligned)]
1486+ ///
1487+ /// const _: () = {
1488+ /// let data: i32 = 42;
1489+ /// let ptr: *const i32 = &data;
1490+ ///
1491+ /// assert!(ptr.is_aligned_to(1));
1492+ /// assert!(ptr.is_aligned_to(2));
1493+ /// assert!(ptr.is_aligned_to(4));
1494+ ///
1495+ /// // At compiletime, we know for sure that the pointer isn't aligned to 8.
1496+ /// assert!(!ptr.is_aligned_to(8));
1497+ /// assert!(!ptr.wrapping_add(1).is_aligned_to(8));
1498+ /// };
1499+ /// ```
1500+ ///
1501+ /// If a pointer is created from a fixed address, this function behaves the same during
1502+ /// runtime and compiletime.
1503+ ///
1504+ #[ cfg_attr( bootstrap, doc = "```ignore" ) ]
1505+ #[ cfg_attr( not( bootstrap) , doc = "```" ) ]
1506+ /// #![feature(pointer_is_aligned)]
1507+ /// #![feature(const_pointer_is_aligned)]
1508+ ///
1509+ /// const _: () = {
1510+ /// let ptr = 40 as *const i32;
1511+ /// assert!(ptr.is_aligned_to(1));
1512+ /// assert!(ptr.is_aligned_to(2));
1513+ /// assert!(ptr.is_aligned_to(4));
1514+ /// assert!(ptr.is_aligned_to(8));
1515+ /// assert!(!ptr.is_aligned_to(16));
1516+ /// };
1517+ /// ```
1518+ ///
1519+ /// [tracking issue]: https://github.com/rust-lang/rust/issues/comming-soon
13901520 #[ must_use]
13911521 #[ inline]
13921522 #[ unstable( feature = "pointer_is_aligned" , issue = "96284" ) ]
0 commit comments