update word count example
This commit is contained in:
parent
b250ad22ca
commit
823054052d
|
@ -8,7 +8,7 @@ use std::fs::File;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
|
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use pyo3::{py, PyResult, Python, PyModule, ToPyErr};
|
use pyo3::{py, PyResult, Python, PyModule};
|
||||||
|
|
||||||
fn matches(word: &str, search: &str) -> bool {
|
fn matches(word: &str, search: &str) -> bool {
|
||||||
let mut search = search.chars();
|
let mut search = search.chars();
|
||||||
|
@ -52,19 +52,19 @@ fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
|
||||||
|
|
||||||
#[pyfn(m, "search")]
|
#[pyfn(m, "search")]
|
||||||
fn search(py: Python, path: String, search: String) -> PyResult<i32> {
|
fn search(py: Python, path: String, search: String) -> PyResult<i32> {
|
||||||
let mut file = File::open(path).map_err(|e| e.to_pyerr(py))?;
|
let mut file = File::open(path)?;
|
||||||
let mut contents = String::new();
|
let mut contents = String::new();
|
||||||
file.read_to_string(&mut contents).map_err(|e| e.to_pyerr(py))?;
|
file.read_to_string(&mut contents)?;
|
||||||
|
|
||||||
let count = py.allow_threads(move || wc_parallel(&contents, &search));
|
let count = py.allow_threads(move || wc_parallel(&contents, &search));
|
||||||
Ok(count)
|
Ok(count)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pyfn(m, "search_sequential")]
|
#[pyfn(m, "search_sequential")]
|
||||||
fn search_sequential(py: Python, path: String, search: String) -> PyResult<i32> {
|
fn search_sequential(path: String, search: String) -> PyResult<i32> {
|
||||||
let mut file = File::open(path).map_err(|e| e.to_pyerr(py))?;
|
let mut file = File::open(path)?;
|
||||||
let mut contents = String::new();
|
let mut contents = String::new();
|
||||||
file.read_to_string(&mut contents).map_err(|e| e.to_pyerr(py))?;
|
file.read_to_string(&mut contents)?;
|
||||||
Ok(wc_sequential(&contents, &search))
|
Ok(wc_sequential(&contents, &search))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue