2015-07-12 11:32:32 +00:00
|
|
|
use std::ops::Index;
|
|
|
|
|
2020-06-15 04:36:25 +00:00
|
|
|
pub trait Array2D: Index<usize> + Sized {
|
2015-07-12 11:32:32 +00:00
|
|
|
fn rows(&self) -> usize;
|
|
|
|
fn columns(&self) -> usize;
|
|
|
|
fn get<'a>(&'a self, y: usize, x: usize) -> Option<&'a <Self as Index<usize>>::Output> {
|
|
|
|
if y >= self.rows() || x >= self.columns() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let i = y * self.columns() + x;
|
2019-05-28 18:46:13 +00:00
|
|
|
let indexer = &(*self as &dyn Index<usize, Output = <Self as Index<usize>>::Output>);
|
2017-06-09 20:04:29 +00:00
|
|
|
//~^ERROR non-primitive cast
|
2015-07-12 11:32:32 +00:00
|
|
|
Some(indexer.index(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|