diff --git a/Cargo.toml b/Cargo.toml index c90b8e0c..6cd66714 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,8 @@ readme = "Readme.md" [dependencies] byteorder = { version = "1.4", default-features = false } twox-hash = { version = "1.6", default-features = false } -thiserror = { package = "thiserror-core", version = "1.0.38", default-features = false } +thiserror = { version = "1.0.38", default-features = false } +core-error = { version = "0.0.0", default-features = false } [dev-dependencies] criterion = "0.3" @@ -21,7 +22,7 @@ rand = {version = "0.8.5", features = ["small_rng"]} [features] default = ["std"] -std = ["thiserror/std"] +std = [] [[bench]] name = "reversedbitreader_bench" diff --git a/src/blocks/literals_section.rs b/src/blocks/literals_section.rs index 8f2aa51d..330b068c 100644 --- a/src/blocks/literals_section.rs +++ b/src/blocks/literals_section.rs @@ -1,5 +1,8 @@ use super::super::decoding::bit_reader::{BitReader, GetBitsError}; +#[cfg(not(feature = "std"))] +use crate::std; + pub struct LiteralsSection { pub regenerated_size: u32, pub compressed_size: Option, diff --git a/src/blocks/sequence_section.rs b/src/blocks/sequence_section.rs index 544755e0..bb26277b 100644 --- a/src/blocks/sequence_section.rs +++ b/src/blocks/sequence_section.rs @@ -3,6 +3,9 @@ pub struct SequencesHeader { pub modes: Option, } +#[cfg(not(feature = "std"))] +use crate::std; + #[derive(Clone, Copy)] pub struct Sequence { pub ll: u32, diff --git a/src/decoding/bit_reader.rs b/src/decoding/bit_reader.rs index e07ac45c..cf52deb1 100644 --- a/src/decoding/bit_reader.rs +++ b/src/decoding/bit_reader.rs @@ -1,3 +1,6 @@ +#[cfg(not(feature = "std"))] +use crate::std; + pub struct BitReader<'s> { idx: usize, //index counts bits already read source: &'s [u8], diff --git a/src/decoding/block_decoder.rs b/src/decoding/block_decoder.rs index 0fbce8f4..c82875e0 100644 --- a/src/decoding/block_decoder.rs +++ b/src/decoding/block_decoder.rs @@ -13,6 +13,9 @@ use crate::decoding::scratch::DecoderScratch; use crate::decoding::sequence_execution::execute_sequences; use crate::io::{self, Read}; +#[cfg(not(feature = "std"))] +use crate::std; + pub struct BlockDecoder { header_buffer: [u8; 3], internal_state: DecoderState, diff --git a/src/decoding/decodebuffer.rs b/src/decoding/decodebuffer.rs index 15f43e2a..c006c2e6 100644 --- a/src/decoding/decodebuffer.rs +++ b/src/decoding/decodebuffer.rs @@ -2,6 +2,9 @@ use crate::io::{Error, Read, Write}; use alloc::vec::Vec; use core::hash::Hasher; +#[cfg(not(feature = "std"))] +use crate::std; + use twox_hash::XxHash64; use super::ringbuffer::RingBuffer; diff --git a/src/decoding/dictionary.rs b/src/decoding/dictionary.rs index aa676937..dda4c331 100644 --- a/src/decoding/dictionary.rs +++ b/src/decoding/dictionary.rs @@ -1,6 +1,9 @@ use alloc::vec::Vec; use core::convert::TryInto; +#[cfg(not(feature = "std"))] +use crate::std; + use crate::decoding::scratch::FSEScratch; use crate::decoding::scratch::HuffmanScratch; use crate::fse::FSETableError; diff --git a/src/decoding/literals_section_decoder.rs b/src/decoding/literals_section_decoder.rs index bd7fb18e..28a55dbb 100644 --- a/src/decoding/literals_section_decoder.rs +++ b/src/decoding/literals_section_decoder.rs @@ -4,6 +4,9 @@ use super::scratch::HuffmanScratch; use crate::huff0::{HuffmanDecoder, HuffmanDecoderError, HuffmanTableError}; use alloc::vec::Vec; +#[cfg(not(feature = "std"))] +use crate::std; + #[derive(Debug, thiserror::Error)] #[non_exhaustive] pub enum DecompressLiteralsError { diff --git a/src/decoding/sequence_execution.rs b/src/decoding/sequence_execution.rs index 5946df18..5db4283d 100644 --- a/src/decoding/sequence_execution.rs +++ b/src/decoding/sequence_execution.rs @@ -1,5 +1,8 @@ use super::{decodebuffer::DecodebufferError, scratch::DecoderScratch}; +#[cfg(not(feature = "std"))] +use crate::std; + #[derive(Debug, thiserror::Error)] #[non_exhaustive] pub enum ExecuteSequencesError { diff --git a/src/decoding/sequence_section_decoder.rs b/src/decoding/sequence_section_decoder.rs index 27ad4b25..2bc8f204 100644 --- a/src/decoding/sequence_section_decoder.rs +++ b/src/decoding/sequence_section_decoder.rs @@ -6,6 +6,9 @@ use super::scratch::FSEScratch; use crate::fse::{FSEDecoder, FSEDecoderError, FSETableError}; use alloc::vec::Vec; +#[cfg(not(feature = "std"))] +use crate::std; + #[derive(Debug, thiserror::Error)] #[non_exhaustive] pub enum DecodeSequenceError { diff --git a/src/frame.rs b/src/frame.rs index 3810e4bc..8d2c30a9 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -1,4 +1,8 @@ use crate::io::{Error, Read}; + +#[cfg(not(feature = "std"))] +use crate::std; + pub const MAGIC_NUM: u32 = 0xFD2F_B528; pub const MIN_WINDOW_SIZE: u64 = 1024; pub const MAX_WINDOW_SIZE: u64 = (1 << 41) + 7 * (1 << 38); diff --git a/src/frame_decoder.rs b/src/frame_decoder.rs index b871fe4e..764e4d2f 100644 --- a/src/frame_decoder.rs +++ b/src/frame_decoder.rs @@ -8,6 +8,9 @@ use alloc::vec::Vec; use core::convert::TryInto; use core::hash::Hasher; +#[cfg(not(feature = "std"))] +use crate::std; + /// This implements a decoder for zstd frames. This decoder is able to decode frames only partially and gives control /// over how many bytes/blocks will be decoded at a time (so you don't have to decode a 10GB file into memory all at once). /// It reads bytes as needed from a provided source and can be read from to collect partial results. diff --git a/src/fse/fse_decoder.rs b/src/fse/fse_decoder.rs index 21868ff8..06cc66a8 100644 --- a/src/fse/fse_decoder.rs +++ b/src/fse/fse_decoder.rs @@ -2,6 +2,9 @@ use crate::decoding::bit_reader::BitReader; use crate::decoding::bit_reader_reverse::{BitReaderReversed, GetBitsError}; use alloc::vec::Vec; +#[cfg(not(feature = "std"))] +use crate::std; + pub struct FSETable { pub decode: Vec, //used to decode symbols, and calculate the next state diff --git a/src/huff0/huff0_decoder.rs b/src/huff0/huff0_decoder.rs index a8aa7456..9fb3ca30 100644 --- a/src/huff0/huff0_decoder.rs +++ b/src/huff0/huff0_decoder.rs @@ -2,6 +2,9 @@ use crate::decoding::bit_reader_reverse::{BitReaderReversed, GetBitsError}; use crate::fse::{FSEDecoder, FSEDecoderError, FSETable, FSETableError}; use alloc::vec::Vec; +#[cfg(not(feature = "std"))] +use crate::std; + pub struct HuffmanTable { decode: Vec, diff --git a/src/io_nostd.rs b/src/io_nostd.rs index 1e5d1416..cecc26b0 100644 --- a/src/io_nostd.rs +++ b/src/io_nostd.rs @@ -1,5 +1,8 @@ use alloc::boxed::Box; +#[cfg(not(feature = "std"))] +use crate::std; + #[non_exhaustive] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] pub enum ErrorKind { @@ -30,13 +33,13 @@ impl core::fmt::Display for ErrorKind { #[derive(Debug)] pub struct Error { kind: ErrorKind, - err: Option>, + err: Option>, } impl Error { pub fn new(kind: ErrorKind, err: E) -> Self where - E: Into>, + E: Into>, { Self { kind, @@ -52,15 +55,15 @@ impl Error { self.kind } - pub fn get_ref(&self) -> Option<&(dyn core::error::Error + Send + Sync + 'static)> { + pub fn get_ref(&self) -> Option<&(dyn std::error::Error + Send + Sync + 'static)> { self.err.as_ref().map(|e| e.as_ref()) } - pub fn get_mut(&mut self) -> Option<&mut (dyn core::error::Error + Send + Sync + 'static)> { + pub fn get_mut(&mut self) -> Option<&mut (dyn std::error::Error + Send + Sync + 'static)> { self.err.as_mut().map(|e| e.as_mut()) } - pub fn into_inner(self) -> Option> { + pub fn into_inner(self) -> Option> { self.err } } @@ -77,7 +80,7 @@ impl core::fmt::Display for Error { } } -impl core::error::Error for Error {} +impl std::error::Error for Error {} impl From for Error { fn from(value: ErrorKind) -> Self { diff --git a/src/lib.rs b/src/lib.rs index aecef623..4f86a895 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,16 @@ #![no_std] #![deny(trivial_casts, trivial_numeric_casts, rust_2018_idioms)] -#![cfg_attr(not(feature = "std"), feature(error_in_core))] #[cfg(feature = "std")] extern crate std; +#[cfg(not(feature = "std"))] +mod std { + pub mod error { + pub use core_error::Error; + } +} + extern crate alloc; #[cfg(feature = "std")]