@@ -1635,8 +1635,12 @@ impl<T: ?Sized> *mut T {
1635
1635
/// #![feature(pointer_is_aligned)]
1636
1636
/// #![feature(pointer_byte_offsets)]
1637
1637
///
1638
- /// let data: i32 = 42;
1639
- /// let ptr: *const i32 = &data;
1638
+ /// // On some platforms, the alignment of i32 is less than 4.
1639
+ /// #[repr(align(4))]
1640
+ /// struct AlignedI32(i32);
1641
+ ///
1642
+ /// let mut data = AlignedI32(42);
1643
+ /// let ptr = &mut data as *mut AlignedI32;
1640
1644
///
1641
1645
/// assert!(ptr.is_aligned());
1642
1646
/// assert!(!ptr.wrapping_byte_add(1).is_aligned());
@@ -1656,16 +1660,22 @@ impl<T: ?Sized> *mut T {
1656
1660
#[ cfg_attr( not( bootstrap) , doc = "```" ) ]
1657
1661
/// #![feature(pointer_is_aligned)]
1658
1662
/// #![feature(const_pointer_is_aligned)]
1663
+ /// #![feature(const_mut_refs)]
1664
+ ///
1665
+ /// // On some platforms, the alignment of primitives is less than their size.
1666
+ /// #[repr(align(4))]
1667
+ /// struct AlignedI32(i32);
1668
+ /// #[repr(align(8))]
1669
+ /// struct AlignedI64(i64);
1659
1670
///
1660
1671
/// const _: () = {
1661
- /// let data: i32 = 42 ;
1662
- /// let ptr: *const i32 = &data;
1672
+ /// let mut data = AlignedI32(42) ;
1673
+ /// let ptr = &mut data as *mut AlignedI32 ;
1663
1674
/// assert!(ptr.is_aligned());
1664
1675
///
1665
- /// // At runtime either `ptr1` or `ptr2` would be aligned,
1666
- /// // but at compiletime neither is aligned.
1667
- /// let ptr1: *const i64 = ptr.cast();
1668
- /// let ptr2: *const i64 = ptr.wrapping_add(1).cast();
1676
+ /// // At runtime either `ptr1` or `ptr2` would be aligned, but at compiletime neither is aligned.
1677
+ /// let ptr1 = ptr.cast::<AlignedI64>();
1678
+ /// let ptr2 = ptr.wrapping_add(1).cast::<AlignedI64>();
1669
1679
/// assert!(!ptr1.is_aligned());
1670
1680
/// assert!(!ptr2.is_aligned());
1671
1681
/// };
@@ -1679,16 +1689,23 @@ impl<T: ?Sized> *mut T {
1679
1689
/// #![feature(pointer_is_aligned)]
1680
1690
/// #![feature(const_pointer_is_aligned)]
1681
1691
///
1682
- /// // At compiletime, neither `CONST_PTR` nor `CONST_PTR + 1` is aligned.
1683
- /// const CONST_PTR: *const i32 = &42;
1684
- /// const _: () = assert!(!CONST_PTR.cast::<i64>().is_aligned());
1685
- /// const _: () = assert!(!CONST_PTR.wrapping_add(1).cast::<i64>().is_aligned());
1692
+ /// // On some platforms, the alignment of primitives is less than their size.
1693
+ /// #[repr(align(4))]
1694
+ /// struct AlignedI32(i32);
1695
+ /// #[repr(align(8))]
1696
+ /// struct AlignedI64(i64);
1697
+ ///
1698
+ /// // At compiletime, neither `COMPTIME_PTR` nor `COMPTIME_PTR + 1` is aligned.
1699
+ /// // Also, note that mutable references are not allowed in the final value of constants.
1700
+ /// const COMPTIME_PTR: *mut AlignedI32 = (&AlignedI32(42) as *const AlignedI32).cast_mut();
1701
+ /// const _: () = assert!(!COMPTIME_PTR.cast::<AlignedI64>().is_aligned());
1702
+ /// const _: () = assert!(!COMPTIME_PTR.wrapping_add(1).cast::<AlignedI64>().is_aligned());
1686
1703
///
1687
1704
/// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned.
1688
- /// let runtime_ptr = CONST_PTR ;
1705
+ /// let runtime_ptr = COMPTIME_PTR ;
1689
1706
/// assert_ne!(
1690
- /// runtime_ptr.cast::<i64 >().is_aligned(),
1691
- /// runtime_ptr.wrapping_add(1).cast::<i64 >().is_aligned(),
1707
+ /// runtime_ptr.cast::<AlignedI64 >().is_aligned(),
1708
+ /// runtime_ptr.wrapping_add(1).cast::<AlignedI64 >().is_aligned(),
1692
1709
/// );
1693
1710
/// ```
1694
1711
///
@@ -1700,29 +1717,34 @@ impl<T: ?Sized> *mut T {
1700
1717
/// #![feature(pointer_is_aligned)]
1701
1718
/// #![feature(const_pointer_is_aligned)]
1702
1719
///
1720
+ /// // On some platforms, the alignment of primitives is less than their size.
1721
+ /// #[repr(align(4))]
1722
+ /// struct AlignedI32(i32);
1723
+ /// #[repr(align(8))]
1724
+ /// struct AlignedI64(i64);
1725
+ ///
1703
1726
/// const _: () = {
1704
- /// let ptr = 40 as *const i32 ;
1727
+ /// let ptr = 40 as *mut AlignedI32 ;
1705
1728
/// assert!(ptr.is_aligned());
1706
1729
///
1707
- /// // For pointers with a known address, runtime and
1708
- /// // compiletime behavior are identical.
1709
- /// let ptr1: *const i64 = ptr.cast();
1710
- /// let ptr2: *const i64 = ptr.wrapping_add(1).cast();
1730
+ /// // For pointers with a known address, runtime and compiletime behavior are identical.
1731
+ /// let ptr1 = ptr.cast::<AlignedI64>();
1732
+ /// let ptr2 = ptr.wrapping_add(1).cast::<AlignedI64>();
1711
1733
/// assert!(ptr1.is_aligned());
1712
1734
/// assert!(!ptr2.is_aligned());
1713
1735
/// };
1714
1736
/// ```
1715
1737
///
1716
- /// [tracking issue]: https://github.com/rust-lang/rust/issues/comming-soon
1738
+ /// [tracking issue]: https://github.com/rust-lang/rust/issues/104203
1717
1739
#[ must_use]
1718
1740
#[ inline]
1719
1741
#[ unstable( feature = "pointer_is_aligned" , issue = "96284" ) ]
1720
- #[ rustc_const_unstable( feature = "const_pointer_is_aligned" , issue = "none " ) ]
1742
+ #[ rustc_const_unstable( feature = "const_pointer_is_aligned" , issue = "104203 " ) ]
1721
1743
pub const fn is_aligned ( self ) -> bool
1722
1744
where
1723
1745
T : Sized ,
1724
1746
{
1725
- self . is_aligned_to ( core :: mem:: align_of :: < T > ( ) )
1747
+ self . is_aligned_to ( mem:: align_of :: < T > ( ) )
1726
1748
}
1727
1749
1728
1750
/// Returns whether the pointer is aligned to `align`.
@@ -1741,8 +1763,12 @@ impl<T: ?Sized> *mut T {
1741
1763
/// #![feature(pointer_is_aligned)]
1742
1764
/// #![feature(pointer_byte_offsets)]
1743
1765
///
1744
- /// let data: i32 = 42;
1745
- /// let ptr: *const i32 = &data;
1766
+ /// // On some platforms, the alignment of i32 is less than 4.
1767
+ /// #[repr(align(4))]
1768
+ /// struct AlignedI32(i32);
1769
+ ///
1770
+ /// let mut data = AlignedI32(42);
1771
+ /// let ptr = &mut data as *mut AlignedI32;
1746
1772
///
1747
1773
/// assert!(ptr.is_aligned_to(1));
1748
1774
/// assert!(ptr.is_aligned_to(2));
@@ -1767,10 +1793,15 @@ impl<T: ?Sized> *mut T {
1767
1793
#[ cfg_attr( not( bootstrap) , doc = "```" ) ]
1768
1794
/// #![feature(pointer_is_aligned)]
1769
1795
/// #![feature(const_pointer_is_aligned)]
1796
+ /// #![feature(const_mut_refs)]
1797
+ ///
1798
+ /// // On some platforms, the alignment of i32 is less than 4.
1799
+ /// #[repr(align(4))]
1800
+ /// struct AlignedI32(i32);
1770
1801
///
1771
1802
/// const _: () = {
1772
- /// let data: i32 = 42 ;
1773
- /// let ptr: *const i32 = &data;
1803
+ /// let mut data = AlignedI32(42) ;
1804
+ /// let ptr = &mut data as *mut AlignedI32 ;
1774
1805
///
1775
1806
/// assert!(ptr.is_aligned_to(1));
1776
1807
/// assert!(ptr.is_aligned_to(2));
@@ -1790,13 +1821,18 @@ impl<T: ?Sized> *mut T {
1790
1821
/// #![feature(pointer_is_aligned)]
1791
1822
/// #![feature(const_pointer_is_aligned)]
1792
1823
///
1793
- /// // At compiletime, neither `CONST_PTR` nor `CONST_PTR + 1` is aligned.
1794
- /// const CONST_PTR: *const i32 = &42;
1795
- /// const _: () = assert!(!CONST_PTR.is_aligned_to(8));
1796
- /// const _: () = assert!(!CONST_PTR.wrapping_add(1).is_aligned_to(8));
1824
+ /// // On some platforms, the alignment of i32 is less than 4.
1825
+ /// #[repr(align(4))]
1826
+ /// struct AlignedI32(i32);
1827
+ ///
1828
+ /// // At compiletime, neither `COMPTIME_PTR` nor `COMPTIME_PTR + 1` is aligned.
1829
+ /// // Also, note that mutable references are not allowed in the final value of constants.
1830
+ /// const COMPTIME_PTR: *mut AlignedI32 = (&AlignedI32(42) as *const AlignedI32).cast_mut();
1831
+ /// const _: () = assert!(!COMPTIME_PTR.is_aligned_to(8));
1832
+ /// const _: () = assert!(!COMPTIME_PTR.wrapping_add(1).is_aligned_to(8));
1797
1833
///
1798
1834
/// // At runtime, either `runtime_ptr` or `runtime_ptr + 1` is aligned.
1799
- /// let runtime_ptr = CONST_PTR ;
1835
+ /// let runtime_ptr = COMPTIME_PTR ;
1800
1836
/// assert_ne!(
1801
1837
/// runtime_ptr.is_aligned_to(8),
1802
1838
/// runtime_ptr.wrapping_add(1).is_aligned_to(8),
@@ -1812,7 +1848,7 @@ impl<T: ?Sized> *mut T {
1812
1848
/// #![feature(const_pointer_is_aligned)]
1813
1849
///
1814
1850
/// const _: () = {
1815
- /// let ptr = 40 as *const i32 ;
1851
+ /// let ptr = 40 as *mut u8 ;
1816
1852
/// assert!(ptr.is_aligned_to(1));
1817
1853
/// assert!(ptr.is_aligned_to(2));
1818
1854
/// assert!(ptr.is_aligned_to(4));
@@ -1821,14 +1857,14 @@ impl<T: ?Sized> *mut T {
1821
1857
/// };
1822
1858
/// ```
1823
1859
///
1824
- /// [tracking issue]: https://github.com/rust-lang/rust/issues/comming-soon
1860
+ /// [tracking issue]: https://github.com/rust-lang/rust/issues/104203
1825
1861
#[ must_use]
1826
1862
#[ inline]
1827
1863
#[ unstable( feature = "pointer_is_aligned" , issue = "96284" ) ]
1828
- #[ rustc_const_unstable( feature = "const_pointer_is_aligned" , issue = "none " ) ]
1864
+ #[ rustc_const_unstable( feature = "const_pointer_is_aligned" , issue = "104203 " ) ]
1829
1865
pub const fn is_aligned_to ( self , align : usize ) -> bool {
1830
1866
if !align. is_power_of_two ( ) {
1831
- panic ! ( "is_aligned_to: align is not a power-of-two" )
1867
+ panic ! ( "is_aligned_to: align is not a power-of-two" ) ;
1832
1868
}
1833
1869
1834
1870
// We can't use the address of `self` in a `const fn`, so we use `align_offset` instead.
0 commit comments