Add unchecked access to vector components on the Indexable trait.

This commit is contained in:
Sébastien Crozet 2013-12-01 20:17:18 +01:00
parent 15d12e5322
commit 1e71dc1d0a
4 changed files with 37 additions and 1 deletions

View File

@ -192,6 +192,16 @@ macro_rules! indexable_impl(
.swap(i1 + j1 * $dim, i2 + j2 * $dim) .swap(i1 + j1 * $dim, i2 + j2 * $dim)
} }
} }
#[inline]
unsafe fn unsafe_at(&self, (i, j): (uint, uint)) -> N {
(*cast::transmute::<&$t<N>, &[N, ..$dim * $dim]>(self).unsafe_ref(i + j * $dim)).clone()
}
#[inline]
unsafe fn unsafe_set(&mut self, (i, j): (uint, uint), val: N) {
(*cast::transmute::<&mut $t<N>, &mut [N, ..$dim * $dim]>(self).unsafe_mut_ref(i + j * $dim)) = val
}
} }
) )
) )

View File

@ -23,12 +23,19 @@ impl<N> Indexable<uint, N> for vec::Vec0<N> {
#[inline] #[inline]
fn set(&mut self, _: uint, _: N) { fn set(&mut self, _: uint, _: N) {
} }
#[inline] #[inline]
fn swap(&mut self, _: uint, _: uint) { fn swap(&mut self, _: uint, _: uint) {
}
#[inline]
unsafe fn unsafe_at(&self, _: uint) -> N {
fail!("Cannot index a Vec0.")
}
#[inline]
unsafe fn unsafe_set(&mut self, _: uint, _: N) {
} }
} }

View File

@ -144,6 +144,16 @@ macro_rules! indexable_impl(
cast::transmute::<&mut $t<N>, &mut [N, ..$dim]>(self).swap(i1, i2) cast::transmute::<&mut $t<N>, &mut [N, ..$dim]>(self).swap(i1, i2)
} }
} }
#[inline]
unsafe fn unsafe_at(&self, i: uint) -> N {
(*cast::transmute::<&$t<N>, &[N, ..$dim]>(self).unsafe_ref(i)).clone()
}
#[inline]
unsafe fn unsafe_set(&mut self, i: uint, val: N) {
(*cast::transmute::<&mut $t<N>, &mut [N, ..$dim]>(self).unsafe_mut_ref(i)) = val
}
} }
) )
) )

View File

@ -113,6 +113,15 @@ pub trait Indexable<Index, Res> {
fn set(&mut self, i: Index, Res); fn set(&mut self, i: Index, Res);
/// Swaps the `i`-th element of `self` with its `j`-th element. /// Swaps the `i`-th element of `self` with its `j`-th element.
fn swap(&mut self, i: Index, j: Index); fn swap(&mut self, i: Index, j: Index);
/// Reads the `i`-th element of `self`.
///
/// `i` is not checked.
unsafe fn unsafe_at(&self, i: Index) -> Res;
/// Writes to the `i`-th element of `self`.
///
/// `i` is not checked.
unsafe fn unsafe_set(&mut self, i: Index, Res);
} }
/// This is a workaround of current Rust limitations. /// This is a workaround of current Rust limitations.