From 85e97e16d3233a58e104b4adab5d7d91d4fe94a6 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Mon, 13 May 2019 17:28:17 -0600 Subject: [PATCH] Change vec to BufReader Former-commit-id: 0e91de7976357c96ac58e4f4293c680b9ab98155 --- common/src/assets/mod.rs | 17 +++++++++-------- voxygen/src/ui/mod.rs | 7 ++++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/common/src/assets/mod.rs b/common/src/assets/mod.rs index 685cb5654b..5a98f4e57a 100644 --- a/common/src/assets/mod.rs +++ b/common/src/assets/mod.rs @@ -5,6 +5,7 @@ use std::{ any::Any, collections::HashMap, fs::File, + io::BufReader, io::Read, sync::{Arc, RwLock}, }; @@ -74,13 +75,17 @@ pub trait Asset: Send + Sync + Sized { impl Asset for DynamicImage { fn load(specifier: &str) -> Result { - Ok(image::load_from_memory(load_from_path(specifier)?.as_slice()).unwrap()) + let mut buf = Vec::new(); + load_from_path(specifier)?.read_to_end(&mut buf)?; + Ok(image::load_from_memory(&buf).unwrap()) } } impl Asset for DotVoxData { fn load(specifier: &str) -> Result { - Ok(dot_vox::load_bytes(load_from_path(specifier)?.as_slice()).unwrap()) + let mut buf = Vec::new(); + load_from_path(specifier)?.read_to_end(&mut buf)?; + Ok(dot_vox::load_bytes(&buf).unwrap()) } } @@ -104,13 +109,9 @@ fn try_open_with_path(name: &str) -> Option { .find_map(|ref filename| File::open(filename).ok()) } -pub fn load_from_path(name: &str) -> Result, Error> { +pub fn load_from_path(name: &str) -> Result, Error> { match try_open_with_path(name) { - Some(mut f) => { - let mut content = Vec::::new(); - f.read_to_end(&mut content)?; - Ok(content) - } + Some(mut f) => Ok(BufReader::new(f)), None => Err(Error::NotFound(name.to_owned())), } } diff --git a/voxygen/src/ui/mod.rs b/voxygen/src/ui/mod.rs index 9af2f75d3e..5bf71acd8b 100644 --- a/voxygen/src/ui/mod.rs +++ b/voxygen/src/ui/mod.rs @@ -36,6 +36,7 @@ use conrod_core::{ }; use graphic::Id as GraphicId; use scale::Scale; +use std::io::Read; use std::ops::Range; use std::sync::Arc; use util::{linear_to_srgb, srgb_to_linear}; @@ -73,9 +74,9 @@ impl DrawCommand { pub struct Font(text::Font); impl assets::Asset for Font { fn load(specifier: &str) -> Result { - Ok(Font( - text::Font::from_bytes(assets::load_from_path(specifier)?).unwrap(), - )) + let mut buf = Vec::new(); + assets::load_from_path(specifier)?.read_to_end(&mut buf)?; + Ok(Font(text::Font::from_bytes(buf.clone()).unwrap())) } }