mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 16:24:46 +00:00
18 lines
530 B
Rust
18 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() {}
|