@@ -1764,9 +1764,7 @@ impl<T> [T] {
1764
1764
/// the index `N` itself) and the slice will contain all
1765
1765
/// indices from `[N, len)` (excluding the index `len` itself).
1766
1766
///
1767
- /// # Panics
1768
- ///
1769
- /// Panics if `N > len`.
1767
+ /// Returns `None` if the slice has less than `N` elements.
1770
1768
///
1771
1769
/// # Examples
1772
1770
///
@@ -1776,31 +1774,38 @@ impl<T> [T] {
1776
1774
/// let v = &[1, 2, 3, 4, 5, 6][..];
1777
1775
///
1778
1776
/// {
1779
- /// let (left, right) = v.split_array_ref::<0>();
1777
+ /// let (left, right) = v.split_array_ref::<0>().unwrap() ;
1780
1778
/// assert_eq!(left, &[]);
1781
1779
/// assert_eq!(right, [1, 2, 3, 4, 5, 6]);
1782
1780
/// }
1783
1781
///
1784
1782
/// {
1785
- /// let (left, right) = v.split_array_ref::<2>();
1783
+ /// let (left, right) = v.split_array_ref::<2>().unwrap() ;
1786
1784
/// assert_eq!(left, &[1, 2]);
1787
1785
/// assert_eq!(right, [3, 4, 5, 6]);
1788
1786
/// }
1789
1787
///
1790
1788
/// {
1791
- /// let (left, right) = v.split_array_ref::<6>();
1789
+ /// let (left, right) = v.split_array_ref::<6>().unwrap() ;
1792
1790
/// assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
1793
1791
/// assert_eq!(right, []);
1794
1792
/// }
1793
+ ///
1794
+ /// assert!(v.split_array_ref::<7>().is_none());
1795
1795
/// ```
1796
1796
#[ unstable( feature = "split_array" , reason = "new API" , issue = "90091" ) ]
1797
1797
#[ inline]
1798
1798
#[ track_caller]
1799
1799
#[ must_use]
1800
- pub fn split_array_ref < const N : usize > ( & self ) -> ( & [ T ; N ] , & [ T ] ) {
1801
- let ( a, b) = self . split_at ( N ) ;
1802
- // SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at)
1803
- unsafe { ( & * ( a. as_ptr ( ) as * const [ T ; N ] ) , b) }
1800
+ pub fn split_array_ref < const N : usize > ( & self ) -> Option < ( & [ T ; N ] , & [ T ] ) > {
1801
+ if N > self . len ( ) {
1802
+ None
1803
+ } else {
1804
+ // SAFETY: 0 <= N <= len
1805
+ let ( a, b) = unsafe { self . split_at_unchecked ( N ) } ;
1806
+ // SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at)
1807
+ Some ( unsafe { ( & * ( a. as_ptr ( ) as * const [ T ; N ] ) , b) } )
1808
+ }
1804
1809
}
1805
1810
1806
1811
/// Divides one mutable slice into an array and a remainder slice at an index.
@@ -1809,31 +1814,36 @@ impl<T> [T] {
1809
1814
/// the index `N` itself) and the slice will contain all
1810
1815
/// indices from `[N, len)` (excluding the index `len` itself).
1811
1816
///
1812
- /// # Panics
1813
- ///
1814
- /// Panics if `N > len`.
1817
+ /// Returns `None` if the slice has less than `N` elements.
1815
1818
///
1816
1819
/// # Examples
1817
1820
///
1818
1821
/// ```
1819
1822
/// #![feature(split_array)]
1820
1823
///
1821
1824
/// let mut v = &mut [1, 0, 3, 0, 5, 6][..];
1822
- /// let (left, right) = v.split_array_mut::<2>();
1825
+ /// let (left, right) = v.split_array_mut::<2>().unwrap() ;
1823
1826
/// assert_eq!(left, &mut [1, 0]);
1824
1827
/// assert_eq!(right, [3, 0, 5, 6]);
1825
1828
/// left[1] = 2;
1826
1829
/// right[1] = 4;
1827
1830
/// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
1831
+ ///
1832
+ /// assert!(v.split_array_mut::<7>().is_none());
1828
1833
/// ```
1829
1834
#[ unstable( feature = "split_array" , reason = "new API" , issue = "90091" ) ]
1830
1835
#[ inline]
1831
1836
#[ track_caller]
1832
1837
#[ must_use]
1833
- pub fn split_array_mut < const N : usize > ( & mut self ) -> ( & mut [ T ; N ] , & mut [ T ] ) {
1834
- let ( a, b) = self . split_at_mut ( N ) ;
1835
- // SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at_mut)
1836
- unsafe { ( & mut * ( a. as_mut_ptr ( ) as * mut [ T ; N ] ) , b) }
1838
+ pub fn split_array_mut < const N : usize > ( & mut self ) -> Option < ( & mut [ T ; N ] , & mut [ T ] ) > {
1839
+ if N > self . len ( ) {
1840
+ None
1841
+ } else {
1842
+ // SAFETY: 0 <= N <= len
1843
+ let ( a, b) = unsafe { self . split_at_mut_unchecked ( N ) } ;
1844
+ // SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at)
1845
+ Some ( unsafe { ( & mut * ( a. as_mut_ptr ( ) as * mut [ T ; N ] ) , b) } )
1846
+ }
1837
1847
}
1838
1848
1839
1849
/// Divides one slice into an array and a remainder slice at an index from
@@ -1843,9 +1853,7 @@ impl<T> [T] {
1843
1853
/// the index `len - N` itself) and the array will contain all
1844
1854
/// indices from `[len - N, len)` (excluding the index `len` itself).
1845
1855
///
1846
- /// # Panics
1847
- ///
1848
- /// Panics if `N > len`.
1856
+ /// Returns `None` if the slice has less than `N` elements.
1849
1857
///
1850
1858
/// # Examples
1851
1859
///
@@ -1855,31 +1863,37 @@ impl<T> [T] {
1855
1863
/// let v = &[1, 2, 3, 4, 5, 6][..];
1856
1864
///
1857
1865
/// {
1858
- /// let (left, right) = v.rsplit_array_ref::<0>();
1866
+ /// let (left, right) = v.rsplit_array_ref::<0>().unwrap() ;
1859
1867
/// assert_eq!(left, [1, 2, 3, 4, 5, 6]);
1860
1868
/// assert_eq!(right, &[]);
1861
1869
/// }
1862
1870
///
1863
1871
/// {
1864
- /// let (left, right) = v.rsplit_array_ref::<2>();
1872
+ /// let (left, right) = v.rsplit_array_ref::<2>().unwrap() ;
1865
1873
/// assert_eq!(left, [1, 2, 3, 4]);
1866
1874
/// assert_eq!(right, &[5, 6]);
1867
1875
/// }
1868
1876
///
1869
1877
/// {
1870
- /// let (left, right) = v.rsplit_array_ref::<6>();
1878
+ /// let (left, right) = v.rsplit_array_ref::<6>().unwrap() ;
1871
1879
/// assert_eq!(left, []);
1872
1880
/// assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
1873
1881
/// }
1882
+ ///
1883
+ /// assert!(v.rsplit_array_ref::<7>().is_none());
1874
1884
/// ```
1875
1885
#[ unstable( feature = "split_array" , reason = "new API" , issue = "90091" ) ]
1876
1886
#[ inline]
1877
1887
#[ must_use]
1878
- pub fn rsplit_array_ref < const N : usize > ( & self ) -> ( & [ T ] , & [ T ; N ] ) {
1879
- assert ! ( N <= self . len( ) ) ;
1880
- let ( a, b) = self . split_at ( self . len ( ) - N ) ;
1881
- // SAFETY: b points to [T; N]? Yes it's [T] of length N (checked by split_at)
1882
- unsafe { ( a, & * ( b. as_ptr ( ) as * const [ T ; N ] ) ) }
1888
+ pub fn rsplit_array_ref < const N : usize > ( & self ) -> Option < ( & [ T ] , & [ T ; N ] ) > {
1889
+ if N > self . len ( ) {
1890
+ None
1891
+ } else {
1892
+ // SAFETY: N <= len; thus 0 <= len - N <= len
1893
+ let ( a, b) = unsafe { self . split_at_unchecked ( self . len ( ) - N ) } ;
1894
+ // SAFETY: b points to [T; N]? Yes it's [T] of length N (checked by split_at)
1895
+ Some ( unsafe { ( a, & * ( b. as_ptr ( ) as * const [ T ; N ] ) ) } )
1896
+ }
1883
1897
}
1884
1898
1885
1899
/// Divides one mutable slice into an array and a remainder slice at an
@@ -1889,31 +1903,35 @@ impl<T> [T] {
1889
1903
/// the index `N` itself) and the array will contain all
1890
1904
/// indices from `[len - N, len)` (excluding the index `len` itself).
1891
1905
///
1892
- /// # Panics
1893
- ///
1894
- /// Panics if `N > len`.
1906
+ /// Returns `None` if the slice has less than `N` elements.
1895
1907
///
1896
1908
/// # Examples
1897
1909
///
1898
1910
/// ```
1899
1911
/// #![feature(split_array)]
1900
1912
///
1901
1913
/// let mut v = &mut [1, 0, 3, 0, 5, 6][..];
1902
- /// let (left, right) = v.rsplit_array_mut::<4>();
1914
+ /// let (left, right) = v.rsplit_array_mut::<4>().unwrap() ;
1903
1915
/// assert_eq!(left, [1, 0]);
1904
1916
/// assert_eq!(right, &mut [3, 0, 5, 6]);
1905
1917
/// left[1] = 2;
1906
1918
/// right[1] = 4;
1907
1919
/// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
1920
+ ///
1921
+ /// assert!(v.rsplit_array_mut::<7>().is_none());
1908
1922
/// ```
1909
1923
#[ unstable( feature = "split_array" , reason = "new API" , issue = "90091" ) ]
1910
1924
#[ inline]
1911
1925
#[ must_use]
1912
- pub fn rsplit_array_mut < const N : usize > ( & mut self ) -> ( & mut [ T ] , & mut [ T ; N ] ) {
1913
- assert ! ( N <= self . len( ) ) ;
1914
- let ( a, b) = self . split_at_mut ( self . len ( ) - N ) ;
1915
- // SAFETY: b points to [T; N]? Yes it's [T] of length N (checked by split_at_mut)
1916
- unsafe { ( a, & mut * ( b. as_mut_ptr ( ) as * mut [ T ; N ] ) ) }
1926
+ pub fn rsplit_array_mut < const N : usize > ( & mut self ) -> Option < ( & mut [ T ] , & mut [ T ; N ] ) > {
1927
+ if N > self . len ( ) {
1928
+ None
1929
+ } else {
1930
+ // SAFETY: N <= len; thus 0 <= len - N <= len
1931
+ let ( a, b) = unsafe { self . split_at_mut_unchecked ( self . len ( ) - N ) } ;
1932
+ // SAFETY: b points to [T; N]? Yes it's [T] of length N (checked by split_at_mut)
1933
+ Some ( unsafe { ( a, & mut * ( b. as_mut_ptr ( ) as * mut [ T ; N ] ) ) } )
1934
+ }
1917
1935
}
1918
1936
1919
1937
/// Returns an iterator over subslices separated by elements that match
0 commit comments