diff options
| author | bors <bors@rust-lang.org> | 2014-10-08 21:22:32 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-10-08 21:22:32 +0000 |
| commit | 218cb4bc9968277fb90e9ad5c2eee03e1f36724d (patch) | |
| tree | acf630e46c68a06ac082680a0eace46dcabd606a /src | |
| parent | afb5fcd553cf2cca1e28a632eb8100ae3f21821d (diff) | |
| parent | bd527909e702640f7250fcf545233ca0ae00ed6e (diff) | |
| download | rust-218cb4bc9968277fb90e9ad5c2eee03e1f36724d.tar.gz rust-218cb4bc9968277fb90e9ad5c2eee03e1f36724d.zip | |
auto merge of #17748 : mahkoh/rust/int_slice, r=aturon
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/slice.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 7246fc367f8..02c0c9bf310 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -1945,3 +1945,61 @@ impl<'a, T: PartialOrd> PartialOrd for &'a [T] { order::gt(self.iter(), other.iter()) } } + +/// Extension methods for immutable slices containing integers. +#[experimental] +pub trait ImmutableIntSlice<'a, U, S> { + /// Converts the slice to an immutable slice of unsigned integers with the same width. + fn as_unsigned(self) -> &'a [U]; + /// Converts the slice to an immutable slice of signed integers with the same width. + fn as_signed(self) -> &'a [S]; +} + +/// Extension methods for mutable slices containing integers. +#[experimental] +pub trait MutableIntSlice<'a, U, S>: ImmutableIntSlice<'a, U, S> { + /// Converts the slice to a mutable slice of unsigned integers with the same width. + fn as_unsigned_mut(self) -> &'a mut [U]; + /// Converts the slice to a mutable slice of signed integers with the same width. + fn as_signed_mut(self) -> &'a mut [S]; +} + +macro_rules! impl_immut_int_slice { + ($u:ty, $s:ty, $t:ty) => { + #[experimental] + impl<'a> ImmutableIntSlice<'a, $u, $s> for $t { + #[inline] + fn as_unsigned(self) -> &'a [$u] { unsafe { transmute(self) } } + #[inline] + fn as_signed(self) -> &'a [$s] { unsafe { transmute(self) } } + } + } +} +macro_rules! impl_mut_int_slice { + ($u:ty, $s:ty, $t:ty) => { + #[experimental] + impl<'a> MutableIntSlice<'a, $u, $s> for $t { + #[inline] + fn as_unsigned_mut(self) -> &'a mut [$u] { unsafe { transmute(self) } } + #[inline] + fn as_signed_mut(self) -> &'a mut [$s] { unsafe { transmute(self) } } + } + } +} + +macro_rules! impl_int_slice { + ($u:ty, $s:ty) => { + impl_immut_int_slice!($u, $s, &'a [$u]) + impl_immut_int_slice!($u, $s, &'a [$s]) + impl_immut_int_slice!($u, $s, &'a mut [$u]) + impl_immut_int_slice!($u, $s, &'a mut [$s]) + impl_mut_int_slice!($u, $s, &'a mut [$u]) + impl_mut_int_slice!($u, $s, &'a mut [$s]) + } +} + +impl_int_slice!(u8, i8) +impl_int_slice!(u16, i16) +impl_int_slice!(u32, i32) +impl_int_slice!(u64, i64) +impl_int_slice!(uint, int) |
