use super::{fmt::Default, read_from::ReadFromError};
use crate::{unwrap, BufReadExt, Mat, ReadFrom};
pub trait ReadInto<T>: BufReadExt {
type Error: std::error::Error;
fn try_read(&mut self) -> Result<T, Self::Error>;
fn try_read_n(&mut self, n: usize) -> Result<Vec<T>, Self::Error>;
fn try_read_m_n(&mut self, m: usize, n: usize) -> Result<Mat<T>, Self::Error>;
#[inline]
#[track_caller]
fn read(&mut self) -> T {
unwrap!(self.try_read())
}
#[inline]
#[track_caller]
fn read_n(&mut self, n: usize) -> Vec<T> {
unwrap!(self.try_read_n(n))
}
#[inline]
#[track_caller]
fn read_m_n(&mut self, m: usize, n: usize) -> Mat<T> {
unwrap!(self.try_read_m_n(m, n))
}
}
impl<T: BufReadExt, U> ReadInto<U> for T
where
U: ReadFrom,
{
type Error = ReadFromError<U>;
#[inline]
fn try_read(&mut self) -> Result<U, Self::Error> {
U::try_read_from(self, Default::new())
}
#[inline]
fn try_read_n(&mut self, n: usize) -> Result<Vec<U>, Self::Error> {
U::try_read_n_from(self, n, Default::new())
}
#[inline]
fn try_read_m_n(&mut self, m: usize, n: usize) -> Result<Mat<U>, Self::Error> {
U::try_read_m_n_from(self, m, n, Default::new())
}
}