Debian Codegraph @

src/lz4f/stream/comp/write.rs

raw symbols SCIP
1 use super::{Compressor, Dictionary, Preferences};
2 use crate::lz4f::Result;
3 use std::{fmt, io::Write};
4
5 /// The [`Write`]-based streaming compressor.
6 ///
7 /// # Example
8 ///
9 /// ```
10 /// # use std::env;
11 /// # use std::path::Path;
12 /// # use lzzzz::{Error, Result};
13 /// # use assert_fs::prelude::*;
14 /// # let tmp_dir = assert_fs::TempDir::new().unwrap().into_persistent();
15 /// # env::set_current_dir(tmp_dir.path()).unwrap();
16 /// use lzzzz::lz4f::WriteCompressor;
17 /// use std::{fs::File, io::prelude::*};
18 ///
19 /// let mut f = File::create("foo.lz4")?;
20 /// let mut w = WriteCompressor::new(&mut f, Default::default())?;
21 ///
22 /// w.write_all(b"Hello world!")?;
23 /// # Ok::<(), std::io::Error>(())
24 /// ```
25 ///
26 /// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
27 pub struct WriteCompressor<W: Write> {
28 inner: Option<W>,
29 comp: Compressor,
30 }
31
32 impl<W: Write> WriteCompressor<W> {
33 /// Creates a new `WriteCompressor<W>`.
34 pub fn new(writer: W, prefs: Preferences) -> Result<Self> {
35 Ok(Self {
36 inner: Some(writer),
37 comp: Compressor::new(prefs, None)?,
38 })
39 }
40
41 /// Creates a new `WriteCompressor<W>` with a dictionary.
42 pub fn with_dict(writer: W, prefs: Preferences, dict: Dictionary) -> Result<Self> {
43 Ok(Self {
44 inner: Some(writer),
45 comp: Compressor::new(prefs, Some(dict))?,
46 })
47 }
48
49 /// Returns a mutable reference to the writer.
50 pub fn get_mut(&mut self) -> &mut W {
51 self.inner.as_mut().unwrap()
52 }
53
54 /// Returns a shared reference to the writer.
55 pub fn get_ref(&self) -> &W {
56 self.inner.as_ref().unwrap()
57 }
58
59 /// Returns the ownership of the writer, finishing the stream in the process.
60 pub fn into_inner(mut self) -> W {
61 let _ = self.end();
62 self.inner.take().unwrap()
63 }
64
65 fn end(&mut self) -> std::io::Result<()> {
66 if let Some(device) = &mut self.inner {
67 self.comp.end(false)?;
68 device.write_all(self.comp.buf())?;
69 self.comp.clear_buf();
70 device.flush()?;
71 }
72
73 Ok(())
74 }
75 }
76
77 impl<W> fmt::Debug for WriteCompressor<W>
78 where
79 W: Write + fmt::Debug,
80 {
81 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
82 fmt.debug_struct("WriteCompressor")
83 .field("writer", &self.inner)
84 .field("prefs", &self.comp.prefs())
85 .finish()
86 }
87 }
88
89 impl<W: Write> Write for WriteCompressor<W> {
90 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
91 self.comp.update(buf, false)?;
92 self.inner.as_mut().unwrap().write_all(self.comp.buf())?;
93 self.comp.clear_buf();
94 Ok(buf.len())
95 }
96
97 fn flush(&mut self) -> std::io::Result<()> {
98 self.comp.flush(false)?;
99 self.inner.as_mut().unwrap().write_all(self.comp.buf())?;
100 self.comp.clear_buf();
101 self.inner.as_mut().unwrap().flush()
102 }
103 }
104
105 impl<W: Write> Drop for WriteCompressor<W> {
106 fn drop(&mut self) {
107 let _ = self.end();
108 }
109 }