Skip to content

Commit 69e04a6

Browse files
lifetime and roaring doc (#106)
* lifetime and roaring doc * Update src/roaring.rs * lifetime and roaring * Update src/lib.rs --------- Co-authored-by: Nate Nethercott <nathaniel.nethercott@mistral.ai>
1 parent a5f804c commit 69e04a6

File tree

7 files changed

+19
-9
lines changed

7 files changed

+19
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ once_cell = { version = "1.21.3", optional = true }
4545
tempfile = { version = "3.21.0", optional = true }
4646
parking_lot = { version = "0.12.4", optional = true }
4747

48+
4849
[target.'cfg(not(windows))'.dependencies]
4950
madvise = "0.1.0"
5051

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ use metadata::{Metadata, MetadataCodec};
102102
use node::{Node, NodeCodec};
103103
use node_id::{NodeId, NodeMode};
104104
pub use reader::{QueryBuilder, Reader};
105+
pub use roaring::RoaringBitmapCodec;
105106
pub use writer::{HannoyBuilder, Writer};
106107

107108
/// The set of types used by the [`Distance`] trait.

src/roaring.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ use std::borrow::Cow;
33
use heed::BoxedError;
44
use roaring::RoaringBitmap;
55

6+
/// A `heed` codec for `roaring::RoaringBitmap`.
7+
///
8+
/// Encodes via [`RoaringBitmap::serialize_into`] and decodes via
9+
/// [`RoaringBitmap::deserialize_unchecked_from`].
10+
///
11+
/// # Safety
12+
/// Decoding trusts the bytes. Only use with data written by this codec or
13+
/// switch to [`RoaringBitmap::deserialize_from`] if you need validation.
614
pub struct RoaringBitmapCodec;
715

816
impl heed::BytesDecode<'_> for RoaringBitmapCodec {
@@ -16,7 +24,7 @@ impl heed::BytesDecode<'_> for RoaringBitmapCodec {
1624
impl heed::BytesEncode<'_> for RoaringBitmapCodec {
1725
type EItem = RoaringBitmap;
1826

19-
fn bytes_encode(item: &Self::EItem) -> Result<Cow<[u8]>, BoxedError> {
27+
fn bytes_encode(item: &Self::EItem) -> Result<Cow<'_, [u8]>, BoxedError> {
2028
let mut bytes = Vec::with_capacity(item.serialized_size());
2129
item.serialize_into(&mut bytes)?;
2230
Ok(Cow::Owned(bytes))

src/unaligned_vector/binary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const PACKED_WORD_BYTES: usize = std::mem::size_of::<BitPackedWord>();
1515
pub enum Binary {}
1616

1717
impl UnalignedVectorCodec for Binary {
18-
fn from_bytes(bytes: &[u8]) -> Result<Cow<UnalignedVector<Self>>, SizeMismatch> {
18+
fn from_bytes(bytes: &[u8]) -> Result<Cow<'_, UnalignedVector<Self>>, SizeMismatch> {
1919
let rem = bytes.len() % PACKED_WORD_BYTES;
2020
if rem == 0 {
2121
// safety: `UnalignedVector` is transparent

src/unaligned_vector/binary_quantized.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const QUANTIZED_WORD_BYTES: usize = std::mem::size_of::<QuantizedWord>();
1515
pub enum BinaryQuantized {}
1616

1717
impl UnalignedVectorCodec for BinaryQuantized {
18-
fn from_bytes(bytes: &[u8]) -> Result<Cow<UnalignedVector<Self>>, SizeMismatch> {
18+
fn from_bytes(bytes: &[u8]) -> Result<Cow<'_, UnalignedVector<Self>>, SizeMismatch> {
1919
let rem = bytes.len() % QUANTIZED_WORD_BYTES;
2020
if rem == 0 {
2121
// safety: `UnalignedVector` is transparent

src/unaligned_vector/f32.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::{SizeMismatch, UnalignedVector, UnalignedVectorCodec};
1010

1111
impl UnalignedVectorCodec for f32 {
1212
/// Creates an unaligned slice of f32 wrapper from a slice of bytes.
13-
fn from_bytes(bytes: &[u8]) -> Result<Cow<UnalignedVector<Self>>, SizeMismatch> {
13+
fn from_bytes(bytes: &[u8]) -> Result<Cow<'_, UnalignedVector<Self>>, SizeMismatch> {
1414
let rem = bytes.len() % size_of::<f32>();
1515
if rem == 0 {
1616
// safety: `UnalignedF32Slice` is transparent
@@ -22,7 +22,7 @@ impl UnalignedVectorCodec for f32 {
2222

2323
/// Creates an unaligned slice of f32 wrapper from a slice of f32.
2424
/// The slice is already known to be of the right length.
25-
fn from_slice(slice: &[f32]) -> Cow<UnalignedVector<Self>> {
25+
fn from_slice(slice: &[f32]) -> Cow<'_, UnalignedVector<Self>> {
2626
Self::from_bytes(cast_slice(slice)).unwrap()
2727
}
2828

src/unaligned_vector/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ mod binary_quantized_test;
2121
pub trait UnalignedVectorCodec: std::borrow::ToOwned + Sized {
2222
/// Creates an unaligned vector from a slice of bytes.
2323
/// Don't allocate.
24-
fn from_bytes(bytes: &[u8]) -> Result<Cow<UnalignedVector<Self>>, SizeMismatch>;
24+
fn from_bytes(bytes: &[u8]) -> Result<Cow<'_, UnalignedVector<Self>>, SizeMismatch>;
2525

2626
/// Creates an unaligned vector from a slice of f32.
2727
/// May allocate depending on the codec.
28-
fn from_slice(slice: &[f32]) -> Cow<UnalignedVector<Self>>;
28+
fn from_slice(slice: &[f32]) -> Cow<'_, UnalignedVector<Self>>;
2929

3030
/// Creates an unaligned slice of f32 wrapper from a slice of f32.
3131
/// The slice is already known to be of the right length.
@@ -62,13 +62,13 @@ pub struct UnalignedVector<Codec: UnalignedVectorCodec> {
6262
impl<Codec: UnalignedVectorCodec> UnalignedVector<Codec> {
6363
/// Creates an unaligned vector from a slice of bytes.
6464
/// Don't allocate.
65-
pub fn from_bytes(bytes: &[u8]) -> Result<Cow<UnalignedVector<Codec>>, SizeMismatch> {
65+
pub fn from_bytes(bytes: &[u8]) -> Result<Cow<'_, UnalignedVector<Codec>>, SizeMismatch> {
6666
Codec::from_bytes(bytes)
6767
}
6868

6969
/// Creates an unaligned vector from a slice of f32.
7070
/// May allocate depending on the codec.
71-
pub fn from_slice(slice: &[f32]) -> Cow<UnalignedVector<Codec>> {
71+
pub fn from_slice(slice: &[f32]) -> Cow<'_, UnalignedVector<Codec>> {
7272
Codec::from_slice(slice)
7373
}
7474

0 commit comments

Comments
 (0)