Skip to content

Commit fbdf500

Browse files
committed
lock down block size
1 parent db79d66 commit fbdf500

2 files changed

Lines changed: 16 additions & 24 deletions

File tree

src/rust/integer_compression/fastpfor.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ use crate::rust::cursor::IncrementCursor;
1010
use crate::rust::integer_compression::{bitpacking, bitunpacking};
1111
use crate::{BlockCodec, FastPForError, FastPForResult};
1212

13+
mod sealed {
14+
/// Sealed marker trait: only `[u32; 128]` and `[u32; 256]` are valid `FastPFor` block arrays.
15+
///
16+
/// This is intentionally private so that users cannot implement it for other sizes,
17+
/// preventing instantiation of `FastPFor<N>` for unsupported `N` at compile time.
18+
pub trait BlockSize: bytemuck::Pod {}
19+
impl BlockSize for [u32; 128] {}
20+
impl BlockSize for [u32; 256] {}
21+
}
22+
1323
/// Overhead cost (in bits) for storing each exception's position in the block
1424
const OVERHEAD_OF_EACH_EXCEPT: u32 = 8;
1525

@@ -63,36 +73,20 @@ pub struct FastPFor<const N: usize> {
6373

6474
impl<const N: usize> Default for FastPFor<N>
6575
where
66-
[u32; N]: bytemuck::Pod,
76+
[u32; N]: sealed::BlockSize,
6777
{
6878
fn default() -> Self {
69-
Self::create(DEFAULT_PAGE_SIZE)
79+
Self::new(DEFAULT_PAGE_SIZE)
7080
.expect("DEFAULT_PAGE_SIZE is a multiple of all valid block sizes")
7181
}
7282
}
7383

74-
impl FastPFor<128> {
75-
/// Creates a new `FastPForBlock128` codec with the given page size.
84+
impl<const N: usize> FastPFor<N> {
85+
/// Creates a new `FastPForBlock` with a codec with the given page size.
7686
///
7787
/// Returns an error if `page_size` is not a multiple of 128.
7888
/// Use [`Default`] for the default page size.
7989
pub fn new(page_size: u32) -> FastPForResult<Self> {
80-
Self::create(page_size)
81-
}
82-
}
83-
84-
impl FastPFor<256> {
85-
/// Creates a new `FastPForBlock256` codec with the given page size.
86-
///
87-
/// Returns an error if `page_size` is not a multiple of 256.
88-
/// Use [`Default`] for the default page size.
89-
pub fn new(page_size: u32) -> FastPForResult<Self> {
90-
Self::create(page_size)
91-
}
92-
}
93-
94-
impl<const N: usize> FastPFor<N> {
95-
fn create(page_size: u32) -> FastPForResult<Self> {
9690
if page_size % N as u32 != 0 {
9791
return Err(FastPForError::InvalidPageSize {
9892
page_size,
@@ -499,7 +493,7 @@ impl<const N: usize> FastPFor<N> {
499493

500494
impl<const N: usize> BlockCodec for FastPFor<N>
501495
where
502-
[u32; N]: bytemuck::Pod,
496+
[u32; N]: sealed::BlockSize,
503497
{
504498
type Block = [u32; N];
505499

tests/basic_tests.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
#[path = "../src/test_utils.rs"]
66
mod test_utils;
77

8-
use bytemuck::cast_slice;
9-
use fastpfor::{BlockCodec, FastPForBlock128, FastPForBlock256, slice_to_blocks};
108
use rand::rngs::StdRng;
119
use rand::{RngExt as _, SeedableRng};
1210

13-
use crate::test_utils::{RNG_SEED, block_compress, block_roundtrip_all, roundtrip_all};
11+
use crate::test_utils::{RNG_SEED, block_roundtrip_all, roundtrip_all};
1412

1513
// ── Tests ─────────────────────────────────────────────────────────────────────
1614

0 commit comments

Comments
 (0)