Skip to content

Commit bd52790

Browse files
author
Julian Orth
committed
add {Imm,M}utableIntSlice
1 parent 9a2286d commit bd52790

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/libcore/slice.rs

+58
Original file line numberDiff line numberDiff line change
@@ -1853,3 +1853,61 @@ impl<'a, T: PartialOrd> PartialOrd for &'a [T] {
18531853
order::gt(self.iter(), other.iter())
18541854
}
18551855
}
1856+
1857+
/// Extension methods for immutable slices containing integers.
1858+
#[experimental]
1859+
pub trait ImmutableIntSlice<'a, U, S> {
1860+
/// Converts the slice to an immutable slice of unsigned integers with the same width.
1861+
fn as_unsigned(self) -> &'a [U];
1862+
/// Converts the slice to an immutable slice of signed integers with the same width.
1863+
fn as_signed(self) -> &'a [S];
1864+
}
1865+
1866+
/// Extension methods for mutable slices containing integers.
1867+
#[experimental]
1868+
pub trait MutableIntSlice<'a, U, S>: ImmutableIntSlice<'a, U, S> {
1869+
/// Converts the slice to a mutable slice of unsigned integers with the same width.
1870+
fn as_unsigned_mut(self) -> &'a mut [U];
1871+
/// Converts the slice to a mutable slice of signed integers with the same width.
1872+
fn as_signed_mut(self) -> &'a mut [S];
1873+
}
1874+
1875+
macro_rules! impl_immut_int_slice {
1876+
($u:ty, $s:ty, $t:ty) => {
1877+
#[experimental]
1878+
impl<'a> ImmutableIntSlice<'a, $u, $s> for $t {
1879+
#[inline]
1880+
fn as_unsigned(self) -> &'a [$u] { unsafe { transmute(self) } }
1881+
#[inline]
1882+
fn as_signed(self) -> &'a [$s] { unsafe { transmute(self) } }
1883+
}
1884+
}
1885+
}
1886+
macro_rules! impl_mut_int_slice {
1887+
($u:ty, $s:ty, $t:ty) => {
1888+
#[experimental]
1889+
impl<'a> MutableIntSlice<'a, $u, $s> for $t {
1890+
#[inline]
1891+
fn as_unsigned_mut(self) -> &'a mut [$u] { unsafe { transmute(self) } }
1892+
#[inline]
1893+
fn as_signed_mut(self) -> &'a mut [$s] { unsafe { transmute(self) } }
1894+
}
1895+
}
1896+
}
1897+
1898+
macro_rules! impl_int_slice {
1899+
($u:ty, $s:ty) => {
1900+
impl_immut_int_slice!($u, $s, &'a [$u])
1901+
impl_immut_int_slice!($u, $s, &'a [$s])
1902+
impl_immut_int_slice!($u, $s, &'a mut [$u])
1903+
impl_immut_int_slice!($u, $s, &'a mut [$s])
1904+
impl_mut_int_slice!($u, $s, &'a mut [$u])
1905+
impl_mut_int_slice!($u, $s, &'a mut [$s])
1906+
}
1907+
}
1908+
1909+
impl_int_slice!(u8, i8)
1910+
impl_int_slice!(u16, i16)
1911+
impl_int_slice!(u32, i32)
1912+
impl_int_slice!(u64, i64)
1913+
impl_int_slice!(uint, int)

0 commit comments

Comments
 (0)