use std::io::{Read, Result as iRes};
pub trait IsByteReader {
fn read_i8<R>(read: &mut R) -> iRes<i8>
where
R: Read;
fn read_u8<R>(read: &mut R) -> iRes<u8>
where
R: Read;
fn read_i16<R>(read: &mut R) -> iRes<i16>
where
R: Read;
fn read_u16<R>(read: &mut R) -> iRes<u16>
where
R: Read;
fn read_i32<R>(read: &mut R) -> iRes<i32>
where
R: Read;
fn read_u32<R>(read: &mut R) -> iRes<u32>
where
R: Read;
fn read_f32<R>(read: &mut R) -> iRes<f32>
where
R: Read;
fn read_i64<R>(read: &mut R) -> iRes<i64>
where
R: Read;
fn read_u64<R>(read: &mut R) -> iRes<u64>
where
R: Read;
fn read_f64<R>(read: &mut R) -> iRes<f64>
where
R: Read;
}
pub struct LittleReader {}
impl IsByteReader for LittleReader {
#[inline(always)]
fn read_i8<R>(read: &mut R) -> iRes<i8>
where
R: Read,
{
let mut buffer = [0u8; 1];
read.read_exact(&mut buffer)?;
Ok(i8::from_le_bytes(buffer))
}
#[inline(always)]
fn read_u8<R>(read: &mut R) -> iRes<u8>
where
R: Read,
{
let mut buffer = [0u8; 1];
read.read_exact(&mut buffer)?;
Ok(u8::from_le_bytes(buffer))
}
#[inline(always)]
fn read_i16<R>(read: &mut R) -> iRes<i16>
where
R: Read,
{
let mut buffer = [0u8; 2];
read.read_exact(&mut buffer)?;
Ok(i16::from_le_bytes(buffer))
}
#[inline(always)]
fn read_u16<R>(read: &mut R) -> iRes<u16>
where
R: Read,
{
let mut buffer = [0u8; 2];
read.read_exact(&mut buffer)?;
Ok(u16::from_le_bytes(buffer))
}
#[inline(always)]
fn read_i32<R>(read: &mut R) -> iRes<i32>
where
R: Read,
{
let mut buffer = [0u8; 4];
read.read_exact(&mut buffer)?;
Ok(i32::from_le_bytes(buffer))
}
#[inline(always)]
fn read_u32<R>(read: &mut R) -> iRes<u32>
where
R: Read,
{
let mut buffer = [0u8; 4];
read.read_exact(&mut buffer)?;
Ok(u32::from_le_bytes(buffer))
}
#[inline(always)]
fn read_f32<R>(read: &mut R) -> iRes<f32>
where
R: Read,
{
let mut buffer = [0u8; 4];
read.read_exact(&mut buffer)?;
Ok(f32::from_le_bytes(buffer))
}
#[inline(always)]
fn read_i64<R>(read: &mut R) -> iRes<i64>
where
R: Read,
{
let mut buffer = [0u8; 8];
read.read_exact(&mut buffer)?;
Ok(i64::from_le_bytes(buffer))
}
#[inline(always)]
fn read_u64<R>(read: &mut R) -> iRes<u64>
where
R: Read,
{
let mut buffer = [0u8; 8];
read.read_exact(&mut buffer)?;
Ok(u64::from_le_bytes(buffer))
}
#[inline(always)]
fn read_f64<R>(read: &mut R) -> iRes<f64>
where
R: Read,
{
let mut buffer = [0u8; 8];
read.read_exact(&mut buffer)?;
Ok(f64::from_le_bytes(buffer))
}
}
pub struct BigReader {}
impl IsByteReader for BigReader {
#[inline(always)]
fn read_i8<R>(read: &mut R) -> iRes<i8>
where
R: Read,
{
let mut buffer = [0u8; 1];
read.read_exact(&mut buffer)?;
Ok(i8::from_be_bytes(buffer))
}
#[inline(always)]
fn read_u8<R>(read: &mut R) -> iRes<u8>
where
R: Read,
{
let mut buffer = [0u8; 1];
read.read_exact(&mut buffer)?;
Ok(u8::from_be_bytes(buffer))
}
#[inline(always)]
fn read_i16<R>(read: &mut R) -> iRes<i16>
where
R: Read,
{
let mut buffer = [0u8; 2];
read.read_exact(&mut buffer)?;
Ok(i16::from_be_bytes(buffer))
}
#[inline(always)]
fn read_u16<R>(read: &mut R) -> iRes<u16>
where
R: Read,
{
let mut buffer = [0u8; 2];
read.read_exact(&mut buffer)?;
Ok(u16::from_be_bytes(buffer))
}
#[inline(always)]
fn read_i32<R>(read: &mut R) -> iRes<i32>
where
R: Read,
{
let mut buffer = [0u8; 4];
read.read_exact(&mut buffer)?;
Ok(i32::from_be_bytes(buffer))
}
#[inline(always)]
fn read_u32<R>(read: &mut R) -> iRes<u32>
where
R: Read,
{
let mut buffer = [0u8; 4];
read.read_exact(&mut buffer)?;
Ok(u32::from_be_bytes(buffer))
}
#[inline(always)]
fn read_f32<R>(read: &mut R) -> iRes<f32>
where
R: Read,
{
let mut buffer = [0u8; 4];
read.read_exact(&mut buffer)?;
Ok(f32::from_be_bytes(buffer))
}
#[inline(always)]
fn read_i64<R>(read: &mut R) -> iRes<i64>
where
R: Read,
{
let mut buffer = [0u8; 8];
read.read_exact(&mut buffer)?;
Ok(i64::from_be_bytes(buffer))
}
#[inline(always)]
fn read_u64<R>(read: &mut R) -> iRes<u64>
where
R: Read,
{
let mut buffer = [0u8; 8];
read.read_exact(&mut buffer)?;
Ok(u64::from_be_bytes(buffer))
}
#[inline(always)]
fn read_f64<R>(read: &mut R) -> iRes<f64>
where
R: Read,
{
let mut buffer = [0u8; 8];
read.read_exact(&mut buffer)?;
Ok(f64::from_be_bytes(buffer))
}
}