(index<- ) ./libstd/rt/io/flate.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Some various other I/O types
12
13 // FIXME(#3660): should move to libextra
14
15 use prelude::*;
16 use super::*;
17
18 /// A Writer decorator that compresses using the 'deflate' scheme
19 pub struct DeflateWriter<W> {
20 inner_writer: W
21 }
22
23 impl<W: Writer> DeflateWriter<W> {
24 pub fn new(inner_writer: W) -> DeflateWriter<W> {
25 DeflateWriter {
26 inner_writer: inner_writer
27 }
28 }
29 }
30
31 impl<W: Writer> Writer for DeflateWriter<W> {
32 fn write(&mut self, _buf: &[u8]) { fail2!() }
33
34 fn flush(&mut self) { fail2!() }
35 }
36
37 impl<W: Writer> Decorator<W> for DeflateWriter<W> {
38 fn inner(self) -> W {
39 match self {
40 DeflateWriter { inner_writer: w } => w
41 }
42 }
43
44 fn inner_ref<'a>(&'a self) -> &'a W {
45 match *self {
46 DeflateWriter { inner_writer: ref w } => w
47 }
48 }
49
50 fn inner_mut_ref<'a>(&'a mut self) -> &'a mut W {
51 match *self {
52 DeflateWriter { inner_writer: ref mut w } => w
53 }
54 }
55 }
56
57 /// A Reader decorator that decompresses using the 'deflate' scheme
58 pub struct InflateReader<R> {
59 inner_reader: R
60 }
61
62 impl<R: Reader> InflateReader<R> {
63 pub fn new(inner_reader: R) -> InflateReader<R> {
64 InflateReader {
65 inner_reader: inner_reader
66 }
67 }
68 }
69
70 impl<R: Reader> Reader for InflateReader<R> {
71 fn read(&mut self, _buf: &mut [u8]) -> Option<uint> { fail2!() }
72
73 fn eof(&mut self) -> bool { fail2!() }
74 }
75
76 impl<R: Reader> Decorator<R> for InflateReader<R> {
77 fn inner(self) -> R {
78 match self {
79 InflateReader { inner_reader: r } => r
80 }
81 }
82
83 fn inner_ref<'a>(&'a self) -> &'a R {
84 match *self {
85 InflateReader { inner_reader: ref r } => r
86 }
87 }
88
89 fn inner_mut_ref<'a>(&'a mut self) -> &'a mut R {
90 match *self {
91 InflateReader { inner_reader: ref mut r } => r
92 }
93 }
94 }
95
96 #[cfg(test)]
97 mod test {
98 use prelude::*;
99 use super::*;
100 use super::super::mem::*;
101 use super::super::Decorator;
102
103 use str;
104
105 #[test]
106 #[ignore]
107 fn smoke_test() {
108 let mem_writer = MemWriter::new();
109 let mut deflate_writer = DeflateWriter::new(mem_writer);
110 let in_msg = "test";
111 let in_bytes = in_msg.as_bytes();
112 deflate_writer.write(in_bytes);
113 deflate_writer.flush();
114 let buf = deflate_writer.inner().inner();
115 let mem_reader = MemReader::new(buf);
116 let mut inflate_reader = InflateReader::new(mem_reader);
117 let mut out_bytes = [0, .. 100];
118 let bytes_read = inflate_reader.read(out_bytes).unwrap();
119 assert_eq!(bytes_read, in_bytes.len());
120 let out_msg = str::from_utf8(out_bytes);
121 assert!(in_msg == out_msg);
122 }
123 }