diff options
author | metamuffin <metamuffin@disroot.org> | 2023-01-28 10:55:10 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-01-28 10:55:10 +0100 |
commit | 0427a45ce8fa4762b087eeaf7e24f00678ceb48b (patch) | |
tree | 3f55f4bf994dbff980d930f51ac201beb22aa5e3 /matroska/src | |
parent | a742f7dbd8bda0bf23a6d5273e5dd2f83b9d4c9f (diff) | |
download | jellything-0427a45ce8fa4762b087eeaf7e24f00678ceb48b.tar jellything-0427a45ce8fa4762b087eeaf7e24f00678ceb48b.tar.bz2 jellything-0427a45ce8fa4762b087eeaf7e24f00678ceb48b.tar.zst |
seeking logic
Diffstat (limited to 'matroska/src')
-rw-r--r-- | matroska/src/write.rs | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/matroska/src/write.rs b/matroska/src/write.rs index 6f8aad4..2555380 100644 --- a/matroska/src/write.rs +++ b/matroska/src/write.rs @@ -5,19 +5,16 @@ */ use crate::{matroska::MatroskaTag, size::EbmlSize, Master}; use anyhow::{bail, Result}; -use std::io::Write; +use std::io::{Seek, Write}; -pub struct EbmlWriter { - inner: Box<dyn Write>, +pub struct EbmlWriter<W> { + inner: W, position: usize, } -impl EbmlWriter { - pub fn new<T: Write + 'static>(inner: T, position: usize) -> Self { - Self { - inner: Box::new(inner), - position, - } +impl<W: Write> EbmlWriter<W> { + pub fn new(inner: W, position: usize) -> Self { + Self { inner, position } } pub fn write(&mut self, data: &[u8]) -> Result<()> { @@ -66,18 +63,9 @@ impl EbmlWriter { if i > (1 << 56) - 1 { bail!("vint does not fit"); } - self.write_vint_len(i, Self::vint_length(i)) - } - pub fn vint_length(v: u64) -> usize { - let mut len = 1; - while len <= 8 { - if v < (1 << ((7 * len) - 1)) { - break; - } - len += 1; - } - len + self.write_vint_len(i, vint_length(i)) } + pub fn write_vint_len(&mut self, i: u64, len: usize) -> Result<()> { let mut bytes = i.to_be_bytes(); let trunc = &mut bytes[(8 - len)..]; @@ -86,6 +74,12 @@ impl EbmlWriter { } } +impl<W: Seek> Seek for EbmlWriter<W> { + fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> { + self.inner.seek(pos) + } +} + impl MatroskaTag { pub fn write_full(&self, w: &mut Vec<u8>) -> Result<()> { let mut buf = vec![]; @@ -97,6 +91,17 @@ impl MatroskaTag { } } +pub fn vint_length(v: u64) -> usize { + let mut len = 1; + while len <= 8 { + if v < (1 << ((7 * len) - 1)) { + break; + } + len += 1; + } + len +} + pub trait WriteValue { fn write_to(&self, w: &mut Vec<u8>) -> Result<()>; } |