Skip to content

Commit

Permalink
Merge pull request #74 from kupiakos/master
Browse files Browse the repository at this point in the history
Turn the no_std feature into default-enabled std
  • Loading branch information
sile committed Jul 8, 2023
2 parents 33566f7 + ee29897 commit cc5fb68
Show file tree
Hide file tree
Showing 25 changed files with 187 additions and 353 deletions.
55 changes: 40 additions & 15 deletions .github/workflows/ci.yml
Expand Up @@ -11,6 +11,7 @@ jobs:
strategy:
matrix:
toolchain: [stable, beta, nightly]
features: [--no-default-features, --all-features]
steps:
- name: Checkout sources
uses: actions/checkout@v1
Expand All @@ -26,14 +27,49 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: check
args: --all-features --all
args: ${{ matrix.features }} --workspace

no-std:
name: no_std Check
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v1

- name: Install nightly toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true

- name: Install cargo no-std-check
uses: actions-rs/cargo@v1
with:
command: install
args: cargo-no-std-check

# https://github.com/mystor/cargo-no-std-check/issues/2
# `--no-default-features` doesn't work with `--workspace` correctly.
- name: Run cargo no-std-check on libflate crate
uses: actions-rs/cargo@v1
with:
command: no-std-check
args: --no-default-features

- name: Run cargo no-std-check on libflate_lz77 crate
uses: actions-rs/cargo@v1
with:
command: no-std-check
args: --no-default-features --manifest-path libflate_lz77/Cargo.toml

test:
name: Test Suite
runs-on: ubuntu-latest
strategy:
matrix:
toolchain: [stable, beta, nightly]
features: [--no-default-features, --all-features]
steps:
- name: Checkout sources
uses: actions/checkout@v1
Expand All @@ -49,14 +85,15 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features --all
args: ${{ matrix.features }} --workspace

lints:
name: Lints
runs-on: ubuntu-latest
strategy:
matrix:
toolchain: [stable, beta, nightly]
features: [--no-default-features, --all-features]
steps:
- name: Checkout sources
uses: actions/checkout@v1
Expand All @@ -75,21 +112,9 @@ jobs:
command: fmt
args: --all -- --check

- name: Run cargo clippy
if: matrix.toolchain == 'beta'
uses: actions-rs/cargo@v1
with:
command: clippy
# As `return_self_not_must_use` became `pedantic` in the 1.60.0-nightly channel,
# we allow the warning to avoid unnecessary code fixes.
# See: https://github.com/rust-lang/rust-clippy/issues/8197
#
# TODO: Remove this branch once stable-1.59.0 is released.
args: --all-features --all -- -D warnings --allow clippy::return_self_not_must_use

- name: Run cargo clippy
if: matrix.toolchain != 'beta'
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-features --all -- -D warnings
args: ${{ matrix.features }} --workspace -- -D warnings
16 changes: 9 additions & 7 deletions Cargo.toml
@@ -1,27 +1,29 @@
[package]
name = "libflate"
version = "1.4.0"
version = "2.0.0"
authors = ["Takeru Ohta <phjgt308@gmail.com>"]
description = "A Rust implementation of DEFLATE algorithm and related formats (ZLIB, GZIP)"
homepage = "https://github.com/sile/libflate"
repository = "https://github.com/sile/libflate"
readme = "README.md"
keywords = ["deflate", "gzip", "zlib"]
categories = ["compression"]
categories = ["compression", "no-std"]
license = "MIT"
edition = "2018"

[badges]
coveralls = {repository = "sile/libflate"}

[dependencies]
adler32 = "1"
crc32fast = "1.1.1"
libflate_lz77 = { path = "libflate_lz77", version = "1.1", default_features = false }
core2 = { version = "0.4", default_features = false, features = ["alloc"], optional = true }
adler32 = { version = "1", default-features = false }
crc32fast = { version = "1.1.1", default-features = false }
dary_heap = "0.3.5"
libflate_lz77 = { path = "libflate_lz77", version = "2.0.0", default-features = false }
core2 = { version = "0.4", default-features = false, features = ["alloc"] }

[features]
no_std = ["libflate_lz77/no_std", "core2"]
default = ["std"]
std = ["libflate_lz77/std", "core2/std"]

[dev-dependencies]
clap = "2"
Expand Down
38 changes: 15 additions & 23 deletions examples/flate.rs
@@ -1,32 +1,24 @@
#![cfg_attr(not(feature = "std"), no_std)]

extern crate clap;
extern crate libflate;

#[cfg(not(feature = "no_std"))]
use clap::App;
#[cfg(not(feature = "no_std"))]
use clap::Arg;
#[cfg(not(feature = "no_std"))]
use clap::SubCommand;
#[cfg(not(feature = "no_std"))]
use libflate::gzip;
#[cfg(not(feature = "no_std"))]
use libflate::zlib;
#[cfg(not(feature = "no_std"))]
use std::fs;
#[cfg(not(feature = "no_std"))]
use std::io;
#[cfg(not(feature = "no_std"))]
use std::io::Read;
#[cfg(not(feature = "no_std"))]
use std::io::Write;
#[cfg(not(feature = "no_std"))]
use std::process;

#[cfg(feature = "no_std")]
#[cfg(not(feature = "std"))]
fn main() {}

#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
fn main() {
use clap::App;
use clap::Arg;
use clap::SubCommand;
use libflate::gzip;
use libflate::zlib;
use std::fs;
use std::io;
use std::io::Read;
use std::io::Write;
use std::process;

let matches = App::new("deflate")
.arg(
Arg::with_name("INPUT")
Expand Down
11 changes: 6 additions & 5 deletions libflate_lz77/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "libflate_lz77"
version = "1.2.0"
version = "2.0.0"
authors = ["Takeru Ohta <phjgt308@gmail.com>"]
edition = "2018"
description = "LZ77 encoder for libflate crate"
Expand All @@ -16,11 +16,12 @@ coveralls = {repository = "sile/libflate"}

[dependencies]
rle-decode-fast = "1.0.0"
core2 = { version = "0.4", default-features = false, features = ["alloc"], optional = true }
hashbrown = { version = "0.13", optional = true }
core2 = { version = "0.4", default-features = false, features = ["alloc"] }
hashbrown = { version = "0.13" }

[dev-dependencies]
libflate = { path = "../", version = "1" }
libflate = { path = "../", version = "2.0", default-features = false }

[features]
no_std = ["core2", "hashbrown"]
default = ["std"]
std = ["core2/std", "libflate/std"]
8 changes: 3 additions & 5 deletions libflate_lz77/src/default.rs
@@ -1,11 +1,9 @@
#[cfg(feature = "no_std")]
use alloc::vec::Vec;
#[cfg(feature = "no_std")]
use core::cmp;
#[cfg(feature = "no_std")]
#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
#[cfg(not(feature = "no_std"))]
use std::{cmp, collections::HashMap};
#[cfg(feature = "std")]
use std::collections::HashMap;

use super::Code;
use super::Lz77Encode;
Expand Down
18 changes: 5 additions & 13 deletions libflate_lz77/src/lib.rs
Expand Up @@ -2,19 +2,15 @@
//!
//! LZ77 is a compression algorithm used in [DEFLATE](https://tools.ietf.org/html/rfc1951).
#![warn(missing_docs)]
#![cfg_attr(no_std, feature = "no_std")]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "no_std")]
extern crate alloc;

pub use self::default::{DefaultLz77Encoder, DefaultLz77EncoderBuilder};
#[cfg(feature = "no_std")]
use alloc::vec::Vec;
#[cfg(feature = "no_std")]
use core::cmp;
use core2::io;
use rle_decode_fast::rle_decode;
#[cfg(not(feature = "no_std"))]
use std::io;

mod default;

Expand Down Expand Up @@ -180,13 +176,13 @@ impl Lz77Decoder {
if self.buffer.len() < backward_distance as usize {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
format!(
"Too long backword reference: buffer.len={}, distance={}",
self.buffer.len(),
backward_distance
),
#[cfg(feature = "no_std")]
#[cfg(not(feature = "std"))]
"Too long backword reference",
));
}
Expand Down Expand Up @@ -240,7 +236,7 @@ impl Lz77Decoder {

impl io::Read for Lz77Decoder {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let copy_size = std::cmp::min(buf.len(), self.buffer.len() - self.offset);
let copy_size = cmp::min(buf.len(), self.buffer.len() - self.offset);
buf[..copy_size].copy_from_slice(&self.buffer[self.offset..][..copy_size]);
self.offset += copy_size;
self.truncate_old_buffer();
Expand All @@ -251,12 +247,8 @@ impl io::Read for Lz77Decoder {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "no_std")]
use alloc::vec::Vec;
#[cfg(feature = "no_std")]
use core2::io::Read as _;
#[cfg(not(feature = "no_std"))]
use std::io::Read as _;

#[test]
fn encoder_and_decoder_works() {
Expand Down
7 changes: 1 addition & 6 deletions src/bit.rs
@@ -1,7 +1,4 @@
#[cfg(feature = "no_std")]
use core2::io;
#[cfg(not(feature = "no_std"))]
use std::io;

#[derive(Debug)]
pub struct BitWriter<W> {
Expand Down Expand Up @@ -179,10 +176,8 @@ pub(crate) struct BitReaderState {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "no_std")]
use alloc::vec::Vec;
use core2::io;
#[cfg(not(feature = "no_std"))]
use std::io;

#[test]
fn writer_works() {
Expand Down
3 changes: 0 additions & 3 deletions src/checksum.rs
@@ -1,8 +1,5 @@
use adler32::RollingAdler32;
#[cfg(feature = "no_std")]
use core::fmt;
#[cfg(not(feature = "no_std"))]
use std::fmt;

pub struct Adler32(RollingAdler32);
impl Adler32 {
Expand Down
23 changes: 8 additions & 15 deletions src/deflate/decode.rs
@@ -1,10 +1,7 @@
use super::symbol;
use crate::bit;
use crate::lz77;
#[cfg(feature = "no_std")]
use core2::io::{self, Read};
#[cfg(not(feature = "no_std"))]
use std::io::{self, Read};

/// DEFLATE decoder.
#[derive(Debug)]
Expand All @@ -23,10 +20,9 @@ where
///
/// # Examples
/// ```
/// #[cfg(feature = "no_std")]
/// # extern crate alloc;
/// # use alloc::vec::Vec;
/// use core2::io::{Cursor, Read};
/// #[cfg(not(feature = "no_std"))]
/// use std::io::{Cursor, Read};
/// use libflate::deflate::Decoder;
///
/// let encoded_data = [243, 72, 205, 201, 201, 87, 8, 207, 47, 202, 73, 81, 4, 0];
Expand Down Expand Up @@ -58,10 +54,7 @@ where
///
/// # Examples
/// ```
/// #[cfg(feature = "no_std")]
/// use core2::io::Cursor;
/// #[cfg(not(feature = "no_std"))]
/// use std::io::Cursor;
/// use libflate::deflate::Decoder;
///
/// let encoded_data = [243, 72, 205, 201, 201, 87, 8, 207, 47, 202, 73, 81, 4, 0];
Expand Down Expand Up @@ -105,9 +98,9 @@ where
if used != len.into() {
Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
format!("The reader has incorrect length: expected {len}, read {used}"),
#[cfg(feature = "no_std")]
#[cfg(not(feature = "std"))]
"The reader has incorrect length",
))
} else {
Expand Down Expand Up @@ -173,10 +166,10 @@ where

#[cfg(test)]
mod tests {
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
use super::*;
use crate::deflate::symbol::{DynamicHuffmanCodec, HuffmanCodec};
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
use std::io;

#[test]
Expand All @@ -197,7 +190,7 @@ mod tests {
}

#[test]
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
fn it_works() {
let input = [
180, 253, 73, 143, 28, 201, 150, 46, 8, 254, 150, 184, 139, 75, 18, 69, 247, 32, 157,
Expand All @@ -219,7 +212,7 @@ mod tests {
}

#[test]
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
fn test_issue_64() {
let input = b"\x04\x04\x04\x05:\x1az*\xfc\x06\x01\x90\x01\x06\x01";
let mut decoder = Decoder::new(&input[..]);
Expand Down

0 comments on commit cc5fb68

Please sign in to comment.