diff options
author | metamuffin <metamuffin@disroot.org> | 2022-12-06 17:45:26 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-12-06 17:45:26 +0100 |
commit | e3c742ff04a665c70c029f266aa0fe72e12ac72c (patch) | |
tree | e6a262a223d151afd804359e243cc5d0301e732f /evc/src/vec2.rs | |
parent | 849c3769fbd38940c9bfa73bcea160848a38d9b6 (diff) | |
download | video-codec-experiments-e3c742ff04a665c70c029f266aa0fe72e12ac72c.tar video-codec-experiments-e3c742ff04a665c70c029f266aa0fe72e12ac72c.tar.bz2 video-codec-experiments-e3c742ff04a665c70c029f266aa0fe72e12ac72c.tar.zst |
vec2 everywhere
Diffstat (limited to 'evc/src/vec2.rs')
-rw-r--r-- | evc/src/vec2.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/evc/src/vec2.rs b/evc/src/vec2.rs new file mode 100644 index 0000000..ffee124 --- /dev/null +++ b/evc/src/vec2.rs @@ -0,0 +1,59 @@ +use crate::ser::{Ser, Sink, Source}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Vec2 { + pub x: isize, + pub y: isize, +} +impl Vec2 { + pub const ZERO: Vec2 = Vec2 { x: 0, y: 0 }; +} + +impl Ser for Vec2 { + fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { + sink.put((self.x, self.y)) + } + + fn read(source: &mut impl std::io::Read) -> anyhow::Result<Self> { + let (x, y) = source.get()?; + Ok(Vec2 { x, y }) + } +} + +impl std::ops::Add for Vec2 { + type Output = Vec2; + #[inline] + fn add(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x + rhs.x, + y: self.y + rhs.y, + } + } +} +impl std::ops::Sub for Vec2 { + type Output = Vec2; + #[inline] + fn sub(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x - rhs.x, + y: self.y - rhs.y, + } + } +} +impl std::ops::Mul for Vec2 { + type Output = Vec2; + #[inline] + fn mul(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x * rhs.x, + y: self.y * rhs.y, + } + } +} + +impl From<(isize, isize)> for Vec2 { + #[inline] + fn from((x, y): (isize, isize)) -> Self { + Vec2 { x, y } + } +} |