25
25
html_favicon_url = "http://www.rust-lang.org/favicon.ico" ,
26
26
html_root_url = "http://doc.rust-lang.org/nightly/" ) ]
27
27
28
- #![ feature( int_uint) ]
29
28
#![ feature( libc) ]
30
29
#![ feature( staged_api) ]
31
30
#![ feature( unique) ]
35
34
extern crate libc;
36
35
37
36
use libc:: { c_void, size_t, c_int} ;
37
+ use std:: fmt;
38
38
use std:: ops:: Deref ;
39
39
use std:: ptr:: Unique ;
40
40
use std:: slice;
41
41
42
+ #[ derive( Clone , Eq , Hash , Ord , PartialEq , PartialOrd ) ]
43
+ pub struct Error {
44
+ _unused : ( ) ,
45
+ }
46
+
47
+ impl Error {
48
+ fn new ( ) -> Error {
49
+ Error {
50
+ _unused : ( ) ,
51
+ }
52
+ }
53
+ }
54
+
55
+ impl fmt:: Debug for Error {
56
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
57
+ "decompression error" . fmt ( f)
58
+ }
59
+ }
60
+
42
61
pub struct Bytes {
43
62
ptr : Unique < u8 > ,
44
- len : uint ,
63
+ len : usize ,
45
64
}
46
65
47
66
impl Deref for Bytes {
@@ -78,55 +97,56 @@ const LZ_NORM: c_int = 0x80; // LZ with 128 probes, "normal"
78
97
const TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1 ; // parse zlib header and adler32 checksum
79
98
const TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000 ; // write zlib header and adler32 checksum
80
99
81
- fn deflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> Option < Bytes > {
100
+ fn deflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> Bytes {
82
101
unsafe {
83
- let mut outsz : size_t = 0 ;
102
+ let mut outsz: size_t = 0 ;
84
103
let res = tdefl_compress_mem_to_heap ( bytes. as_ptr ( ) as * const _ ,
85
104
bytes. len ( ) as size_t ,
86
105
& mut outsz,
87
106
flags) ;
88
- if !res. is_null ( ) {
89
- let res = Unique :: new ( res as * mut u8 ) ;
90
- Some ( Bytes { ptr : res, len : outsz as uint } )
91
- } else {
92
- None
107
+ assert ! ( !res. is_null( ) ) ;
108
+ Bytes {
109
+ ptr : Unique :: new ( res as * mut u8 ) ,
110
+ len : outsz as usize ,
93
111
}
94
112
}
95
113
}
96
114
97
115
/// Compress a buffer, without writing any sort of header on the output.
98
- pub fn deflate_bytes ( bytes : & [ u8 ] ) -> Option < Bytes > {
116
+ pub fn deflate_bytes ( bytes : & [ u8 ] ) -> Bytes {
99
117
deflate_bytes_internal ( bytes, LZ_NORM )
100
118
}
101
119
102
120
/// Compress a buffer, using a header that zlib can understand.
103
- pub fn deflate_bytes_zlib ( bytes : & [ u8 ] ) -> Option < Bytes > {
121
+ pub fn deflate_bytes_zlib ( bytes : & [ u8 ] ) -> Bytes {
104
122
deflate_bytes_internal ( bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER )
105
123
}
106
124
107
- fn inflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> Option < Bytes > {
125
+ fn inflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> Result < Bytes , Error > {
108
126
unsafe {
109
- let mut outsz : size_t = 0 ;
127
+ let mut outsz: size_t = 0 ;
110
128
let res = tinfl_decompress_mem_to_heap ( bytes. as_ptr ( ) as * const _ ,
111
129
bytes. len ( ) as size_t ,
112
130
& mut outsz,
113
131
flags) ;
114
132
if !res. is_null ( ) {
115
- let res = Unique :: new ( res as * mut u8 ) ;
116
- Some ( Bytes { ptr : res, len : outsz as uint } )
133
+ Ok ( Bytes {
134
+ ptr : Unique :: new ( res as * mut u8 ) ,
135
+ len : outsz as usize ,
136
+ } )
117
137
} else {
118
- None
138
+ Err ( Error :: new ( ) )
119
139
}
120
140
}
121
141
}
122
142
123
143
/// Decompress a buffer, without parsing any sort of header on the input.
124
- pub fn inflate_bytes ( bytes : & [ u8 ] ) -> Option < Bytes > {
144
+ pub fn inflate_bytes ( bytes : & [ u8 ] ) -> Result < Bytes , Error > {
125
145
inflate_bytes_internal ( bytes, 0 )
126
146
}
127
147
128
148
/// Decompress a buffer that starts with a zlib header.
129
- pub fn inflate_bytes_zlib ( bytes : & [ u8 ] ) -> Option < Bytes > {
149
+ pub fn inflate_bytes_zlib ( bytes : & [ u8 ] ) -> Result < Bytes , Error > {
130
150
inflate_bytes_internal ( bytes, TINFL_FLAG_PARSE_ZLIB_HEADER )
131
151
}
132
152
@@ -140,7 +160,7 @@ mod tests {
140
160
#[ test]
141
161
fn test_flate_round_trip ( ) {
142
162
let mut r = rand:: thread_rng ( ) ;
143
- let mut words = vec ! ( ) ;
163
+ let mut words = vec ! [ ] ;
144
164
for _ in 0 ..20 {
145
165
let range = r. gen_range ( 1 , 10 ) ;
146
166
let v = r. gen_iter :: < u8 > ( ) . take ( range) . collect :: < Vec < u8 > > ( ) ;
@@ -153,8 +173,8 @@ mod tests {
153
173
}
154
174
debug ! ( "de/inflate of {} bytes of random word-sequences" ,
155
175
input. len( ) ) ;
156
- let cmp = deflate_bytes ( & input) . expect ( "deflation failed" ) ;
157
- let out = inflate_bytes ( & cmp) . expect ( "inflation failed" ) ;
176
+ let cmp = deflate_bytes ( & input) ;
177
+ let out = inflate_bytes ( & cmp) . unwrap ( ) ;
158
178
debug ! ( "{} bytes deflated to {} ({:.1}% size)" ,
159
179
input. len( ) , cmp. len( ) ,
160
180
100.0 * ( ( cmp. len( ) as f64 ) / ( input. len( ) as f64 ) ) ) ;
@@ -164,9 +184,9 @@ mod tests {
164
184
165
185
#[ test]
166
186
fn test_zlib_flate ( ) {
167
- let bytes = vec ! ( 1 , 2 , 3 , 4 , 5 ) ;
168
- let deflated = deflate_bytes ( & bytes) . expect ( "deflation failed" ) ;
169
- let inflated = inflate_bytes ( & deflated) . expect ( "inflation failed" ) ;
187
+ let bytes = vec ! [ 1 , 2 , 3 , 4 , 5 ] ;
188
+ let deflated = deflate_bytes ( & bytes) ;
189
+ let inflated = inflate_bytes ( & deflated) . unwrap ( ) ;
170
190
assert_eq ! ( & * inflated, & * bytes) ;
171
191
}
172
192
}
0 commit comments