Use generic IntoIterator instead of &[foo] slices.

This commit is contained in:
Jordan Petridis 2017-11-26 07:03:21 +02:00
parent 9d0df9de46
commit 866904a687
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
3 changed files with 15 additions and 18 deletions

View File

@ -33,7 +33,7 @@ static URLS: &[(&[u8], &str)] = &[
];
fn index_urls() {
URLS.par_iter()
let feeds: Vec<_> = URLS.par_iter()
.map(|&(buff, url)| {
// Create and insert a Source into db
let s = Source::from_url(url).unwrap();
@ -41,9 +41,9 @@ fn index_urls() {
let chan = rss::Channel::read_from(BufReader::new(buff)).unwrap();
Feed::from_channel_source(chan, s)
})
.for_each(|feed| {
index(&mut [feed]);
});
.collect();
index(feeds);
}
#[bench]

View File

@ -1,5 +1,6 @@
use rayon::prelude::*;
use diesel::prelude::*;
use rayon::iter::IntoParallelIterator;
use diesel::Identifiable;
use rss;
@ -100,13 +101,13 @@ impl Feed {
}
pub fn index_all() -> Result<()> {
let mut feeds = fetch_all()?;
let feeds = fetch_all()?;
index(&mut feeds);
index(feeds);
Ok(())
}
pub fn index(feeds: &mut [Feed]) {
pub fn index<F: IntoParallelIterator<Item = Feed>>(feeds: F) {
feeds.into_par_iter().for_each(|f| {
let e = f.index();
if e.is_err() {
@ -122,7 +123,7 @@ pub fn fetch_all() -> Result<Vec<Feed>> {
Ok(fetch(feeds))
}
pub fn fetch(feeds: Vec<Source>) -> Vec<Feed> {
pub fn fetch<F: IntoParallelIterator<Item = Source>>(feeds: F) -> Vec<Feed> {
let results: Vec<_> = feeds
.into_par_iter()
.filter_map(|x| {
@ -140,13 +141,9 @@ pub fn fetch(feeds: Vec<Source>) -> Vec<Feed> {
#[cfg(test)]
mod tests {
use rss;
use models::Source;
use database::truncate_db;
use std::fs;
use std::io::BufReader;
use database::truncate_db;
use super::*;
@ -196,7 +193,7 @@ mod tests {
truncate_db().unwrap();
let mut feeds: Vec<_> = urls.iter()
let feeds: Vec<_> = urls.iter()
.map(|&(path, url)| {
// Create and insert a Source into db
let s = Source::from_url(url).unwrap();
@ -210,7 +207,7 @@ mod tests {
.collect();
// Index the channels
index(&mut feeds);
index(feeds);
// Assert the index rows equal the controlled results
assert_eq!(dbqueries::get_sources().unwrap().len(), 4);

View File

@ -35,15 +35,15 @@ pub fn refresh_feed(stack: &gtk::Stack, source: Option<Vec<Source>>, delay: Opti
}
let feeds = {
if let Some(mut vec) = source {
if let Some(vec) = source {
Ok(feed::fetch(vec))
} else {
feed::fetch_all()
}
};
if let Ok(mut x) = feeds {
feed::index(&mut x);
if let Ok(x) = feeds {
feed::index(x);
sender.send(true).expect("Couldn't send data to channel");;
glib::idle_add(refresh_podcasts_view);