Skip to content

Commit 0642965

Browse files
committed
Switch to core implicit prelude and only use std where required
By default, all modules in a crate will have the `std::prelude` included implicitly. By marking the crate as `no_std` and then manually adding `std` back using `extern crate std`, we ensure usage of `std` is explicit rather than implicit. Additionally, we can swap most imports from `std` to `core` and `alloc`. `std` is mostly re-exports of those two crates, so only `std::error` and `std::io` remain used. Further, `std::error` is only used to preserve MSRV, `core::error` provides the same trait.
1 parent 454a63c commit 0642965

20 files changed

Lines changed: 85 additions & 48 deletions

src/bufreader.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::cmp;
1211
use std::io;
1312
use std::io::prelude::*;
14-
use std::mem;
13+
use alloc::boxed::Box;
14+
use alloc::vec::Vec;
15+
use core::cmp;
16+
use core::mem;
1517

1618
pub struct BufReader<R> {
1719
inner: R,
@@ -20,11 +22,11 @@ pub struct BufReader<R> {
2022
cap: usize,
2123
}
2224

23-
impl<R> ::std::fmt::Debug for BufReader<R>
25+
impl<R> ::core::fmt::Debug for BufReader<R>
2426
where
25-
R: ::std::fmt::Debug,
27+
R: ::core::fmt::Debug,
2628
{
27-
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
29+
fn fmt(&self, fmt: &mut ::core::fmt::Formatter) -> Result<(), ::core::fmt::Error> {
2830
fmt.debug_struct("BufReader")
2931
.field("reader", &self.inner)
3032
.field(

src/deflate/bufread.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::io;
22
use std::io::prelude::*;
3-
use std::mem;
3+
use core::mem;
44

55
use crate::zio;
66
use crate::{Compress, Decompress};
@@ -255,6 +255,7 @@ mod test {
255255
use crate::deflate::write;
256256
use crate::Compression;
257257
use std::io::{Read, Write};
258+
use alloc::vec::Vec;
258259

259260
// DeflateDecoder consumes one deflate archive and then returns 0 for subsequent reads, allowing any
260261
// additional data to be consumed by the caller.
@@ -274,7 +275,7 @@ mod test {
274275
let mut decoder = DeflateDecoder::new(compressed.as_slice());
275276
let decoded_bytes = decoder.read_to_end(&mut output).unwrap();
276277
assert_eq!(decoded_bytes, output.len());
277-
let actual = std::str::from_utf8(&output).expect("String parsing error");
278+
let actual = core::str::from_utf8(&output).expect("String parsing error");
278279
assert_eq!(
279280
actual, expected,
280281
"after decompression we obtain the original input"

src/deflate/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ pub mod write;
55
#[cfg(test)]
66
mod tests {
77
use std::io::prelude::*;
8+
use alloc::string::ToString;
9+
use alloc::vec::Vec;
810

911
use rand::{rng, Rng};
1012

src/deflate/read.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::io;
22
use std::io::prelude::*;
3+
use alloc::vec::Vec;
34

45
use super::bufread;
56
use crate::bufreader::BufReader;

src/deflate/write.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ impl<W: Read + Write> Read for DeflateDecoder<W> {
329329
mod tests {
330330
use super::*;
331331
use crate::Compression;
332+
use alloc::string::String;
333+
use alloc::vec::Vec;
332334

333335
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
334336
Hello World Hello World Hello World Hello World Hello World \

src/ffi/c.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! Implementation for C backends.
2-
use std::fmt;
3-
use std::marker;
4-
use std::mem::MaybeUninit;
5-
use std::os::raw::{c_int, c_uint};
6-
use std::ptr;
2+
use alloc::boxed::Box;
3+
use core::ffi::{c_int, c_uint};
4+
use core::fmt;
5+
use core::marker;
6+
use core::mem::MaybeUninit;
7+
use core::ptr;
78

89
use super::*;
910
use crate::mem;
@@ -71,11 +72,11 @@ impl Drop for StreamWrapper {
7172
mod allocator {
7273
use super::*;
7374

74-
use std::alloc::{self, Layout};
75-
use std::convert::TryFrom;
76-
use std::os::raw::c_void;
75+
use core::alloc::Layout;
76+
use core::convert::TryFrom;
77+
use core::ffi::c_void;
7778

78-
const ALIGN: usize = std::mem::align_of::<usize>();
79+
const ALIGN: usize = core::mem::align_of::<usize>();
7980

8081
fn align_up(size: usize, align: usize) -> usize {
8182
(size + align - 1) & !(align - 1)
@@ -90,7 +91,7 @@ mod allocator {
9091
.checked_mul(item_size)
9192
.and_then(|i| usize::try_from(i).ok())
9293
.map(|size| align_up(size, ALIGN))
93-
.and_then(|i| i.checked_add(std::mem::size_of::<usize>()))
94+
.and_then(|i| i.checked_add(core::mem::size_of::<usize>()))
9495
{
9596
Some(i) => i,
9697
None => return ptr::null_mut(),
@@ -105,7 +106,7 @@ mod allocator {
105106
unsafe {
106107
// Allocate the data, and if successful store the size we allocated
107108
// at the beginning and then return an offset pointer.
108-
let ptr = alloc::alloc(layout) as *mut usize;
109+
let ptr = ::alloc::alloc::alloc(layout) as *mut usize;
109110
if ptr.is_null() {
110111
return ptr as *mut c_void;
111112
}
@@ -122,7 +123,7 @@ mod allocator {
122123
let ptr = (address as *mut usize).offset(-1);
123124
let size = *ptr;
124125
let layout = Layout::from_size_align_unchecked(size, ALIGN);
125-
alloc::dealloc(ptr as *mut u8, layout)
126+
::alloc::alloc::dealloc(ptr as *mut u8, layout)
126127
}
127128
}
128129
}
@@ -158,8 +159,8 @@ impl<D: Direction> Stream<D> {
158159
ErrorMessage(if msg.is_null() {
159160
None
160161
} else {
161-
let s = unsafe { std::ffi::CStr::from_ptr(msg) };
162-
std::str::from_utf8(s.to_bytes()).ok()
162+
let s = unsafe { core::ffi::CStr::from_ptr(msg) };
163+
core::str::from_utf8(s.to_bytes()).ok()
163164
})
164165
}
165166
}
@@ -418,8 +419,8 @@ pub use self::c_backend::*;
418419
#[allow(bad_style)]
419420
#[allow(unused_imports)]
420421
mod c_backend {
421-
use std::mem;
422-
use std::os::raw::{c_char, c_int};
422+
use core::ffi::{c_char, c_int};
423+
use core::mem;
423424

424425
#[cfg(feature = "zlib-ng")]
425426
use libz_ng_sys as libz;

src/ffi/miniz_oxide.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Implementation for `miniz_oxide` rust backend.
22
3-
use std::convert::TryInto;
4-
use std::fmt;
3+
use alloc::boxed::Box;
4+
use core::convert::TryInto;
5+
use core::fmt;
56

67
use ::miniz_oxide::deflate::core::CompressorOxide;
78
use ::miniz_oxide::inflate::stream::InflateState;

src/ffi/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::mem::{CompressError, DecompressError, FlushCompress, FlushDecompress, Status};
44
use crate::Compression;
5-
use std::mem::MaybeUninit;
5+
use core::mem::MaybeUninit;
66

77
fn initialize_buffer(output: &mut [MaybeUninit<u8>]) -> &mut [u8] {
88
// SAFETY: Here we zero-initialize the output and cast it to [u8]
@@ -81,8 +81,8 @@ pub use self::miniz_oxide::*;
8181
#[cfg(not(feature = "any_impl"))]
8282
compile_error!("No compression backend selected; enable one of `zlib`, `zlib-ng`, `zlib-rs`, or the default `rust_backend` feature.");
8383

84-
impl std::fmt::Debug for ErrorMessage {
85-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
84+
impl core::fmt::Debug for ErrorMessage {
85+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
8686
self.get().fmt(f)
8787
}
8888
}

src/ffi/zlib_rs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! With zlib_rs the state is not self-referential and hence no boxing is needed. The `new` methods
1818
//! internally do allocate space for the (de)compression state.
1919
20-
use std::fmt;
20+
use core::fmt;
2121

2222
use ::zlib_rs::{DeflateFlush, InflateError, InflateFlush};
2323

src/gz/bufread.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::cmp;
21
use std::io;
32
use std::io::prelude::*;
4-
use std::mem;
3+
use alloc::vec::Vec;
4+
use core::cmp;
5+
use core::mem;
56

67
use super::{corrupt, read_into, GzBuilder, GzHeader, GzHeaderParser};
78
use crate::crc::CrcReader;
@@ -458,6 +459,7 @@ mod test {
458459
use crate::gz::write;
459460
use crate::Compression;
460461
use std::io::{Read, Write};
462+
use alloc::vec::Vec;
461463

462464
// GzDecoder consumes one gzip member and then returns 0 for subsequent reads, allowing any
463465
// additional data to be consumed by the caller.
@@ -477,7 +479,7 @@ mod test {
477479
let mut decoder = GzDecoder::new(compressed.as_slice());
478480
let decoded_bytes = decoder.read_to_end(&mut output).unwrap();
479481
assert_eq!(decoded_bytes, output.len());
480-
let actual = std::str::from_utf8(&output).expect("String parsing error");
482+
let actual = core::str::from_utf8(&output).expect("String parsing error");
481483
assert_eq!(
482484
actual, expected,
483485
"after decompression we obtain the original input"

0 commit comments

Comments
 (0)