diff --git a/Cargo.toml b/Cargo.toml index dff93da4..77a958de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ keywords = ["zip", "archive"] description = """ Library to support the reading and writing of zip files. """ +edition = "2018" [dependencies] flate2 = { version = "1.0", default-features = false, optional = true } diff --git a/benches/read_entry.rs b/benches/read_entry.rs index 104e97c1..7b345240 100644 --- a/benches/read_entry.rs +++ b/benches/read_entry.rs @@ -1,7 +1,4 @@ -#[macro_use] -extern crate bencher; -extern crate rand; -extern crate zip; +use bencher::{benchmark_group, benchmark_main}; use std::io::{Cursor, Read, Write}; diff --git a/examples/extract.rs b/examples/extract.rs index 3d3a0939..1ec429d3 100644 --- a/examples/extract.rs +++ b/examples/extract.rs @@ -1,5 +1,3 @@ -extern crate zip; - use std::io; use std::fs; diff --git a/examples/extract_lorem.rs b/examples/extract_lorem.rs index eb49bbe3..207fd530 100644 --- a/examples/extract_lorem.rs +++ b/examples/extract_lorem.rs @@ -1,7 +1,5 @@ use std::io::prelude::*; -extern crate zip; - fn main() { std::process::exit(real_main()); diff --git a/examples/file_info.rs b/examples/file_info.rs index 63f7d87e..af7abdd4 100644 --- a/examples/file_info.rs +++ b/examples/file_info.rs @@ -1,5 +1,3 @@ -extern crate zip; - use std::fs; use std::io::BufReader; diff --git a/examples/stdin_info.rs b/examples/stdin_info.rs index 910ba8e8..8d3f7c43 100644 --- a/examples/stdin_info.rs +++ b/examples/stdin_info.rs @@ -1,5 +1,3 @@ -extern crate zip; - use std::io::{self, Read}; fn main() { diff --git a/examples/write_dir.rs b/examples/write_dir.rs index fb36ccaf..6b2bac0a 100644 --- a/examples/write_dir.rs +++ b/examples/write_dir.rs @@ -1,5 +1,3 @@ -extern crate zip; -extern crate walkdir; use std::io::prelude::*; use std::io::{Write, Seek}; @@ -48,7 +46,7 @@ fn real_main() -> i32 { return 0; } -fn zip_dir(it: &mut Iterator, prefix: &str, writer: T, method: zip::CompressionMethod) +fn zip_dir(it: &mut dyn Iterator, prefix: &str, writer: T, method: zip::CompressionMethod) -> zip::result::ZipResult<()> where T: Write+Seek { diff --git a/examples/write_sample.rs b/examples/write_sample.rs index 6ff12f0b..ab0ef1a2 100644 --- a/examples/write_sample.rs +++ b/examples/write_sample.rs @@ -1,8 +1,6 @@ use std::io::prelude::*; use zip::write::FileOptions; -extern crate zip; - fn main() { std::process::exit(real_main()); diff --git a/src/compression.rs b/src/compression.rs index 9585ef3b..3ec9dbf3 100644 --- a/src/compression.rs +++ b/src/compression.rs @@ -45,7 +45,7 @@ impl CompressionMethod { } impl fmt::Display for CompressionMethod { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Just duplicate what the Debug format looks like, i.e, the enum key: write!(f, "{:?}", self) } diff --git a/src/cp437.rs b/src/cp437.rs index 69547219..a1ef6d27 100644 --- a/src/cp437.rs +++ b/src/cp437.rs @@ -41,7 +41,7 @@ fn to_char(input: u8) -> char { let output = match input { - 0x00 ... 0x7f => input as u32, + 0x00 ..= 0x7f => input as u32, 0x80 => 0x00c7, 0x81 => 0x00fc, 0x82 => 0x00e9, diff --git a/src/lib.rs b/src/lib.rs index cfe4a598..4685e18a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,19 +2,10 @@ #![warn(missing_docs)] -#[cfg(feature = "bzip2")] -extern crate bzip2; -extern crate crc32fast; -#[cfg(feature = "deflate")] -extern crate flate2; -extern crate podio; -#[cfg(feature = "time")] -extern crate time; - -pub use read::ZipArchive; -pub use write::ZipWriter; -pub use compression::CompressionMethod; -pub use types::DateTime; +pub use crate::read::ZipArchive; +pub use crate::write::ZipWriter; +pub use crate::compression::CompressionMethod; +pub use crate::types::DateTime; mod spec; mod crc32; diff --git a/src/read.rs b/src/read.rs index 2eb3d83b..9b4bad43 100644 --- a/src/read.rs +++ b/src/read.rs @@ -1,20 +1,18 @@ //! Structs for reading a ZIP archive -use crc32::Crc32Reader; -use compression::CompressionMethod; -use spec; -use result::{ZipResult, ZipError}; +use crate::crc32::Crc32Reader; +use crate::compression::CompressionMethod; +use crate::spec; +use crate::result::{ZipResult, ZipError}; use std::io; use std::io::prelude::*; use std::collections::HashMap; use std::borrow::Cow; use podio::{ReadPodExt, LittleEndian}; -use types::{ZipFileData, System, DateTime}; -use cp437::FromCp437; +use crate::types::{ZipFileData, System, DateTime}; +use crate::cp437::FromCp437; -#[cfg(feature = "deflate")] -use flate2; #[cfg(feature = "deflate")] use flate2::read::DeflateDecoder; @@ -64,11 +62,11 @@ pub struct ZipArchive enum ZipFileReader<'a> { NoReader, - Stored(Crc32Reader>), + Stored(Crc32Reader>), #[cfg(feature = "deflate")] - Deflated(Crc32Reader>>), + Deflated(Crc32Reader>>), #[cfg(feature = "bzip2")] - Bzip2(Crc32Reader>>), + Bzip2(Crc32Reader>>), } /// A struct for reading a zip file @@ -84,9 +82,9 @@ fn unsupported_zip_error(detail: &'static str) -> ZipResult fn make_reader<'a>( - compression_method: ::compression::CompressionMethod, + compression_method: crate::compression::CompressionMethod, crc32: u32, - reader: io::Take<&'a mut io::Read>) + reader: io::Take<&'a mut dyn io::Read>) -> ZipResult> { match compression_method { @@ -289,7 +287,7 @@ impl ZipArchive data.data_start = data.header_start + magic_and_header + file_name_length + extra_field_length; self.reader.seek(io::SeekFrom::Start(data.data_start))?; - let limit_reader = (self.reader.by_ref() as &mut Read).take(data.compressed_size); + let limit_reader = (self.reader.by_ref() as &mut dyn Read).take(data.compressed_size); Ok(ZipFile { reader: make_reader(data.compression_method, data.crc32, limit_reader)?, data: Cow::Borrowed(data) }) } @@ -414,20 +412,20 @@ fn parse_extra_field(file: &mut ZipFileData, data: &[u8]) -> ZipResult<()> Ok(()) } -fn get_reader<'a>(reader: &'a mut ZipFileReader) -> &'a mut Read { +fn get_reader<'a>(reader: &'a mut ZipFileReader<'_>) -> &'a mut dyn Read { match *reader { ZipFileReader::NoReader => panic!("ZipFileReader was in an invalid state"), - ZipFileReader::Stored(ref mut r) => r as &mut Read, + ZipFileReader::Stored(ref mut r) => r as &mut dyn Read, #[cfg(feature = "deflate")] - ZipFileReader::Deflated(ref mut r) => r as &mut Read, + ZipFileReader::Deflated(ref mut r) => r as &mut dyn Read, #[cfg(feature = "bzip2")] - ZipFileReader::Bzip2(ref mut r) => r as &mut Read, + ZipFileReader::Bzip2(ref mut r) => r as &mut dyn Read, } } /// Methods for retrieving information on zip files impl<'a> ZipFile<'a> { - fn get_reader(&mut self) -> &mut Read { + fn get_reader(&mut self) -> &mut dyn Read { get_reader(&mut self.reader) } /// Get the version of the file @@ -563,7 +561,7 @@ impl<'a> Drop for ZipFile<'a> { /// * `comment`: set to an empty string /// * `data_start`: set to 0 /// * `external_attributes`: `unix_mode()`: will return None -pub fn read_zipfile_from_stream<'a, R: io::Read>(reader: &'a mut R) -> ZipResult> { +pub fn read_zipfile_from_stream<'a, R: io::Read>(reader: &'a mut R) -> ZipResult>> { let signature = reader.read_u32::()?; match signature { @@ -630,7 +628,7 @@ pub fn read_zipfile_from_stream<'a, R: io::Read>(reader: &'a mut R) -> ZipResult return unsupported_zip_error("The file length is not available in the local header"); } - let limit_reader = (reader as &'a mut io::Read).take(result.compressed_size as u64); + let limit_reader = (reader as &'a mut dyn io::Read).take(result.compressed_size as u64); let result_crc32 = result.crc32; let result_compression_method = result.compression_method; diff --git a/src/result.rs b/src/result.rs index d82259ea..c826d1d4 100644 --- a/src/result.rs +++ b/src/result.rs @@ -27,14 +27,14 @@ pub enum ZipError impl ZipError { - fn detail(&self) -> ::std::borrow::Cow + fn detail(&self) -> ::std::borrow::Cow<'_, str> { use std::error::Error; match *self { ZipError::Io(ref io_err) => { - ("Io Error: ".to_string() + (io_err as &error::Error).description()).into() + ("Io Error: ".to_string() + (io_err as &dyn error::Error).description()).into() }, ZipError::InvalidArchive(msg) | ZipError::UnsupportedArchive(msg) => { (self.description().to_string() + ": " + msg).into() @@ -64,7 +64,7 @@ impl convert::From for io::Error impl fmt::Display for ZipError { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { fmt.write_str(&*self.detail()) } @@ -76,18 +76,18 @@ impl error::Error for ZipError { match *self { - ZipError::Io(ref io_err) => (io_err as &error::Error).description(), + ZipError::Io(ref io_err) => (io_err as &dyn error::Error).description(), ZipError::InvalidArchive(..) => "Invalid Zip archive", ZipError::UnsupportedArchive(..) => "Unsupported Zip archive", ZipError::FileNotFound => "Specified file not found in archive", } } - fn cause(&self) -> Option<&error::Error> + fn cause(&self) -> Option<&dyn error::Error> { match *self { - ZipError::Io(ref io_err) => Some(io_err as &error::Error), + ZipError::Io(ref io_err) => Some(io_err as &dyn error::Error), _ => None, } } diff --git a/src/spec.rs b/src/spec.rs index 9da07caa..8a165391 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -1,6 +1,6 @@ use std::io; use std::io::prelude::*; -use result::{ZipResult, ZipError}; +use crate::result::{ZipResult, ZipError}; use podio::{ReadPodExt, WritePodExt, LittleEndian}; pub const LOCAL_FILE_HEADER_SIGNATURE : u32 = 0x04034b50; diff --git a/src/types.rs b/src/types.rs index e0ed8006..640e0c52 100644 --- a/src/types.rs +++ b/src/types.rs @@ -202,7 +202,7 @@ pub struct ZipFileData /// True if the file is encrypted. pub encrypted: bool, /// Compression method used to store the file - pub compression_method: ::compression::CompressionMethod, + pub compression_method: crate::compression::CompressionMethod, /// Last modified time. This will only have a 2 second precision. pub last_modified_time: DateTime, /// CRC32 checksum @@ -258,7 +258,7 @@ impl ZipFileData { pub fn version_needed(&self) -> u16 { match self.compression_method { #[cfg(feature = "bzip2")] - ::compression::CompressionMethod::Bzip2 => 46, + crate::compression::CompressionMethod::Bzip2 => 46, _ => 20, } } @@ -283,7 +283,7 @@ mod test { system: System::Dos, version_made_by: 0, encrypted: false, - compression_method: ::compression::CompressionMethod::Stored, + compression_method: crate::compression::CompressionMethod::Stored, last_modified_time: DateTime::default(), crc32: 0, compressed_size: 0, diff --git a/src/write.rs b/src/write.rs index 5e4c223f..da3a4aae 100644 --- a/src/write.rs +++ b/src/write.rs @@ -1,25 +1,19 @@ //! Structs for creating a new zip archive -use compression::CompressionMethod; -use types::{ZipFileData, System, DEFAULT_VERSION, DateTime}; -use spec; +use crate::compression::CompressionMethod; +use crate::types::{ZipFileData, System, DEFAULT_VERSION, DateTime}; +use crate::spec; use crc32fast::Hasher; -use result::{ZipResult, ZipError}; +use crate::result::{ZipResult, ZipError}; use std::default::Default; use std::io; use std::io::prelude::*; use std::mem; -#[cfg(feature = "time")] -use time; use podio::{WritePodExt, LittleEndian}; -#[cfg(feature = "deflate")] -use flate2; #[cfg(feature = "deflate")] use flate2::write::DeflateEncoder; -#[cfg(feature = "bzip2")] -use bzip2; #[cfg(feature = "bzip2")] use bzip2::write::BzEncoder; @@ -395,13 +389,13 @@ impl GenericZipWriter Ok(()) } - fn ref_mut(&mut self) -> Option<&mut Write> { + fn ref_mut(&mut self) -> Option<&mut dyn Write> { match *self { - GenericZipWriter::Storer(ref mut w) => Some(w as &mut Write), + GenericZipWriter::Storer(ref mut w) => Some(w as &mut dyn Write), #[cfg(feature = "deflate")] - GenericZipWriter::Deflater(ref mut w) => Some(w as &mut Write), + GenericZipWriter::Deflater(ref mut w) => Some(w as &mut dyn Write), #[cfg(feature = "bzip2")] - GenericZipWriter::Bzip2(ref mut w) => Some(w as &mut Write), + GenericZipWriter::Bzip2(ref mut w) => Some(w as &mut dyn Write), GenericZipWriter::Closed => None, } } @@ -563,9 +557,9 @@ fn path_to_string(path: &std::path::Path) -> String { mod test { use std::io; use std::io::Write; - use types::DateTime; + use crate::types::DateTime; use super::{FileOptions, ZipWriter}; - use compression::CompressionMethod; + use crate::compression::CompressionMethod; #[test] fn write_empty_zip() { diff --git a/tests/end_to_end.rs b/tests/end_to_end.rs index cd69ce3c..6af89ced 100644 --- a/tests/end_to_end.rs +++ b/tests/end_to_end.rs @@ -1,5 +1,3 @@ -extern crate zip; - use std::io::prelude::*; use zip::write::FileOptions; use std::io::Cursor; diff --git a/tests/invalid_date.rs b/tests/invalid_date.rs index e5c24200..b245d2c0 100644 --- a/tests/invalid_date.rs +++ b/tests/invalid_date.rs @@ -1,5 +1,3 @@ -extern crate zip; - use zip::read::ZipArchive; use std::io::Cursor; diff --git a/tests/zip64_large.rs b/tests/zip64_large.rs index d10c91e8..f537edc1 100644 --- a/tests/zip64_large.rs +++ b/tests/zip64_large.rs @@ -53,9 +53,6 @@ // 22c400260 00 00 50 4b 05 06 00 00 00 00 03 00 03 00 27 01 |..PK..........'.| // 22c400270 00 00 ff ff ff ff 00 00 |........| // 22c400278 - -extern crate zip; - use std::io::{self, Seek, SeekFrom, Read}; const BLOCK1_LENGTH : u64 = 0x60; @@ -167,16 +164,16 @@ impl Read for Zip64File { return Ok(0); } match self.pointer { - BLOCK1_START ... BLOCK1_END => { + BLOCK1_START ..= BLOCK1_END => { buf[0] = BLOCK1[(self.pointer - BLOCK1_START) as usize]; }, - BLOCK2_START ... BLOCK2_END => { + BLOCK2_START ..= BLOCK2_END => { buf[0] = BLOCK2[(self.pointer - BLOCK2_START) as usize]; }, - BLOCK3_START ... BLOCK3_END => { + BLOCK3_START ..= BLOCK3_END => { buf[0] = BLOCK3[(self.pointer - BLOCK3_START) as usize]; }, - BLOCK4_START ... BLOCK4_END => { + BLOCK4_START ..= BLOCK4_END => { buf[0] = BLOCK4[(self.pointer - BLOCK4_START) as usize]; }, _ => {