From 849c3769fbd38940c9bfa73bcea160848a38d9b6 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 6 Dec 2022 16:45:30 +0100 Subject: simplify block type --- evc/src/block.rs | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) (limited to 'evc/src/block.rs') diff --git a/evc/src/block.rs b/evc/src/block.rs index dae538c..8daba5c 100644 --- a/evc/src/block.rs +++ b/evc/src/block.rs @@ -6,13 +6,7 @@ use crate::{ }; #[derive(Clone, Debug)] -pub struct Block { - pub size: (usize, usize), - pub inner: BlockInner, -} - -#[derive(Clone, Debug)] -pub enum BlockInner { +pub enum Block { Literal(Vec), Split(Box<[Block; 2]>), Reference { translation: (usize, usize) }, @@ -20,17 +14,17 @@ pub enum BlockInner { impl Block { pub fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { - match &self.inner { - BlockInner::Literal(pixels) => { + match &self { + Block::Literal(pixels) => { sink.put(0u8)?; pixels.write(sink)?; } - BlockInner::Split(box [a, b]) => { + Block::Split(box [a, b]) => { sink.put(1u8)?; a.write(sink)?; b.write(sink)?; } - BlockInner::Reference { translation: _ } => { + Block::Reference { translation: _ } => { sink.put(2u8)?; // sink.put(*translation)?; } @@ -39,9 +33,9 @@ impl Block { } pub fn read(source: &mut impl std::io::Read, size: (usize, usize)) -> anyhow::Result { - let inner = match source.get::()? { - 0 => BlockInner::Literal(source.get()?), - 1 => BlockInner::Split(Box::new({ + Ok(match source.get::()? { + 0 => Block::Literal(source.get()?), + 1 => Block::Split(Box::new({ let vert = size.0 > size.1; let asize = if vert { (size.0 / 2, size.1) @@ -58,18 +52,16 @@ impl Block { let b = Block::read(source, bsize)?; [a, b] })), - 2 => BlockInner::Reference { + 2 => Block::Reference { translation: (0, 0), //source.get()?, }, x => bail!("corrupt block type ({})", x), - }; - - Ok(Self { size, inner }) + }) } } impl Block { pub fn is_literal(&self) -> bool { - matches!(self.inner, BlockInner::Literal(..)) + matches!(self, Block::Literal(..)) } } -- cgit v1.2.3-70-g09d2