pub struct Encoder<W: BitWriter> { /* private fields */ }
Expand description
LZW encoder using the algorithm of GIF files.
Implementations§
Source§impl<W: BitWriter> Encoder<W>
impl<W: BitWriter> Encoder<W>
Sourcepub fn new(w: W, min_code_size: u8) -> Result<Encoder<W>>
pub fn new(w: W, min_code_size: u8) -> Result<Encoder<W>>
Creates a new LZW encoder.
Note: If min_code_size < 8
then Self::encode_bytes
might panic when
the supplied data containts values that exceed 1 << min_code_size
.
Examples found in repository?
examples/lzw-compress.rs (line 10)
7fn main() {
8 match (|| -> io::Result<()> {
9 let mut encoder = try!(
10 lzw::Encoder::new(lzw::LsbWriter::new(io::stdout()), 8)
11 );
12 let stdin = io::stdin();
13 let mut stdin = stdin.lock();
14 loop {
15 let len = {
16 let buf = try!(stdin.fill_buf());
17 try!(encoder.encode_bytes(buf));
18 buf.len()
19 };
20 if len == 0 {
21 break
22 }
23 stdin.consume(len);
24 }
25 Ok(())
26 })() {
27 Ok(()) => (),
28 Err(err) => { let _ = write!(io::stderr(), "{}", err); }
29 }
30
31}
Sourcepub fn encode_bytes(&mut self, bytes: &[u8]) -> Result<()>
pub fn encode_bytes(&mut self, bytes: &[u8]) -> Result<()>
Compresses bytes
and writes the result into the writer.
§Panics
This function might panic if any of the input bytes exceeds 1 << min_code_size
.
This cannot happen if min_code_size >= 8
.
Examples found in repository?
examples/lzw-compress.rs (line 17)
7fn main() {
8 match (|| -> io::Result<()> {
9 let mut encoder = try!(
10 lzw::Encoder::new(lzw::LsbWriter::new(io::stdout()), 8)
11 );
12 let stdin = io::stdin();
13 let mut stdin = stdin.lock();
14 loop {
15 let len = {
16 let buf = try!(stdin.fill_buf());
17 try!(encoder.encode_bytes(buf));
18 buf.len()
19 };
20 if len == 0 {
21 break
22 }
23 stdin.consume(len);
24 }
25 Ok(())
26 })() {
27 Ok(()) => (),
28 Err(err) => { let _ = write!(io::stderr(), "{}", err); }
29 }
30
31}
Trait Implementations§
Auto Trait Implementations§
impl<W> Freeze for Encoder<W>where
W: Freeze,
impl<W> RefUnwindSafe for Encoder<W>where
W: RefUnwindSafe,
impl<W> Send for Encoder<W>where
W: Send,
impl<W> Sync for Encoder<W>where
W: Sync,
impl<W> Unpin for Encoder<W>where
W: Unpin,
impl<W> UnwindSafe for Encoder<W>where
W: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more