Skip to content

Commit 8eb215a

Browse files
committed
Add new operation fill_count
Due to similarity with the iterator operation count this should be fairly clear, I think.
1 parent 62b55b7 commit 8eb215a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

fill/src/fill.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,31 @@ pub trait Fill<T> {
3333
self.fill(&mut iter);
3434
iter
3535
}
36+
37+
/// Fill the container and count the number of pulled items.
38+
///
39+
/// The count should be equal to the number of items that have been inserted into the
40+
/// container. At least a well-behaving containers inserts all items that are pulled from the
41+
/// iterator.
42+
///
43+
/// ## Examples
44+
///
45+
/// For containers with a statically known capacity this can be an alternative to checking the
46+
/// current state after a fill operation.
47+
///
48+
/// ```
49+
/// # use fill::Fill;
50+
/// let mut option = None;
51+
/// assert_eq!(option.fill_count(0..), 1);
52+
/// assert_eq!(option.fill_count(1..), 0);
53+
/// ```
54+
fn fill_count<I>(&mut self, iter: I) -> usize
55+
where I: IntoIterator<Item=T>
56+
{
57+
let mut count = 0;
58+
self.fill(iter.into_iter().inspect(|_| count += 1));
59+
count
60+
}
3661
}
3762

3863
#[cfg(feature = "alloc")]

0 commit comments

Comments
 (0)