diff options
| author | Michael Woerister <michaelwoerister@posteo.net> | 2015-12-10 14:35:53 -0500 |
|---|---|---|
| committer | Michael Woerister <michaelwoerister@posteo.net> | 2015-12-10 14:56:18 -0500 |
| commit | d62de4c066cf12d820b3dfdcf7a45400611e29a7 (patch) | |
| tree | 042a251953b69e28f33a5c53198e0fb61ff4f188 /src/librustc_data_structures/tuple_slice.rs | |
| parent | ae1cf9815f51f029c9f1b55c7cef6b9b48ea4a5f (diff) | |
| download | rust-d62de4c066cf12d820b3dfdcf7a45400611e29a7.tar.gz rust-d62de4c066cf12d820b3dfdcf7a45400611e29a7.zip | |
Add TupleSlice trait to rustc_data_structures.
Diffstat (limited to 'src/librustc_data_structures/tuple_slice.rs')
| -rw-r--r-- | src/librustc_data_structures/tuple_slice.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/librustc_data_structures/tuple_slice.rs b/src/librustc_data_structures/tuple_slice.rs new file mode 100644 index 00000000000..f157d82eda1 --- /dev/null +++ b/src/librustc_data_structures/tuple_slice.rs @@ -0,0 +1,60 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::slice; + +/// Allows to view uniform tuples as slices +pub trait TupleSlice<T> { + fn as_slice(&self) -> &[T]; + fn as_mut_slice(&mut self) -> &mut [T]; +} + +macro_rules! impl_tuple_slice { + ($tuple_type:ty, $size:expr) => { + impl<T> TupleSlice<T> for $tuple_type { + fn as_slice(&self) -> &[T] { + unsafe { + let ptr = &self.0 as *const T; + slice::from_raw_parts(ptr, $size) + } + } + + fn as_mut_slice(&mut self) -> &mut [T] { + unsafe { + let ptr = &mut self.0 as *mut T; + slice::from_raw_parts_mut(ptr, $size) + } + } + } + } +} + +impl_tuple_slice!((T,T), 2); +impl_tuple_slice!((T,T,T), 3); +impl_tuple_slice!((T,T,T,T), 4); +impl_tuple_slice!((T,T,T,T,T), 5); +impl_tuple_slice!((T,T,T,T,T,T), 6); +impl_tuple_slice!((T,T,T,T,T,T,T), 7); +impl_tuple_slice!((T,T,T,T,T,T,T,T), 8); + +#[test] +fn test_sliced_tuples() { + let t2 = (100i32, 101i32); + assert_eq!(t2.as_slice(), &[100i32, 101i32]); + + let t3 = (102i32, 103i32, 104i32); + assert_eq!(t3.as_slice(), &[102i32, 103i32, 104i32]); + + let t4 = (105i32, 106i32, 107i32, 108i32); + assert_eq!(t4.as_slice(), &[105i32, 106i32, 107i32, 108i32]); + + let t5 = (109i32, 110i32, 111i32, 112i32, 113i32); + assert_eq!(t5.as_slice(), &[109i32, 110i32, 111i32, 112i32, 113i32]); +} |
