Skip to content

Commit

Permalink
Use pub(crate) for items that aren't exported
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Nov 12, 2024
1 parent 10ccb41 commit 0a6da4a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion blocksort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
48 changes: 24 additions & 24 deletions bzlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -240,7 +240,7 @@ pub(crate) fn dangling<T>() -> *mut T {
ptr::null_mut::<T>().wrapping_add(mem::align_of::<T>())
}

pub struct Arr1 {
pub(crate) struct Arr1 {
ptr: *mut u32,
len: usize,
}
Expand Down Expand Up @@ -275,7 +275,7 @@ impl Arr1 {
}
}

pub struct Arr2 {
pub(crate) struct Arr2 {
ptr: *mut u32,
len: usize,
}
Expand Down Expand Up @@ -339,7 +339,7 @@ impl Arr2 {
}
}

pub struct Ftab {
pub(crate) struct Ftab {
ptr: *mut u32,
}

Expand All @@ -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() }
}
Expand All @@ -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,
Expand Down Expand Up @@ -431,7 +431,7 @@ pub struct DState {
pub save_gPerm: i32,
}

pub struct DSlice<T> {
pub(crate) struct DSlice<T> {
ptr: *mut T,
len: usize,
}
Expand All @@ -444,36 +444,36 @@ impl<T> DSlice<T> {
}
}

pub unsafe fn alloc(bzalloc: AllocFunc, opaque: *mut c_void, len: usize) -> Option<Self> {
pub(crate) unsafe fn alloc(bzalloc: AllocFunc, opaque: *mut c_void, len: usize) -> Option<Self> {
let ptr = bzalloc_array::<T>(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())
}
}

/// 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) }
}
}

#[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,
Expand Down Expand Up @@ -523,7 +523,7 @@ fn isempty_rl(s: &mut EState) -> bool {
/// * a `NULL` pointer
/// * a valid pointer to an allocation of `len * size_of::<T>()` bytes aligned to at least `align_of::<usize>()`
/// - the type `T` must be zeroable (i.e. an all-zero bit pattern is valid for `T`)
pub unsafe fn bzalloc_array<T>(
unsafe fn bzalloc_array<T>(
bzalloc: AllocFunc,
opaque: *mut c_void,
len: usize,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down
1 change: 1 addition & 0 deletions c2rust-lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions compress.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -665,7 +665,7 @@ pub(crate) fn compress_block(s: &mut EState, is_last_block: bool) {
);
}

BZ2_blockSort(s);
block_sort(s);
}

{
Expand Down
2 changes: 1 addition & 1 deletion crctable.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion randtable.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 0a6da4a

Please sign in to comment.