grid: impl FromStr for Grid<u8>
This commit is contained in:
parent
3bbf05b30c
commit
a1774d1f73
@ -1,9 +1,10 @@
|
|||||||
use std::{
|
use std::{
|
||||||
fmt::{Debug, Display, Formatter, Write},
|
fmt::{Debug, Display, Formatter, Write},
|
||||||
io::BufRead,
|
io::{BufRead, Cursor},
|
||||||
iter::repeat,
|
iter::repeat,
|
||||||
mem::swap,
|
mem::swap,
|
||||||
ops::{Add, Sub},
|
ops::{Add, Sub},
|
||||||
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
@ -107,7 +108,7 @@ impl AsCoord2d for (u64, u64) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Eq, PartialEq)]
|
||||||
pub struct Grid<T> {
|
pub struct Grid<T> {
|
||||||
pub data: Vec<T>,
|
pub data: Vec<T>,
|
||||||
width: i64,
|
width: i64,
|
||||||
@ -245,6 +246,14 @@ impl<T: BufRead> From<T> for Grid<u8> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Should be Grid<char>?
|
||||||
|
impl FromStr for Grid<u8> {
|
||||||
|
type Err = Box<dyn std::error::Error>;
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
Ok(Cursor::new(s).into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// impl<T: Copy + Eq + PartialEq + Display + Debug + Into<char>> Display for Grid<T> {
|
// impl<T: Copy + Eq + PartialEq + Display + Debug + Into<char>> Display for Grid<T> {
|
||||||
// fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
// fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
// for y in 0..self.height() {
|
// for y in 0..self.height() {
|
||||||
@ -276,6 +285,11 @@ mod tests {
|
|||||||
static TEST_VECTOR: &[u8] = b"ABCD
|
static TEST_VECTOR: &[u8] = b"ABCD
|
||||||
EFGH
|
EFGH
|
||||||
IJKL
|
IJKL
|
||||||
|
FBCG";
|
||||||
|
|
||||||
|
static TEST_VECTOR_S: &str = "ABCD
|
||||||
|
EFGH
|
||||||
|
IJKL
|
||||||
FBCG";
|
FBCG";
|
||||||
|
|
||||||
fn unchecked_load() -> Grid<u8> {
|
fn unchecked_load() -> Grid<u8> {
|
||||||
@ -286,6 +300,7 @@ FBCG";
|
|||||||
fn from_string() {
|
fn from_string() {
|
||||||
let grid = unchecked_load();
|
let grid = unchecked_load();
|
||||||
assert_eq!(grid.data, "ABCDEFGHIJKLFBCG".as_bytes());
|
assert_eq!(grid.data, "ABCDEFGHIJKLFBCG".as_bytes());
|
||||||
|
assert_eq!(TEST_VECTOR_S.parse::<Grid<u8>>().unwrap().data, "ABCDEFGHIJKLFBCG".as_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user