Skip to content

Commit 4747688

Browse files
committed
Fully support building in no_std configuration
1 parent dee3698 commit 4747688

File tree

8 files changed

+79
-33
lines changed

8 files changed

+79
-33
lines changed

.github/workflows/checks.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ jobs:
8282
tool: cargo-nextest
8383
- name: cargo build
8484
run: cargo build --target ${{matrix.target}} ${{ matrix.features }}
85+
- name: cargo build (no_std)
86+
run: cargo rustc --target ${{matrix.target}} -p libbzip2-rs-sys --lib --no-default-features --crate-type rlib
87+
env:
88+
RUSTFLAGS: -Aunused_variables -Aunused_assignments
8589
- name: cargo nextest # reports segfaults in a helpful way
8690
run: cargo nextest run --target ${{matrix.target}} ${{ matrix.features }} --no-fail-fast --workspace
8791
env:

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ libbzip2-rs-sys = { path = "libbzip2-rs-sys/", default-features = false }
2929

3030
[dependencies]
3131
libc = "0.2"
32-
libbzip2-rs-sys = { path = "libbzip2-rs-sys/", default-features = false }
32+
libbzip2-rs-sys = { path = "libbzip2-rs-sys/" }
3333

3434
[dev-dependencies]
3535
tempfile = "3.13.0"

blocksort.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ fn fallbackSort(
238238
initial fmap and initial BH bits.
239239
--*/
240240
if verb >= 4 {
241-
eprintln!(" bucket sorting ...");
241+
#[cfg(feature = "std")]
242+
std::eprintln!(" bucket sorting ...");
242243
}
243244

244245
{
@@ -284,7 +285,8 @@ fn fallbackSort(
284285
H = 1;
285286
loop {
286287
if verb >= 4 {
287-
eprint!(" depth {:>6} has ", H);
288+
#[cfg(feature = "std")]
289+
std::eprint!(" depth {:>6} has ", H);
288290
}
289291
j = 0;
290292
i = 0;
@@ -352,7 +354,8 @@ fn fallbackSort(
352354
}
353355
}
354356
if verb >= 4 {
355-
eprintln!("{:>6} unresolved strings", nNotDone);
357+
#[cfg(feature = "std")]
358+
std::eprintln!("{:>6} unresolved strings", nNotDone);
356359
}
357360
H *= 2;
358361
if H > nblock || nNotDone == 0 {
@@ -361,7 +364,8 @@ fn fallbackSort(
361364
}
362365

363366
if verb >= 4 {
364-
eprintln!(" reconstructing block ...");
367+
#[cfg(feature = "std")]
368+
std::eprintln!(" reconstructing block ...");
365369
}
366370

367371
{
@@ -959,7 +963,8 @@ fn mainSort(
959963
let mut numQSorted: i32;
960964
let mut s: u16;
961965
if verb >= 4 as c_int {
962-
eprintln!(" main sort initialise ...");
966+
#[cfg(feature = "std")]
967+
std::eprintln!(" main sort initialise ...");
963968
}
964969

965970
/*-- set up the 2-byte frequency table --*/
@@ -994,7 +999,8 @@ fn mainSort(
994999
}
9951000

9961001
if verb >= 4 as c_int {
997-
eprintln!(" bucket sorting ...");
1002+
#[cfg(feature = "std")]
1003+
std::eprintln!(" bucket sorting ...");
9981004
}
9991005

10001006
/*-- Complete the initial radix sort --*/
@@ -1114,7 +1120,8 @@ fn mainSort(
11141120

11151121
if hi > lo {
11161122
if verb >= 4 as c_int {
1117-
eprintln!(
1123+
#[cfg(feature = "std")]
1124+
std::eprintln!(
11181125
" qsort [{:#x}, {:#x}] done {} this {}",
11191126
ss,
11201127
j,
@@ -1258,7 +1265,8 @@ fn mainSort(
12581265
}
12591266
}
12601267
if verb >= 4 as c_int {
1261-
eprintln!(
1268+
#[cfg(feature = "std")]
1269+
std::eprintln!(
12621270
" {} pointers, {} sorted, {} scanned",
12631271
nblock,
12641272
numQSorted,
@@ -1332,7 +1340,8 @@ fn BZ2_blockSortHelp(
13321340
);
13331341

13341342
if verbosity >= 3 {
1335-
eprintln!(
1343+
#[cfg(feature = "std")]
1344+
std::eprintln!(
13361345
" {} work, {} block, ratio {:5.2}",
13371346
budgetInit - budget,
13381347
nblock,
@@ -1342,7 +1351,8 @@ fn BZ2_blockSortHelp(
13421351

13431352
if budget < 0 {
13441353
if verbosity >= 2 as c_int {
1345-
eprintln!(" too repetitive; using fallback sorting algorithm");
1354+
#[cfg(feature = "std")]
1355+
std::eprintln!(" too repetitive; using fallback sorting algorithm");
13461356
}
13471357

13481358
fallbackSort(ptr, arr2, ftab, nblock as i32, verbosity);

bzlib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,13 +1648,16 @@ unsafe fn BZ2_bzDecompressHelp(strm: &mut bz_stream) -> ReturnCode {
16481648
if s.nblock_used == s.save_nblock + 1 && s.state_out_len == 0 {
16491649
s.calculatedBlockCRC = !s.calculatedBlockCRC;
16501650
if s.verbosity >= 3 {
1651-
eprint!(
1651+
#[cfg(feature = "std")]
1652+
std::eprint!(
16521653
" {{{:#08x}, {:#08x}}}",
1653-
s.storedBlockCRC, s.calculatedBlockCRC,
1654+
s.storedBlockCRC,
1655+
s.calculatedBlockCRC,
16541656
);
16551657
}
16561658
if s.verbosity >= 2 {
1657-
eprint!("]");
1659+
#[cfg(feature = "std")]
1660+
std::eprint!("]");
16581661
}
16591662
if s.calculatedBlockCRC != s.storedBlockCRC {
16601663
return ReturnCode::BZ_DATA_ERROR;
@@ -1672,9 +1675,11 @@ unsafe fn BZ2_bzDecompressHelp(strm: &mut bz_stream) -> ReturnCode {
16721675
_ => match BZ2_decompress(strm, s) {
16731676
ReturnCode::BZ_STREAM_END => {
16741677
if s.verbosity >= 3 {
1675-
eprint!(
1678+
#[cfg(feature = "std")]
1679+
std::eprint!(
16761680
"\n combined CRCs: stored = {:#08x}, computed = {:#08x}",
1677-
s.storedCombinedCRC, s.calculatedCombinedCRC,
1681+
s.storedCombinedCRC,
1682+
s.calculatedCombinedCRC,
16781683
);
16791684
}
16801685
if s.calculatedCombinedCRC != s.storedCombinedCRC {

c2rust-lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
#![no_std]
12
#![allow(non_snake_case)]
23
#![allow(clippy::too_many_arguments)]
34
#![allow(clippy::missing_safety_doc)] // FIXME remove once everything has safety docs
45
#![allow(clippy::needless_range_loop)] // FIXME remove once all instances are fixed
56

67
//! A drop-in compatible rust implementation of bzip2
78
9+
#[cfg(feature = "std")]
10+
extern crate std;
11+
812
use core::ffi::c_int;
913

1014
mod blocksort;
@@ -71,8 +75,13 @@ pub(crate) use libbzip2_rs_sys_version;
7175
macro_rules! assert_h {
7276
($condition:expr, $errcode:expr) => {{
7377
if !$condition {
74-
eprint!("{}", $crate::AssertFail($errcode));
78+
#[cfg(feature = "std")]
79+
std::eprint!("{}", $crate::AssertFail($errcode));
80+
#[cfg(feature = "std")]
7581
std::process::exit(3);
82+
83+
#[cfg(not(feature = "std"))]
84+
panic!("{}", $crate::AssertFail($errcode));
7685
}
7786
}};
7887
}

compress.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,12 @@ fn sendMTFValues(s: &mut EState) {
234234
let mtfv = s.arr1.mtfv();
235235

236236
if s.verbosity >= 3 {
237-
eprintln!(
237+
#[cfg(feature = "std")]
238+
std::eprintln!(
238239
" {} in block, {} after MTF & 1-2 coding, {}+2 syms in use",
239-
s.nblock, s.nMTF, s.nInUse,
240+
s.nblock,
241+
s.nMTF,
242+
s.nInUse,
240243
);
241244
}
242245

@@ -283,7 +286,8 @@ fn sendMTFValues(s: &mut EState) {
283286
}
284287

285288
if s.verbosity >= 3 {
286-
eprintln!(
289+
#[cfg(feature = "std")]
290+
std::eprintln!(
287291
" initial group {}, [{} .. {}], has {} syms ({:4.1}%)",
288292
nPart,
289293
gs,
@@ -444,15 +448,18 @@ fn sendMTFValues(s: &mut EState) {
444448
}
445449

446450
if s.verbosity >= 3 {
447-
eprint!(
451+
#[cfg(feature = "std")]
452+
std::eprint!(
448453
" pass {}: size is {}, grp uses are ",
449454
iter + 1,
450455
totc / 8,
451456
);
452457
for t in 0..nGroups {
453-
eprint!("{} ", fave[t],);
458+
#[cfg(feature = "std")]
459+
std::eprint!("{} ", fave[t],);
454460
}
455-
eprintln!();
461+
#[cfg(feature = "std")]
462+
std::eprintln!();
456463
}
457464

458465
/*--
@@ -532,7 +539,8 @@ fn sendMTFValues(s: &mut EState) {
532539
}
533540
}
534541
if s.verbosity >= 3 {
535-
eprint!(" bytes: mapping {}, ", writer.num_z as i32 - nBytes,);
542+
#[cfg(feature = "std")]
543+
std::eprint!(" bytes: mapping {}, ", writer.num_z as i32 - nBytes,);
536544
}
537545
}
538546

@@ -548,7 +556,8 @@ fn sendMTFValues(s: &mut EState) {
548556
writer.write(1, 0);
549557
}
550558
if s.verbosity >= 3 {
551-
eprint!("selectors {}, ", writer.num_z as i32 - nBytes);
559+
#[cfg(feature = "std")]
560+
std::eprint!("selectors {}, ", writer.num_z as i32 - nBytes);
552561
}
553562

554563
/*--- Now the coding tables. ---*/
@@ -570,7 +579,8 @@ fn sendMTFValues(s: &mut EState) {
570579
}
571580
}
572581
if s.verbosity >= 3 {
573-
eprint!("code lengths {}, ", writer.num_z as i32 - nBytes);
582+
#[cfg(feature = "std")]
583+
std::eprint!("code lengths {}, ", writer.num_z as i32 - nBytes);
574584
}
575585

576586
/*--- And finally, the block data proper ---*/
@@ -631,7 +641,8 @@ fn sendMTFValues(s: &mut EState) {
631641
assert_h!(selCtr == nSelectors, 3007);
632642

633643
if s.verbosity >= 3 {
634-
eprintln!("codes {}", writer.num_z as i32 - nBytes);
644+
#[cfg(feature = "std")]
645+
std::eprintln!("codes {}", writer.num_z as i32 - nBytes);
635646
}
636647
}
637648

@@ -645,9 +656,13 @@ pub fn BZ2_compressBlock(s: &mut EState, is_last_block: bool) {
645656
}
646657

647658
if s.verbosity >= 2 {
648-
eprintln!(
659+
#[cfg(feature = "std")]
660+
std::eprintln!(
649661
" block {}: crc = 0x{:08x}, combined CRC = 0x{:08x}, size = {}",
650-
s.blockNo, s.blockCRC, s.combinedCRC, s.nblock,
662+
s.blockNo,
663+
s.blockCRC,
664+
s.combinedCRC,
665+
s.nblock,
651666
);
652667
}
653668

@@ -713,7 +728,8 @@ pub fn BZ2_compressBlock(s: &mut EState, is_last_block: bool) {
713728
writer.write_u32(s.combinedCRC);
714729

715730
if s.verbosity >= 2 {
716-
eprint!(" final combined CRC = 0x{:08x}\n ", s.combinedCRC);
731+
#[cfg(feature = "std")]
732+
std::eprint!(" final combined CRC = 0x{:08x}\n ", s.combinedCRC);
717733
}
718734

719735
writer.finish();

decompress.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,8 @@ pub fn BZ2_decompress(strm: &mut bz_stream, s: &mut DState) -> ReturnCode {
565565

566566
s.currBlockNo += 1;
567567
if s.verbosity >= 2 {
568-
eprint!("\n [{}: huff+mtf ", s.currBlockNo);
568+
#[cfg(feature = "std")]
569+
std::eprint!("\n [{}: huff+mtf ", s.currBlockNo);
569570
}
570571
s.storedBlockCRC = 0_u32;
571572
current_block = BZ_X_BCRC_1;
@@ -1062,7 +1063,8 @@ pub fn BZ2_decompress(strm: &mut bz_stream, s: &mut DState) -> ReturnCode {
10621063
s.calculatedBlockCRC = 0xffffffffu32;
10631064
s.state = State::BZ_X_OUTPUT;
10641065
if s.verbosity >= 2 {
1065-
eprint!("rt+rld");
1066+
#[cfg(feature = "std")]
1067+
std::eprint!("rt+rld");
10661068
}
10671069
match s.smallDecompress {
10681070
DecompressMode::Small => {

test-libbzip2-rs-sys/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ edition.workspace = true
1212

1313
[dependencies]
1414
bzip2-sys = { version = "0.1.11", features = ["static"] }
15-
libbzip2-rs-sys = { workspace = true, default-features = false, features = ["testing-prefix"] }
15+
libbzip2-rs-sys = { workspace = true, default-features = true, features = ["testing-prefix"] }
1616
libc.workspace = true

0 commit comments

Comments
 (0)