mirror of
https://github.com/rust-lang/rust
synced 2024-11-05 20:45:15 +00:00
17 lines
530 B
Rust
17 lines
530 B
Rust
use std::ops::Index;
|
|
|
|
pub trait Array2D: Index<usize> + Sized {
|
|
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;
|
|
let indexer = &(*self as &dyn Index<usize, Output = <Self as Index<usize>>::Output>);
|
|
//~^ERROR non-primitive cast
|
|
Some(indexer.index(i))
|
|
}
|
|
}
|
|
|
|
fn main() {}
|