Skip to content

Commit 085fa08

Browse files
authored
chore: cleanup results (#77)
1 parent bf5c165 commit 085fa08

8 files changed

Lines changed: 55 additions & 59 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ Unless otherwise specified, all codecs support `&[u32]` only.
1818
```text
1919
* BP32
2020
* Copy
21+
* FastBinaryPacking16
22+
* FastBinaryPacking32
2123
* FastBinaryPacking8
2224
* FastPFor128 (both `&[u32]` and `&[u64]`)
2325
* FastPFor256 (both `&[u32]` and `&[u64]`)
24-
* FastBinaryPacking16
25-
* FastBinaryPacking32
2626
* MaskedVByte
2727
* NewPFor
2828
* OptPFor
29-
* PFor2008
3029
* PFor
30+
* PFor2008
3131
* SimdBinaryPacking
3232
* SimdFastPFor128
3333
* SimdFastPFor256

src/codec.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bytemuck::{Pod, cast_slice};
22

3-
use crate::FastPForError;
3+
use crate::FastPForResult;
44

55
/// Internal default for max decompressed length. Used by trait defaults and C++ FFI.
66
#[inline]
@@ -23,13 +23,15 @@ pub(crate) fn default_max_decoded_len(compressed_words: usize) -> usize {
2323
///
2424
/// # Implementing this trait
2525
///
26-
/// ```rust,ignore
26+
/// ```
27+
/// # use fastpfor::{BlockCodec, FastPForResult};
28+
/// struct MyCodec;
2729
/// impl BlockCodec for MyCodec {
2830
/// type Block = [u32; 256];
29-
/// fn encode_blocks(&self, blocks: &[[u32; 256]], out: &mut Vec<u32>)
30-
/// -> Result<(), FastPForError> { ... }
31-
/// fn decode_blocks(&self, input: &[u32], expected_len: Option<u32>,
32-
/// out: &mut Vec<u32>) -> Result<usize, FastPForError> { ... }
31+
/// fn encode_blocks(&mut self, blocks: &[[u32; 256]], out: &mut Vec<u32>)
32+
/// -> FastPForResult<()> { todo!() }
33+
/// fn decode_blocks(&mut self, input: &[u32], expected_len: Option<u32>,
34+
/// out: &mut Vec<u32>) -> FastPForResult<usize> { todo!() }
3335
/// }
3436
/// ```
3537
pub trait BlockCodec {
@@ -54,11 +56,7 @@ pub trait BlockCodec {
5456
///
5557
/// No remainder is possible — the caller must split the input first using
5658
/// [`slice_to_blocks`] and handle any remainder separately.
57-
fn encode_blocks(
58-
&mut self,
59-
blocks: &[Self::Block],
60-
out: &mut Vec<u32>,
61-
) -> Result<(), FastPForError>;
59+
fn encode_blocks(&mut self, blocks: &[Self::Block], out: &mut Vec<u32>) -> FastPForResult<()>;
6260

6361
/// Decompress blocks from `input`, using the length stored in the header.
6462
///
@@ -75,7 +73,7 @@ pub trait BlockCodec {
7573
input: &[u32],
7674
expected_len: Option<u32>,
7775
out: &mut Vec<u32>,
78-
) -> Result<usize, FastPForError>;
76+
) -> FastPForResult<usize>;
7977

8078
/// Maximum decompressed element count for a given compressed input length.
8179
/// Reject `expected_len` values exceeding this to avoid allocation from bad data.
@@ -100,9 +98,9 @@ pub trait BlockCodec {
10098
#[cfg(feature = "cpp")]
10199
pub trait BlockCodec64 {
102100
/// Compress 64-bit integers into a 32-bit word stream.
103-
fn encode64(&mut self, input: &[u64], out: &mut Vec<u32>) -> Result<(), FastPForError>;
101+
fn encode64(&mut self, input: &[u64], out: &mut Vec<u32>) -> FastPForResult<()>;
104102
/// Decompress 64-bit integers from a 32-bit word stream.
105-
fn decode64(&mut self, input: &[u32], out: &mut Vec<u64>) -> Result<(), FastPForError>;
103+
fn decode64(&mut self, input: &[u32], out: &mut Vec<u64>) -> FastPForResult<()>;
106104
}
107105

108106
/// Compresses and decompresses an arbitrary-length `&[u32]` slice.
@@ -113,7 +111,7 @@ pub trait BlockCodec64 {
113111
/// to produce an `AnyLenCodec`.
114112
pub trait AnyLenCodec {
115113
/// Compress an arbitrary-length slice of `u32` values.
116-
fn encode(&mut self, input: &[u32], out: &mut Vec<u32>) -> Result<(), FastPForError>;
114+
fn encode(&mut self, input: &[u32], out: &mut Vec<u32>) -> FastPForResult<()>;
117115

118116
/// Maximum decompressed element count for a given compressed input length.
119117
/// Reject `expected_len` values exceeding this to avoid allocation from bad data.
@@ -140,7 +138,7 @@ pub trait AnyLenCodec {
140138
input: &[u32],
141139
out: &mut Vec<u32>,
142140
expected_len: Option<u32>,
143-
) -> Result<(), FastPForError>;
141+
) -> FastPForResult<()>;
144142
}
145143

146144
/// Split a flat `&[u32]` into `(&[Blocks::Block], &[u32])` without copying.
@@ -154,7 +152,8 @@ pub trait AnyLenCodec {
154152
///
155153
/// # Example
156154
///
157-
/// ```rust,ignore
155+
/// ```ignore
156+
/// # use fastpfor::{slice_to_blocks, FastPForBlock256};
158157
/// let data: Vec<u32> = (0..600).collect(); // 2 × 256 + 88 remainder
159158
/// let (blocks, remainder) = slice_to_blocks::<FastPForBlock256>(&data);
160159
/// assert_eq!(blocks.len(), 2); // 2 blocks of [u32; 256]

src/cpp/codecs.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cxx::UniquePtr;
22

3-
use crate::FastPForError;
3+
use crate::FastPForResult;
44
use crate::codec::{AnyLenCodec, BlockCodec64};
55
use crate::cpp::ffi;
66
use crate::cpp::wrappers::{
@@ -37,7 +37,7 @@ macro_rules! implement_cpp_codecs {
3737
}
3838

3939
impl AnyLenCodec for $name {
40-
fn encode(&mut self, input: &[u32], out: &mut Vec<u32>) -> Result<(), FastPForError> {
40+
fn encode(&mut self, input: &[u32], out: &mut Vec<u32>) -> FastPForResult<()> {
4141
encode32_to_vec_ffi(&self.0, input, out)
4242
}
4343

@@ -46,7 +46,7 @@ macro_rules! implement_cpp_codecs {
4646
input: &[u32],
4747
out: &mut Vec<u32>,
4848
expected_len: Option<u32>,
49-
) -> Result<(), FastPForError> {
49+
) -> FastPForResult<()> {
5050
decode32_anylen_ffi(&self.0, input, out, expected_len)
5151
}
5252
}
@@ -161,10 +161,10 @@ macro_rules! implement_cpp_codecs_64 {
161161
($($name:ident => $ffi:ident ,)*) => {
162162
$(
163163
impl BlockCodec64 for $name {
164-
fn encode64(&mut self, input: &[u64], out: &mut Vec<u32>) -> Result<(), FastPForError> {
164+
fn encode64(&mut self, input: &[u64], out: &mut Vec<u32>) -> FastPForResult<()> {
165165
encode64_to_vec_ffi(&self.0, input, out)
166166
}
167-
fn decode64(&mut self, input: &[u32], out: &mut Vec<u64>) -> Result<(), FastPForError> {
167+
fn decode64(&mut self, input: &[u32], out: &mut Vec<u64>) -> FastPForResult<()> {
168168
decode64_to_vec_ffi(&self.0, input, out)
169169
}
170170
}
@@ -192,7 +192,7 @@ pub(crate) mod tests {
192192
}
193193

194194
/// C++ `fastpfor256_codec` returns `CompositeCodec<FastPFor<8>, VariableByte>` — already
195-
/// any-length. Use it directly; do not wrap in Rust `CppComposite`.
195+
/// any-length. Use it directly; do not wrap in Rust `CompositeCodec`.
196196
#[test]
197197
fn test_cpp_fastpfor256_composite_anylen() {
198198
let mut codec = CppFastPFor256::new();

src/cpp/wrappers.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cxx::UniquePtr;
22

3-
use crate::FastPForError;
3+
use crate::FastPForResult;
44
use crate::codec::default_max_decoded_len;
55
use crate::cpp::ffi;
66
use crate::helpers::AsUsize;
@@ -14,7 +14,7 @@ pub fn encode32_to_vec_ffi(
1414
codec: &UniquePtr<ffi::IntegerCODEC>,
1515
input: &[u32],
1616
out: &mut Vec<u32>,
17-
) -> Result<(), FastPForError> {
17+
) -> FastPForResult<()> {
1818
let capacity = input.len() * 2 + 1024;
1919
let start = out.len();
2020
out.resize(start + capacity, 0);
@@ -28,7 +28,7 @@ fn decode32_to_vec_ffi(
2828
input: &[u32],
2929
out: &mut Vec<u32>,
3030
capacity: usize,
31-
) -> Result<(), FastPForError> {
31+
) -> FastPForResult<()> {
3232
if !input.is_empty() {
3333
let start = out.len();
3434
out.resize(start + capacity, 0);
@@ -43,7 +43,7 @@ pub fn decode32_anylen_ffi(
4343
input: &[u32],
4444
out: &mut Vec<u32>,
4545
expected_len: Option<u32>,
46-
) -> Result<(), FastPForError> {
46+
) -> FastPForResult<()> {
4747
let max = default_max_decoded_len(input.len());
4848
let capacity = if let Some(n) = expected_len {
4949
n.is_valid_expected(max)?
@@ -64,7 +64,7 @@ pub fn encode64_to_vec_ffi(
6464
codec: &UniquePtr<ffi::IntegerCODEC>,
6565
input: &[u64],
6666
out: &mut Vec<u32>,
67-
) -> Result<(), FastPForError> {
67+
) -> FastPForResult<()> {
6868
let capacity = input.len() * 3 + 1024;
6969
let start = out.len();
7070
out.resize(start + capacity, 0);
@@ -77,7 +77,7 @@ pub fn decode64_to_vec_ffi(
7777
codec: &UniquePtr<ffi::IntegerCODEC>,
7878
input: &[u32],
7979
out: &mut Vec<u64>,
80-
) -> Result<(), FastPForError> {
80+
) -> FastPForResult<()> {
8181
if !input.is_empty() {
8282
// C++ decodeArray needs output buffer. Variable-byte can pack multiple values per word.
8383
let capacity = input.len().saturating_mul(4);

src/helpers.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::FastPForError;
1+
use crate::{FastPForError, FastPForResult};
22

33
/// Finds the greatest multiple of `factor` that is less than or equal to `value`.
44
#[cfg_attr(feature = "cpp", allow(dead_code))]
@@ -18,7 +18,7 @@ pub trait AsUsize: Eq + Copy {
1818

1919
#[inline]
2020
#[cfg(feature = "cpp")]
21-
fn is_decoded_mismatch(self, expected: impl AsUsize) -> Result<(), FastPForError> {
21+
fn is_decoded_mismatch(self, expected: impl AsUsize) -> FastPForResult<()> {
2222
let actual = self.as_usize();
2323
let expected = expected.as_usize();
2424
if self.as_usize() == expected {
@@ -31,7 +31,7 @@ pub trait AsUsize: Eq + Copy {
3131
/// Returns an error if `expected` exceeds `max`.
3232
#[inline]
3333
#[cfg(feature = "cpp")]
34-
fn is_valid_expected(self, max: impl AsUsize) -> Result<usize, FastPForError> {
34+
fn is_valid_expected(self, max: impl AsUsize) -> FastPForResult<usize> {
3535
let expected = self.as_usize();
3636
let max = max.as_usize();
3737
if expected > max {
@@ -69,12 +69,12 @@ impl AsUsize for u32 {
6969

7070
#[cfg_attr(feature = "cpp", allow(dead_code))]
7171
pub trait GetWithErr<T> {
72-
fn get_val(&self, pos: impl AsUsize) -> Result<T, FastPForError>;
72+
fn get_val(&self, pos: impl AsUsize) -> FastPForResult<T>;
7373
}
7474

7575
impl<T: Copy> GetWithErr<T> for &[T] {
7676
#[inline]
77-
fn get_val(&self, pos: impl AsUsize) -> Result<T, FastPForError> {
77+
fn get_val(&self, pos: impl AsUsize) -> FastPForResult<T> {
7878
self.get(pos.as_usize())
7979
.copied()
8080
.ok_or(FastPForError::NotEnoughData)
@@ -83,7 +83,7 @@ impl<T: Copy> GetWithErr<T> for &[T] {
8383

8484
impl<T: Copy> GetWithErr<T> for Vec<T> {
8585
#[inline]
86-
fn get_val(&self, pos: impl AsUsize) -> Result<T, FastPForError> {
86+
fn get_val(&self, pos: impl AsUsize) -> FastPForResult<T> {
8787
self.as_slice().get_val(pos)
8888
}
8989
}

src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22
#![cfg_attr(docsrs, feature(doc_cfg))]
33
#![doc = include_str!("../README.md")]
44

5-
#[cfg(not(any(feature = "cpp", feature = "rust",)))]
5+
#[cfg(not(any(feature = "cpp", feature = "rust")))]
66
compile_error!("At least one of the features 'cpp' or 'rust' must be enabled");
77

88
// Error types are always available regardless of which codec features are enabled.
99
mod error;
1010
pub use error::{FastPForError, FastPForResult};
1111

12-
// FIXME: need decide on the external API. Some ideas:
13-
// - offer two sets of similar APIs - rust and cpp ffi
14-
// - it will be possible to enable/disable each with a feature flag
15-
// - introduce a new feature-agnostic API that will forward to either
16-
// - if both are enabled, forward to the more stable (ffi probably)
1712
#[cfg(feature = "cpp")]
1813
/// Rust wrapper for the [`FastPFOR` C++ library](https://github.com/fast-pack/FastPFor)
1914
pub mod cpp;

src/rust/integer_compression/fastpfor.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::array;
22
use std::io::Cursor;
33
use std::num::NonZeroU32;
44

5+
use bytemuck::cast_slice;
56
use bytes::{Buf as _, BufMut as _, BytesMut};
67

78
use crate::helpers::{GetWithErr, bits, greatest_multiple};
@@ -176,13 +177,13 @@ impl FastPFOR {
176177
/// - Writes header, packed data, metadata bytes, and exception values.
177178
///
178179
/// # Arguments
179-
/// * `thissize` - Must be multiple of `block_size`
180-
/// * `input_offset` - Advanced by `thissize`
180+
/// * `this_size` - Must be multiple of `block_size`
181+
/// * `input_offset` - Advanced by `this_size`
181182
/// * `output_offset` - Advanced by compressed size
182183
fn encode_page(
183184
&mut self,
184185
input: &[u32],
185-
thissize: u32,
186+
this_size: u32,
186187
input_offset: &mut Cursor<u32>,
187188
output: &mut [u32],
188189
output_offset: &mut Cursor<u32>,
@@ -196,7 +197,7 @@ impl FastPFOR {
196197
self.bytes_container.clear();
197198

198199
let mut tmp_input_offset = input_offset.position() as u32;
199-
let final_input_offset = tmp_input_offset + thissize - self.block_size;
200+
let final_input_offset = tmp_input_offset + this_size - self.block_size;
200201
while tmp_input_offset <= final_input_offset {
201202
self.best_bit_from_data(input, tmp_input_offset);
202203
self.bytes_container.put_u8(self.optimal_bits);
@@ -331,17 +332,17 @@ impl FastPFOR {
331332
/// unpacks regular values per block, patches in exceptions by position.
332333
///
333334
/// # Arguments
334-
/// * `thissize` - Expected decompressed integer count
335+
/// * `this_size` - Expected decompressed integer count
335336
/// * `input_offset` - Advanced by bytes read
336-
/// * `output_offset` - Advanced by `thissize`
337+
/// * `output_offset` - Advanced by `this_size`
337338
#[expect(clippy::too_many_lines)]
338339
fn decode_page(
339340
&mut self,
340341
input: &[u32],
341342
input_offset: &mut Cursor<u32>,
342343
output: &mut [u32],
343344
output_offset: &mut Cursor<u32>,
344-
thissize: u32,
345+
this_size: u32,
345346
) -> FastPForResult<()> {
346347
let n = u32::try_from(input.len())
347348
.map_err(|_| FastPForError::InvalidInputLength(input.len()))?;
@@ -362,7 +363,7 @@ impl FastPFOR {
362363
// The C++ encoder uses a raw `memcpy` of bytes into the u32 output (no endian
363364
// conversion), and the decoder does a raw reinterpret_cast back -- both native byte
364365
// order. `cast_slice` is the exact Rust equivalent: a safe, zero-copy native view.
365-
let input_bytes: &[u8] = bytemuck::cast_slice(input);
366+
let input_bytes: &[u8] = cast_slice(input);
366367
let mut byte_pos = (inexcept as usize)
367368
.checked_mul(4)
368369
.filter(|&bp| bp <= input_bytes.len())
@@ -448,7 +449,7 @@ impl FastPFOR {
448449
let mut tmp_output_offset = output_offset.position() as u32;
449450
let mut tmp_input_offset = input_offset.position() as u32;
450451

451-
let run_end = thissize / self.block_size;
452+
let run_end = this_size / self.block_size;
452453
for _ in 0..run_end {
453454
let bits = input_bytes.get_val(byte_pos)?;
454455
if bits > 32 {

0 commit comments

Comments
 (0)