Skip to content

Commit 871283d

Browse files
committed
Make the next_many API more Rust idiomatic
1 parent 840d999 commit 871283d

6 files changed

Lines changed: 109 additions & 112 deletions

File tree

roaring/src/bitmap/container.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl Iter<'_> {
478478
/// Returns the number of values read.
479479
///
480480
/// This can be significantly faster than calling `next()` repeatedly.
481-
pub(crate) fn next_many(&mut self, dst: &mut [u32]) -> usize {
481+
pub(crate) fn next_many<'a>(&mut self, dst: &'a mut [u32]) -> &'a [u32] {
482482
// Use a temporary u16 buffer for the inner iterator
483483
const BUF_SIZE: usize = 256;
484484
let mut buf = [0u16; BUF_SIZE];
@@ -489,17 +489,17 @@ impl Iter<'_> {
489489
while count < dst.len() {
490490
let remaining = dst.len() - count;
491491
let to_read = remaining.min(BUF_SIZE);
492-
let n = self.inner.next_many(&mut buf[..to_read]);
493-
if n == 0 {
492+
let out_len = self.inner.next_many(&mut buf[..to_read]).len();
493+
if out_len == 0 {
494494
break;
495495
}
496-
for i in 0..n {
496+
for i in 0..out_len {
497497
dst[count + i] = util::join(key, buf[i]);
498498
}
499-
count += n;
499+
count += out_len;
500500
}
501501

502-
count
502+
&dst[..count]
503503
}
504504
}
505505

roaring/src/bitmap/iter.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -349,29 +349,28 @@ impl Iter<'_> {
349349
/// let mut iter = bitmap.iter();
350350
/// let mut buf = [0u32; 32];
351351
///
352-
/// let n = iter.next_many(&mut buf);
353-
/// assert_eq!(n, 32);
354-
/// assert_eq!(buf[0], 0);
355-
/// assert_eq!(buf[31], 31);
352+
/// let out = iter.next_many(&mut buf);
353+
/// assert_eq!(out.len(), 32);
354+
/// assert_eq!(out[0], 0);
355+
/// assert_eq!(out[31], 31);
356356
///
357357
/// // Iterate remainder
358-
/// let n = iter.next_many(&mut buf);
359-
/// assert_eq!(n, 32);
360-
/// assert_eq!(buf[0], 32);
358+
/// let out = iter.next_many(&mut buf);
359+
/// assert_eq!(out.len(), 32);
360+
/// assert_eq!(out[0], 32);
361361
/// ```
362-
pub fn next_many(&mut self, dst: &mut [u32]) -> usize {
362+
pub fn next_many<'a>(&mut self, dst: &'a mut [u32]) -> &'a [u32] {
363363
if dst.is_empty() {
364-
return 0;
364+
return &[];
365365
}
366366

367367
let mut count = 0;
368368

369369
// First drain from the front container iterator if present
370370
if let Some(ref mut front_iter) = self.front {
371-
let n = front_iter.next_many(&mut dst[count..]);
372-
count += n;
371+
count += front_iter.next_many(&mut dst[count..]).len();
373372
if count >= dst.len() {
374-
return count;
373+
return &dst[..count];
375374
}
376375
// Front is exhausted
377376
self.front = None;
@@ -384,28 +383,28 @@ impl Iter<'_> {
384383
break;
385384
};
386385
let mut container_iter = container.into_iter();
387-
let n = container_iter.next_many(&mut dst[count..]);
388-
count += n;
386+
let out = container_iter.next_many(&mut dst[count..]);
387+
count += out.len();
389388

390389
// If container still has values, save it as new front
391-
if n > 0 && container_iter.len() > 0 {
390+
if !out.is_empty() && container_iter.len() > 0 {
392391
self.front = Some(container_iter);
393-
return count;
392+
return &dst[..count];
394393
}
395394
}
396395

397396
// Finally, try draining from the back iterator if present
398397
if count < dst.len() {
399398
if let Some(ref mut back_iter) = self.back {
400399
let n = back_iter.next_many(&mut dst[count..]);
401-
count += n;
400+
count += n.len();
402401
if back_iter.len() == 0 {
403402
self.back = None;
404403
}
405404
}
406405
}
407406

408-
count
407+
&dst[..count]
409408
}
410409
}
411410

@@ -513,29 +512,28 @@ impl IntoIter {
513512
/// let mut iter = bitmap.into_iter();
514513
/// let mut buf = [0u32; 32];
515514
///
516-
/// let n = iter.next_many(&mut buf);
517-
/// assert_eq!(n, 32);
518-
/// assert_eq!(buf[0], 0);
519-
/// assert_eq!(buf[31], 31);
515+
/// let out = iter.next_many(&mut buf);
516+
/// assert_eq!(out.len(), 32);
517+
/// assert_eq!(out[0], 0);
518+
/// assert_eq!(out[31], 31);
520519
///
521520
/// // Iterate remainder
522-
/// let n = iter.next_many(&mut buf);
523-
/// assert_eq!(n, 32);
524-
/// assert_eq!(buf[0], 32);
521+
/// let out = iter.next_many(&mut buf);
522+
/// assert_eq!(out.len(), 32);
523+
/// assert_eq!(out[0], 32);
525524
/// ```
526-
pub fn next_many(&mut self, dst: &mut [u32]) -> usize {
525+
pub fn next_many<'a>(&mut self, dst: &'a mut [u32]) -> &'a [u32] {
527526
if dst.is_empty() {
528-
return 0;
527+
return &[];
529528
}
530529

531530
let mut count = 0;
532531

533532
// First drain from the front container iterator if present
534533
if let Some(ref mut front_iter) = self.front {
535-
let n = front_iter.next_many(&mut dst[count..]);
536-
count += n;
534+
count += front_iter.next_many(&mut dst[count..]).len();
537535
if count >= dst.len() {
538-
return count;
536+
return &dst[..count];
539537
}
540538
// Front is exhausted
541539
self.front = None;
@@ -548,28 +546,27 @@ impl IntoIter {
548546
break;
549547
};
550548
let mut container_iter = container.into_iter();
551-
let n = container_iter.next_many(&mut dst[count..]);
552-
count += n;
549+
let out = container_iter.next_many(&mut dst[count..]);
550+
count += out.len();
553551

554552
// If container still has values, save it as new front
555-
if n > 0 && container_iter.len() > 0 {
553+
if !out.is_empty() && container_iter.len() > 0 {
556554
self.front = Some(container_iter);
557-
return count;
555+
return &dst[..count];
558556
}
559557
}
560558

561559
// Finally, try draining from the back iterator if present
562560
if count < dst.len() {
563561
if let Some(ref mut back_iter) = self.back {
564-
let n = back_iter.next_many(&mut dst[count..]);
565-
count += n;
562+
count += back_iter.next_many(&mut dst[count..]).len();
566563
if back_iter.len() == 0 {
567564
self.back = None;
568565
}
569566
}
570567
}
571568

572-
count
569+
&dst[..count]
573570
}
574571
}
575572

roaring/src/bitmap/store/bitmap_store.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,9 @@ impl<B: Borrow<[u64; BITMAP_LENGTH]>> BitmapIter<B> {
696696
/// Returns the number of values read.
697697
///
698698
/// This can be significantly faster than calling `next()` repeatedly.
699-
pub fn next_many(&mut self, dst: &mut [u16]) -> usize {
699+
pub fn next_many<'a>(&mut self, dst: &'a mut [u16]) -> &'a [u16] {
700700
if dst.is_empty() {
701-
return 0;
701+
return &[];
702702
}
703703

704704
let mut count = 0;
@@ -738,7 +738,7 @@ impl<B: Borrow<[u64; BITMAP_LENGTH]>> BitmapIter<B> {
738738
}
739739
}
740740

741-
count
741+
&dst[..count]
742742
}
743743
}
744744

roaring/src/bitmap/store/interval_store.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,9 @@ impl<I: SliceIterator<Interval>> RunIter<I> {
840840
///
841841
/// This can be significantly faster than calling `next()` repeatedly
842842
/// because it processes runs in bulk.
843-
pub fn next_many(&mut self, dst: &mut [u16]) -> usize {
843+
pub fn next_many<'a>(&mut self, dst: &'a mut [u16]) -> &'a [u16] {
844844
if dst.is_empty() {
845-
return 0;
845+
return &[];
846846
}
847847

848848
let mut count = 0;
@@ -852,11 +852,8 @@ impl<I: SliceIterator<Interval>> RunIter<I> {
852852
break;
853853
};
854854

855-
let end_offset = if self.intervals.as_slice().len() == 1 {
856-
self.backward_offset
857-
} else {
858-
0
859-
};
855+
let end_offset =
856+
if self.intervals.as_slice().len() == 1 { self.backward_offset } else { 0 };
860857

861858
let start = interval.start + self.forward_offset;
862859
let end = interval.end - end_offset;
@@ -885,7 +882,7 @@ impl<I: SliceIterator<Interval>> RunIter<I> {
885882
}
886883
}
887884

888-
count
885+
&dst[..count]
889886
}
890887
}
891888

roaring/src/bitmap/store/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,21 +1043,25 @@ impl Iter<'_> {
10431043
/// Returns the number of values read.
10441044
///
10451045
/// This can be significantly faster than calling `next()` repeatedly.
1046-
pub fn next_many(&mut self, dst: &mut [u16]) -> usize {
1046+
pub fn next_many<'a>(&mut self, dst: &'a mut [u16]) -> &'a [u16] {
10471047
match self {
10481048
Iter::Array(inner) => {
10491049
let remaining = inner.as_slice();
10501050
let n = remaining.len().min(dst.len());
10511051
dst[..n].copy_from_slice(&remaining[..n]);
1052-
if n > 0 { _ = inner.nth(n - 1); }
1053-
n
1052+
if n > 0 {
1053+
_ = inner.nth(n - 1);
1054+
}
1055+
&dst[..n]
10541056
}
10551057
Iter::Vec(inner) => {
10561058
let remaining = inner.as_slice();
10571059
let n = remaining.len().min(dst.len());
10581060
dst[..n].copy_from_slice(&remaining[..n]);
1059-
if n > 0 { _ = inner.nth(n - 1); }
1060-
n
1061+
if n > 0 {
1062+
_ = inner.nth(n - 1);
1063+
}
1064+
&dst[..n]
10611065
}
10621066
Iter::BitmapBorrowed(inner) => inner.next_many(dst),
10631067
Iter::BitmapOwned(inner) => inner.next_many(dst),
@@ -1067,7 +1071,6 @@ impl Iter<'_> {
10671071
}
10681072
}
10691073

1070-
10711074
impl DoubleEndedIterator for Iter<'_> {
10721075
fn next_back(&mut self) -> Option<Self::Item> {
10731076
match self {

0 commit comments

Comments
 (0)