From 0a6da4af1da919c581d62f9605d33417ba0d8128 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:12:30 +0100 Subject: [PATCH] Use pub(crate) for items that aren't exported --- blocksort.rs | 2 +- bzlib.rs | 48 ++++++++++++++++++++++++------------------------ c2rust-lib.rs | 1 + compress.rs | 8 ++++---- crctable.rs | 2 +- decompress.rs | 2 +- randtable.rs | 2 +- 7 files changed, 33 insertions(+), 32 deletions(-) diff --git a/blocksort.rs b/blocksort.rs index dfc4ea55d..8d5b7b40b 100644 --- a/blocksort.rs +++ b/blocksort.rs @@ -1286,7 +1286,7 @@ fn mainSort( /// All other areas of block destroyed /// ftab [ 0 .. 65536 ] destroyed /// arr1 [0 .. nblock-1] holds sorted order -pub fn BZ2_blockSort(s: &mut EState) { +pub(crate) fn block_sort(s: &mut EState) { let nblock = usize::try_from(s.nblock).unwrap(); let ptr = s.arr1.ptr(); diff --git a/bzlib.rs b/bzlib.rs index cfe18042b..92346a65b 100644 --- a/bzlib.rs +++ b/bzlib.rs @@ -158,7 +158,7 @@ impl bz_stream { #[repr(i32)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[allow(non_camel_case_types)] -pub enum ReturnCode { +pub(crate) enum ReturnCode { BZ_OK = 0, BZ_RUN_OK = 1, BZ_FLUSH_OK = 2, @@ -177,7 +177,7 @@ pub enum ReturnCode { #[repr(i32)] #[derive(Copy, Clone)] -pub enum Mode { +pub(crate) enum Mode { Idle = 1, Running = 2, Flushing = 3, @@ -186,20 +186,20 @@ pub enum Mode { #[repr(i32)] #[derive(Copy, Clone)] -pub enum State { +pub(crate) enum State { Output = 1, Input = 2, } -pub const BZ_N_RADIX: i32 = 2; -pub const BZ_N_QSORT: i32 = 12; -pub const BZ_N_SHELL: i32 = 18; -pub const BZ_N_OVERSHOOT: usize = (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2) as usize; +pub(crate) const BZ_N_RADIX: i32 = 2; +pub(crate) const BZ_N_QSORT: i32 = 12; +pub(crate) const BZ_N_SHELL: i32 = 18; +pub(crate) const BZ_N_OVERSHOOT: usize = (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2) as usize; -pub const FTAB_LEN: usize = u16::MAX as usize + 2; +pub(crate) const FTAB_LEN: usize = u16::MAX as usize + 2; #[repr(C)] -pub struct EState { +pub(crate) struct EState { pub strm: *mut bz_stream, pub mode: Mode, pub state: State, @@ -240,7 +240,7 @@ pub(crate) fn dangling() -> *mut T { ptr::null_mut::().wrapping_add(mem::align_of::()) } -pub struct Arr1 { +pub(crate) struct Arr1 { ptr: *mut u32, len: usize, } @@ -275,7 +275,7 @@ impl Arr1 { } } -pub struct Arr2 { +pub(crate) struct Arr2 { ptr: *mut u32, len: usize, } @@ -339,7 +339,7 @@ impl Arr2 { } } -pub struct Ftab { +pub(crate) struct Ftab { ptr: *mut u32, } @@ -353,7 +353,7 @@ impl Ftab { self.ptr.is_null() } - pub fn ftab(&mut self) -> &mut [u32; FTAB_LEN] { + pub(crate) fn ftab(&mut self) -> &mut [u32; FTAB_LEN] { // NOTE: this panics if the pointer is NULL, that is important! unsafe { self.ptr.cast::<[u32; FTAB_LEN]>().as_mut().unwrap() } } @@ -364,7 +364,7 @@ impl Ftab { } #[repr(C)] -pub struct DState { +pub(crate) struct DState { pub strm: *mut bz_stream, pub state: decompress::State, pub state_out_ch: u8, @@ -431,7 +431,7 @@ pub struct DState { pub save_gPerm: i32, } -pub struct DSlice { +pub(crate) struct DSlice { ptr: *mut T, len: usize, } @@ -444,12 +444,12 @@ impl DSlice { } } - pub unsafe fn alloc(bzalloc: AllocFunc, opaque: *mut c_void, len: usize) -> Option { + pub(crate) unsafe fn alloc(bzalloc: AllocFunc, opaque: *mut c_void, len: usize) -> Option { let ptr = bzalloc_array::(bzalloc, opaque, len)?; Some(Self::from_raw_parts_mut(ptr, len)) } - pub unsafe fn dealloc(&mut self, bzfree: FreeFunc, opaque: *mut c_void) { + pub(crate) unsafe fn dealloc(&mut self, bzfree: FreeFunc, opaque: *mut c_void) { let this = mem::replace(self, Self::new()); if this.len != 0 { bzfree(opaque, this.ptr.cast()) @@ -457,15 +457,15 @@ impl DSlice { } /// Safety: ptr must satisfy the requirements of [`core::slice::from_raw_parts_mut`]. - pub unsafe fn from_raw_parts_mut(ptr: *mut T, len: usize) -> Self { + pub(crate) unsafe fn from_raw_parts_mut(ptr: *mut T, len: usize) -> Self { Self { ptr, len } } - pub fn as_slice(&self) -> &[T] { + pub(crate) fn as_slice(&self) -> &[T] { unsafe { core::slice::from_raw_parts(self.ptr, self.len) } } - pub fn as_mut_slice(&mut self) -> &mut [T] { + pub(crate) fn as_mut_slice(&mut self) -> &mut [T] { unsafe { core::slice::from_raw_parts_mut(self.ptr, self.len) } } } @@ -473,7 +473,7 @@ impl DSlice { #[allow(non_camel_case_types)] #[derive(Copy, Clone)] #[repr(C)] -pub struct bzFile { +pub(crate) struct bzFile { pub handle: *mut FILE, pub buf: [i8; BZ_MAX_UNUSED as usize], pub bufN: i32, @@ -523,7 +523,7 @@ fn isempty_rl(s: &mut EState) -> bool { /// * a `NULL` pointer /// * a valid pointer to an allocation of `len * size_of::()` bytes aligned to at least `align_of::()` /// - the type `T` must be zeroable (i.e. an all-zero bit pattern is valid for `T`) -pub unsafe fn bzalloc_array( +unsafe fn bzalloc_array( bzalloc: AllocFunc, opaque: *mut c_void, len: usize, @@ -1039,7 +1039,7 @@ pub unsafe extern "C" fn BZ2_bzCompressEnd(strm: *mut bz_stream) -> c_int { } #[repr(u8)] -pub enum DecompressMode { +pub(crate) enum DecompressMode { Small, Fast, } @@ -2321,7 +2321,7 @@ pub unsafe extern "C" fn BZ2_bzBuffToBuffDecompress( #[derive(Copy, Clone)] #[repr(u8)] -pub enum Operation { +pub(crate) enum Operation { Reading, Writing, } diff --git a/c2rust-lib.rs b/c2rust-lib.rs index a1988ea02..746416274 100644 --- a/c2rust-lib.rs +++ b/c2rust-lib.rs @@ -3,6 +3,7 @@ #![allow(clippy::too_many_arguments)] #![allow(clippy::missing_safety_doc)] // FIXME remove once everything has safety docs #![allow(clippy::needless_range_loop)] // FIXME remove once all instances are fixed +#![deny(unreachable_pub)] //! A drop-in compatible rust implementation of bzip2 diff --git a/compress.rs b/compress.rs index 341d9d98b..9d70db944 100644 --- a/compress.rs +++ b/compress.rs @@ -1,16 +1,16 @@ #![forbid(unsafe_code)] -use crate::blocksort::BZ2_blockSort; +use crate::blocksort::block_sort; use crate::bzlib::{EState, BZ_MAX_SELECTORS, BZ_N_GROUPS, BZ_N_ITERS, BZ_RUNA, BZ_RUNB}; use crate::{assert_h, huffman}; -pub struct EWriter { +pub(crate) struct EWriter { pub num_z: u32, bs_live: i32, bs_buff: u32, } -pub struct LiveWriter<'a> { +pub(crate) struct LiveWriter<'a> { zbits: &'a mut [u8], writer: &'a mut EWriter, num_z: u32, @@ -665,7 +665,7 @@ pub(crate) fn compress_block(s: &mut EState, is_last_block: bool) { ); } - BZ2_blockSort(s); + block_sort(s); } { diff --git a/crctable.rs b/crctable.rs index 6623f7e7f..c21c71974 100644 --- a/crctable.rs +++ b/crctable.rs @@ -1,4 +1,4 @@ -pub static BZ2_CRC32TABLE: [u32; 256] = [ +pub(crate) static BZ2_CRC32TABLE: [u32; 256] = [ 0, 0x4c11db7, 0x9823b6e, 0xd4326d9, 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75, 0x6a1936c8, diff --git a/decompress.rs b/decompress.rs index 66357e169..89b58c22e 100644 --- a/decompress.rs +++ b/decompress.rs @@ -12,7 +12,7 @@ const MTFL_SIZE: i32 = 16; #[repr(i32)] #[derive(Debug, Clone, Copy)] #[allow(non_camel_case_types)] -pub enum State { +pub(crate) enum State { BZ_X_IDLE = 1, BZ_X_OUTPUT = 2, BZ_X_MAGIC_1 = 10, diff --git a/randtable.rs b/randtable.rs index e46c6c8ff..bcf1c0e5d 100644 --- a/randtable.rs +++ b/randtable.rs @@ -1,4 +1,4 @@ -pub static BZ2_RNUMS: [i32; 512] = [ +pub(crate) static BZ2_RNUMS: [i32; 512] = [ 619, 720, 127, 481, 931, 816, 813, 233, 566, 247, 985, 724, 205, 454, 863, 491, 741, 242, 949, 214, 733, 859, 335, 708, 621, 574, 73, 654, 730, 472, 419, 436, 278, 496, 867, 210, 399, 680, 480, 51, 878, 465, 811, 169, 869, 675, 611, 697, 867, 561, 862, 687, 507, 283, 482, 129, 807,