Make read_resource_file param simpler and more idiomatic.

`<P: AsRef<Path>>` is also what `File::open` uses as a generic type for
the parameter.
This commit is contained in:
Corey Farwell 2016-04-16 23:06:59 -04:00
parent 15e76eb6e2
commit c37ab1facd
3 changed files with 6 additions and 8 deletions

View file

@ -112,7 +112,7 @@ impl HSTSList {
} }
pub fn preload_hsts_domains() -> Option<HSTSList> { pub fn preload_hsts_domains() -> Option<HSTSList> {
read_resource_file(&["hsts_preload.json"]).ok().and_then(|bytes| { read_resource_file("hsts_preload.json").ok().and_then(|bytes| {
from_utf8(&bytes).ok().and_then(|hsts_preload_content| { from_utf8(&bytes).ok().and_then(|hsts_preload_content| {
HSTSList::new_from_preload(hsts_preload_content) HSTSList::new_from_preload(hsts_preload_content)
}) })

View file

@ -38,7 +38,7 @@ lazy_static! {
// FIXME: presentational-hints.css should be at author origin with zero specificity. // FIXME: presentational-hints.css should be at author origin with zero specificity.
// (Does it make a difference?) // (Does it make a difference?)
for &filename in &["user-agent.css", "servo.css", "presentational-hints.css"] { for &filename in &["user-agent.css", "servo.css", "presentational-hints.css"] {
match read_resource_file(&[filename]) { match read_resource_file(filename) {
Ok(res) => { Ok(res) => {
let ua_stylesheet = Stylesheet::from_bytes( let ua_stylesheet = Stylesheet::from_bytes(
&res, &res,
@ -65,7 +65,7 @@ lazy_static! {
lazy_static! { lazy_static! {
pub static ref QUIRKS_MODE_STYLESHEET: Stylesheet<ServoSelectorImpl> = { pub static ref QUIRKS_MODE_STYLESHEET: Stylesheet<ServoSelectorImpl> = {
match read_resource_file(&["quirks-mode.css"]) { match read_resource_file("quirks-mode.css") {
Ok(res) => { Ok(res) => {
Stylesheet::from_bytes( Stylesheet::from_bytes(
&res, &res,

View file

@ -5,7 +5,7 @@
use std::env; use std::env;
use std::fs::File; use std::fs::File;
use std::io::{self, Read}; use std::io::{self, Read};
use std::path::PathBuf; use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
lazy_static! { lazy_static! {
@ -57,11 +57,9 @@ pub fn resources_dir_path() -> PathBuf {
path path
} }
pub fn read_resource_file(relative_path_components: &[&str]) -> io::Result<Vec<u8>> { pub fn read_resource_file<P: AsRef<Path>>(relative_path: P) -> io::Result<Vec<u8>> {
let mut path = resources_dir_path(); let mut path = resources_dir_path();
for component in relative_path_components { path.push(relative_path);
path.push(component);
}
let mut file = try!(File::open(&path)); let mut file = try!(File::open(&path));
let mut data = Vec::new(); let mut data = Vec::new();
try!(file.read_to_end(&mut data)); try!(file.read_to_end(&mut data));