about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJulian Orth <ju.orth@gmail.com>2014-10-03 20:34:32 +0200
committerJulian Orth <ju.orth@gmail.com>2014-10-08 20:51:31 +0200
commitbd527909e702640f7250fcf545233ca0ae00ed6e (patch)
tree518dad48c56eb5f91d41796f713a9a502819e139
parent9a2286d3a13c4a97340c99c86c718654f6cb2ed6 (diff)
downloadrust-bd527909e702640f7250fcf545233ca0ae00ed6e.tar.gz
rust-bd527909e702640f7250fcf545233ca0ae00ed6e.zip
add {Imm,M}utableIntSlice
-rw-r--r--src/libcore/slice.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs
index 9f925f9d371..7090f89d1dc 100644
--- a/src/libcore/slice.rs
+++ b/src/libcore/slice.rs
@@ -1853,3 +1853,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)