vector2d: refactor indexing
This commit is contained in:
parent
37a4fca618
commit
b02e7207ac
1 changed files with 17 additions and 5 deletions
|
@ -1,7 +1,7 @@
|
|||
use std::cmp::Eq;
|
||||
use std::fmt::Debug;
|
||||
use std::hash::Hash;
|
||||
use std::ops::{Add, Mul, Sub};
|
||||
use std::ops::{Add, Index, IndexMut, Mul, Sub};
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct Vector2D<T> {
|
||||
|
@ -23,16 +23,28 @@ impl<T> Vector2D<T> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn index<'a, T, U>(v: &'a [Vec<U>], idx: &Vector2D<T>) -> &'a U
|
||||
pub fn index<'a, C, I>(v: &'a [C], idx: &Vector2D<I>) -> &'a C::Output
|
||||
where
|
||||
usize: TryFrom<T>,
|
||||
<usize as TryFrom<T>>::Error: Debug,
|
||||
T: Copy,
|
||||
I: Copy,
|
||||
C: Index<usize>,
|
||||
usize: TryFrom<I>,
|
||||
<usize as TryFrom<I>>::Error: Debug,
|
||||
{
|
||||
let (x, y): (usize, usize) = (idx.x.try_into().unwrap(), idx.y.try_into().unwrap());
|
||||
&v[y][x]
|
||||
}
|
||||
|
||||
pub fn index_mut<'a, C, I>(v: &'a mut [C], idx: &Vector2D<I>) -> &'a mut C::Output
|
||||
where
|
||||
I: Copy,
|
||||
C: IndexMut<usize>,
|
||||
usize: TryFrom<I>,
|
||||
<usize as TryFrom<I>>::Error: Debug,
|
||||
{
|
||||
let (x, y): (usize, usize) = (idx.x.try_into().unwrap(), idx.y.try_into().unwrap());
|
||||
&mut v[y][x]
|
||||
}
|
||||
|
||||
pub fn in_range<T, U>(v: &[Vec<U>], idx: &Vector2D<T>) -> bool
|
||||
where
|
||||
usize: TryInto<T>,
|
||||
|
|
Loading…
Reference in a new issue