Skip to content

Commit 218cb4b

Browse files
committed
auto merge of #17748 : mahkoh/rust/int_slice, r=aturon
2 parents afb5fcd + bd52790 commit 218cb4b

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
@@ -1945,3 +1945,61 @@ impl<'a, T: PartialOrd> PartialOrd for &'a [T] {
19451945
order::gt(self.iter(), other.iter())
19461946
}
19471947
}
1948+
1949+
/// Extension methods for immutable slices containing integers.
1950+
#[experimental]
1951+
pub trait ImmutableIntSlice<'a, U, S> {
1952+
/// Converts the slice to an immutable slice of unsigned integers with the same width.
1953+
fn as_unsigned(self) -> &'a [U];
1954+
/// Converts the slice to an immutable slice of signed integers with the same width.
1955+
fn as_signed(self) -> &'a [S];
1956+
}
1957+
1958+
/// Extension methods for mutable slices containing integers.
1959+
#[experimental]
1960+
pub trait MutableIntSlice<'a, U, S>: ImmutableIntSlice<'a, U, S> {
1961+
/// Converts the slice to a mutable slice of unsigned integers with the same width.
1962+
fn as_unsigned_mut(self) -> &'a mut [U];
1963+
/// Converts the slice to a mutable slice of signed integers with the same width.
1964+
fn as_signed_mut(self) -> &'a mut [S];
1965+
}
1966+
1967+
macro_rules! impl_immut_int_slice {
1968+
($u:ty, $s:ty, $t:ty) => {
1969+
#[experimental]
1970+
impl<'a> ImmutableIntSlice<'a, $u, $s> for $t {
1971+
#[inline]
1972+
fn as_unsigned(self) -> &'a [$u] { unsafe { transmute(self) } }
1973+
#[inline]
1974+
fn as_signed(self) -> &'a [$s] { unsafe { transmute(self) } }
1975+
}
1976+
}
1977+
}
1978+
macro_rules! impl_mut_int_slice {
1979+
($u:ty, $s:ty, $t:ty) => {
1980+
#[experimental]
1981+
impl<'a> MutableIntSlice<'a, $u, $s> for $t {
1982+
#[inline]
1983+
fn as_unsigned_mut(self) -> &'a mut [$u] { unsafe { transmute(self) } }
1984+
#[inline]
1985+
fn as_signed_mut(self) -> &'a mut [$s] { unsafe { transmute(self) } }
1986+
}
1987+
}
1988+
}
1989+
1990+
macro_rules! impl_int_slice {
1991+
($u:ty, $s:ty) => {
1992+
impl_immut_int_slice!($u, $s, &'a [$u])
1993+
impl_immut_int_slice!($u, $s, &'a [$s])
1994+
impl_immut_int_slice!($u, $s, &'a mut [$u])
1995+
impl_immut_int_slice!($u, $s, &'a mut [$s])
1996+
impl_mut_int_slice!($u, $s, &'a mut [$u])
1997+
impl_mut_int_slice!($u, $s, &'a mut [$s])
1998+
}
1999+
}
2000+
2001+
impl_int_slice!(u8, i8)
2002+
impl_int_slice!(u16, i16)
2003+
impl_int_slice!(u32, i32)
2004+
impl_int_slice!(u64, i64)
2005+
impl_int_slice!(uint, int)

0 commit comments

Comments
 (0)