diff --git a/roaring/src/bitmap/container.rs b/roaring/src/bitmap/container.rs index bd1fffc2..1d635cf2 100644 --- a/roaring/src/bitmap/container.rs +++ b/roaring/src/bitmap/container.rs @@ -26,7 +26,7 @@ pub(crate) struct Iter<'a> { } impl Container { - pub fn new(key: u16) -> Container { + pub const fn new(key: u16) -> Container { Container { key, store: Store::new() } } @@ -61,7 +61,7 @@ impl Container { self.store.len() } - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.store.is_empty() } diff --git a/roaring/src/bitmap/inherent.rs b/roaring/src/bitmap/inherent.rs index c8d01917..7726dbfc 100644 --- a/roaring/src/bitmap/inherent.rs +++ b/roaring/src/bitmap/inherent.rs @@ -20,7 +20,7 @@ impl RoaringBitmap { /// use roaring::RoaringBitmap; /// let rb = RoaringBitmap::new(); /// ``` - pub fn new() -> RoaringBitmap { + pub const fn new() -> RoaringBitmap { RoaringBitmap { containers: Vec::new() } } @@ -635,7 +635,7 @@ impl RoaringBitmap { /// assert_eq!(rb.is_empty(), false); /// ``` #[inline] - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.containers.is_empty() } diff --git a/roaring/src/bitmap/serialization.rs b/roaring/src/bitmap/serialization.rs index aa6544e3..aff24bef 100644 --- a/roaring/src/bitmap/serialization.rs +++ b/roaring/src/bitmap/serialization.rs @@ -343,7 +343,7 @@ impl RoaringBitmap { } } -fn header_size(size: usize, has_run_containers: bool) -> usize { +const fn header_size(size: usize, has_run_containers: bool) -> usize { if has_run_containers { // New format encodes the size (number of containers) into the 4 byte cookie // Additionally a bitmap is included marking which containers are run containers diff --git a/roaring/src/bitmap/store/array_store/mod.rs b/roaring/src/bitmap/store/array_store/mod.rs index 6a201edf..8ccf27b0 100644 --- a/roaring/src/bitmap/store/array_store/mod.rs +++ b/roaring/src/bitmap/store/array_store/mod.rs @@ -79,15 +79,15 @@ fn intersect(lhs: &[u16], rhs: &[u16], visitor: &mut impl BinaryOperationVisitor } impl ArrayStore { - pub fn new() -> ArrayStore { + pub const fn new() -> ArrayStore { ArrayStore { vec: vec![] } } - pub fn serialized_byte_size(cardinality: u64) -> usize { + pub const fn serialized_byte_size(cardinality: u64) -> usize { cardinality as usize * ARRAY_ELEMENT_BYTES } - pub fn byte_size(&self) -> usize { + pub const fn byte_size(&self) -> usize { Self::serialized_byte_size(self.len()) } @@ -97,7 +97,7 @@ impl ArrayStore { } /// The number of total values that can be inserted without needing to reallocate. - pub fn capacity(&self) -> usize { + pub const fn capacity(&self) -> usize { self.vec.capacity() } @@ -300,11 +300,11 @@ impl ArrayStore { BitmapStore::from_unchecked(len, bits) } - pub fn len(&self) -> u64 { + pub const fn len(&self) -> u64 { self.vec.len() as u64 } - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.vec.is_empty() } diff --git a/roaring/src/bitmap/store/array_store/vector.rs b/roaring/src/bitmap/store/array_store/vector.rs index b23c0575..290c58f6 100644 --- a/roaring/src/bitmap/store/array_store/vector.rs +++ b/roaring/src/bitmap/store/array_store/vector.rs @@ -31,6 +31,14 @@ const LANES: usize = 8; /// With `std`, this is detected at runtime on x86/x86-64 (and cached by `fearless_simd`). /// Otherwise, it is determined by the target features enabled at compile time. #[inline] +// Scoped to `not(std)`, the only config where the lint fires; a plain `expect` would go unfulfilled. +#[cfg_attr( + not(feature = "std"), + expect( + clippy::missing_const_for_fn, + reason = "`Level::new()` under `std` is runtime detection" + ) +)] fn level() -> Level { #[cfg(feature = "std")] return Level::new(); diff --git a/roaring/src/bitmap/store/array_store/visitor.rs b/roaring/src/bitmap/store/array_store/visitor.rs index 2702982e..a03a685c 100644 --- a/roaring/src/bitmap/store/array_store/visitor.rs +++ b/roaring/src/bitmap/store/array_store/visitor.rs @@ -76,11 +76,11 @@ pub struct CardinalityCounter { } impl CardinalityCounter { - pub fn new() -> CardinalityCounter { + pub const fn new() -> CardinalityCounter { CardinalityCounter { count: 0 } } - pub fn into_inner(self) -> u64 { + pub const fn into_inner(self) -> u64 { self.count as u64 } } diff --git a/roaring/src/bitmap/store/bitmap_store.rs b/roaring/src/bitmap/store/bitmap_store.rs index c7f6cbd5..39eadafe 100644 --- a/roaring/src/bitmap/store/bitmap_store.rs +++ b/roaring/src/bitmap/store/bitmap_store.rs @@ -26,7 +26,7 @@ impl BitmapStore { BitmapStore { len: 0, bits: Box::new([0; BITMAP_LENGTH]) } } - pub fn capacity(&self) -> usize { + pub const fn capacity(&self) -> usize { BITMAP_LENGTH * u64::BITS as usize } @@ -282,11 +282,11 @@ impl BitmapStore { ArrayStore::from_vec_unchecked(vec) } - pub fn len(&self) -> u64 { + pub const fn len(&self) -> u64 { self.len } - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.len == 0 } @@ -834,12 +834,12 @@ impl> DoubleEndedIterator for BitmapIter { impl> ExactSizeIterator for BitmapIter {} #[inline] -pub fn key(index: u16) -> usize { +pub const fn key(index: u16) -> usize { index as usize / 64 } #[inline] -pub fn bit(index: u16) -> usize { +pub const fn bit(index: u16) -> usize { index as usize % 64 } diff --git a/roaring/src/bitmap/store/interval_store.rs b/roaring/src/bitmap/store/interval_store.rs index 7319a6ff..b60c230b 100644 --- a/roaring/src/bitmap/store/interval_store.rs +++ b/roaring/src/bitmap/store/interval_store.rs @@ -32,11 +32,11 @@ impl IntervalStore { Self(alloc::vec![Interval::new_unchecked(0, u16::MAX)]) } - pub fn byte_size(&self) -> usize { + pub const fn byte_size(&self) -> usize { Self::serialized_byte_size(self.run_amount()) } - pub fn serialized_byte_size(run_amount: u64) -> usize { + pub const fn serialized_byte_size(run_amount: u64) -> usize { RUN_NUM_BYTES + (RUN_ELEMENT_BYTES * run_amount as usize) } @@ -446,7 +446,7 @@ impl IntervalStore { self.0.iter().map(|iv| iv.run_len()).sum() } - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.0.is_empty() } @@ -485,7 +485,7 @@ impl IntervalStore { None } - pub fn run_amount(&self) -> u64 { + pub const fn run_amount(&self) -> u64 { self.0.len() as u64 } @@ -678,7 +678,7 @@ pub(crate) struct RunIter> { } impl> RunIter { - fn new(intervals: I) -> Self { + const fn new(intervals: I) -> Self { Self { forward_offset: 0, backward_offset: 0, intervals } } @@ -956,7 +956,7 @@ impl IntoIterator for &'_ Interval { } } -pub(crate) fn cmp_index_interval(index: u16, iv: Interval) -> Ordering { +pub(crate) const fn cmp_index_interval(index: u16, iv: Interval) -> Ordering { if index < iv.start { Ordering::Less } else if index > iv.end { @@ -972,15 +972,15 @@ impl Interval { Self { start, end } } - pub fn start(&self) -> u16 { + pub const fn start(&self) -> u16 { self.start } - pub fn end(&self) -> u16 { + pub const fn end(&self) -> u16 { self.end } - pub fn overlaps(&self, interval: &Self) -> bool { + pub const fn overlaps(&self, interval: &Self) -> bool { interval.start <= self.end && self.start <= interval.end } @@ -996,7 +996,7 @@ impl Interval { u64::from(self.end - self.start) + 1 } - pub fn is_full(&self) -> bool { + pub const fn is_full(&self) -> bool { self.start == 0 && self.end == u16::MAX } } diff --git a/roaring/src/bitmap/store/mod.rs b/roaring/src/bitmap/store/mod.rs index 659d6bb3..c8143b17 100644 --- a/roaring/src/bitmap/store/mod.rs +++ b/roaring/src/bitmap/store/mod.rs @@ -42,7 +42,7 @@ pub(crate) enum Iter<'a> { } impl Store { - pub fn new() -> Store { + pub const fn new() -> Store { Store::Array(ArrayStore::new()) } @@ -257,7 +257,7 @@ impl Store { } } - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { match self { Array(vec) => vec.is_empty(), Bitmap(bits) => bits.is_empty(), diff --git a/roaring/src/bitmap/util.rs b/roaring/src/bitmap/util.rs index 80a4896e..8e82a152 100644 --- a/roaring/src/bitmap/util.rs +++ b/roaring/src/bitmap/util.rs @@ -3,7 +3,7 @@ use core::ops::{Bound, RangeBounds, RangeInclusive}; /// Returns the container key and the index /// in this container for a given integer. #[inline] -pub fn split(value: u32) -> (u16, u16) { +pub const fn split(value: u32) -> (u16, u16) { ((value >> 16) as u16, value as u16) } diff --git a/roaring/src/lib.rs b/roaring/src/lib.rs index a01c421d..d36f0d5f 100644 --- a/roaring/src/lib.rs +++ b/roaring/src/lib.rs @@ -53,7 +53,7 @@ pub struct NonSortedIntegers { impl NonSortedIntegers { /// Returns the number of elements that were - pub fn valid_until(&self) -> u64 { + pub const fn valid_until(&self) -> u64 { self.valid_until } } diff --git a/roaring/src/treemap/inherent.rs b/roaring/src/treemap/inherent.rs index 0cea4808..3f9bb32f 100644 --- a/roaring/src/treemap/inherent.rs +++ b/roaring/src/treemap/inherent.rs @@ -20,7 +20,7 @@ impl RoaringTreemap { /// use roaring::RoaringTreemap; /// let rb = RoaringTreemap::new(); /// ``` - pub fn new() -> RoaringTreemap { + pub const fn new() -> RoaringTreemap { RoaringTreemap { map: BTreeMap::new() } } diff --git a/roaring/src/treemap/util.rs b/roaring/src/treemap/util.rs index 55839c64..c127924d 100644 --- a/roaring/src/treemap/util.rs +++ b/roaring/src/treemap/util.rs @@ -1,7 +1,7 @@ use core::ops::{Bound, RangeBounds, RangeInclusive}; #[inline] -pub fn split(value: u64) -> (u32, u32) { +pub const fn split(value: u64) -> (u32, u32) { ((value >> 32) as u32, value as u32) }