mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-05-24 01:00:49 +00:00
Fit range to len
This commit is contained in:
parent
2756c463d5
commit
e5b9dee591
1 changed files with 7 additions and 2 deletions
|
@ -102,7 +102,7 @@ impl Scores for Vec<Score> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_range(&mut self, range: Range<usize>) -> impl Future<Output = Result<&[Score]>> + Send {
|
fn get_range(&mut self, range: Range<usize>) -> impl Future<Output = Result<&[Score]>> + Send {
|
||||||
future::ok(&self[range])
|
future::ok(&self[fit_range_to_len(self.len(), range)])
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find<F: FnMut(&Score) -> bool + Send>(&mut self, mut f: F) -> Result<Option<&Score>> {
|
async fn find<F: FnMut(&Score) -> bool + Send>(&mut self, mut f: F) -> Result<Option<&Score>> {
|
||||||
|
@ -110,6 +110,11 @@ impl Scores for Vec<Score> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn fit_range_to_len(len: usize, range: Range<usize>) -> Range<usize> {
|
||||||
|
range.start.min(len)..range.end.min(len)
|
||||||
|
}
|
||||||
|
|
||||||
/// A scores stream with a fetcher.
|
/// A scores stream with a fetcher.
|
||||||
pub(super) struct ScoresFetcher<T> {
|
pub(super) struct ScoresFetcher<T> {
|
||||||
fetcher: T,
|
fetcher: T,
|
||||||
|
@ -166,7 +171,7 @@ impl<T: FetchScores> Scores for ScoresFetcher<T> {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(&self.scores[range.start.min(self.len())..range.end.min(self.len())])
|
Ok(&self.scores[fit_range_to_len(self.len(), range)])
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find<F: FnMut(&Score) -> bool + Send>(&mut self, mut f: F) -> Result<Option<&Score>> {
|
async fn find<F: FnMut(&Score) -> bool + Send>(&mut self, mut f: F) -> Result<Option<&Score>> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue