Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use thiserror rather than thiserror-core #48

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ 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"
rand = {version = "0.8.5", features = ["small_rng"]}

[features]
default = ["std"]
std = ["thiserror/std"]
std = []

[[bench]]
name = "reversedbitreader_bench"
Expand Down
3 changes: 3 additions & 0 deletions src/blocks/literals_section.rs
Original file line number Diff line number Diff line change
@@ -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<u32>,
Expand Down
3 changes: 3 additions & 0 deletions src/blocks/sequence_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ pub struct SequencesHeader {
pub modes: Option<CompressionModes>,
}

#[cfg(not(feature = "std"))]
use crate::std;

#[derive(Clone, Copy)]
pub struct Sequence {
pub ll: u32,
Expand Down
3 changes: 3 additions & 0 deletions src/decoding/bit_reader.rs
Original file line number Diff line number Diff line change
@@ -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],
Expand Down
3 changes: 3 additions & 0 deletions src/decoding/block_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/decoding/decodebuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/decoding/dictionary.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/decoding/literals_section_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/decoding/sequence_execution.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/decoding/sequence_section_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/frame.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/frame_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/fse/fse_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entry>, //used to decode symbols, and calculate the next state

Expand Down
3 changes: 3 additions & 0 deletions src/huff0/huff0_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entry>,

Expand Down
15 changes: 9 additions & 6 deletions src/io_nostd.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -30,13 +33,13 @@ impl core::fmt::Display for ErrorKind {
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
err: Option<Box<dyn core::error::Error + Send + Sync>>,
err: Option<Box<dyn std::error::Error + Send + Sync>>,
}

impl Error {
pub fn new<E>(kind: ErrorKind, err: E) -> Self
where
E: Into<Box<dyn core::error::Error + Send + Sync>>,
E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
Self {
kind,
Expand All @@ -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<Box<dyn core::error::Error + Send + Sync>> {
pub fn into_inner(self) -> Option<Box<dyn std::error::Error + Send + Sync>> {
self.err
}
}
Expand All @@ -77,7 +80,7 @@ impl core::fmt::Display for Error {
}
}

impl core::error::Error for Error {}
impl std::error::Error for Error {}

impl From<ErrorKind> for Error {
fn from(value: ErrorKind) -> Self {
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down