From 96bceb5caa7acad13f46a4e978eff2160668513f Mon Sep 17 00:00:00 2001 From: dyedbyte Date: Mon, 28 Feb 2022 16:24:32 +0100 Subject: [PATCH 1/2] Fix out of bound error if material name is missing --- src/lib.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 68eff98..bfffdca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1742,7 +1742,12 @@ where } } Some("usemtl") => { - let mat_name = line[7..].trim().to_owned(); + let mat_name = line.split_once(" ") + .unwrap_or_default() + .1 + .trim() + .to_owned(); + if !mat_name.is_empty() { let new_mat = mat_map.get(&mat_name).cloned(); // As materials are returned per-model, a new material within an object @@ -1982,7 +1987,7 @@ pub fn load_mtl_buf(reader: &mut B) -> MTLLoadResult { /// /// ``` ///async { -/// +/// /// use std::{env, fs::File, io::BufReader}; /// /// let dir = env::current_dir().unwrap(); From c30a1bfa473b0b0db6f5bc68c9326de832c6dd5a Mon Sep 17 00:00:00 2001 From: dyedbyte Date: Tue, 1 Mar 2022 07:38:19 +0100 Subject: [PATCH 2/2] Fix formatting --- src/lib.rs | 81 +++++++++++++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bfffdca..92ee4b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1464,7 +1464,7 @@ fn reorder_data(mesh: &mut Mesh) { #[inline] fn merge_identical_points(points: &mut Vec, indices: &mut Vec) where - [(); size_of::<[f32; N]>()]: , + [(); size_of::<[f32; N]>()]:, { if indices.is_empty() { return; @@ -1742,11 +1742,7 @@ where } } Some("usemtl") => { - let mat_name = line.split_once(" ") - .unwrap_or_default() - .1 - .trim() - .to_owned(); + let mat_name = line.split_once(" ").unwrap_or_default().1.trim().to_owned(); if !mat_name.is_empty() { let new_mat = mat_map.get(&mat_name).cloned(); @@ -1986,47 +1982,46 @@ pub fn load_mtl_buf(reader: &mut B) -> MTLLoadResult { /// and uses a `Cursor` to provide a `BufRead` interface on the buffer. /// /// ``` -///async { +/// async { +/// use std::{env, fs::File, io::BufReader}; /// -/// use std::{env, fs::File, io::BufReader}; +/// let dir = env::current_dir().unwrap(); +/// let mut cornell_box_obj = dir.clone(); +/// cornell_box_obj.push("obj/cornell_box.obj"); +/// let mut cornell_box_file = BufReader::new(File::open(cornell_box_obj.as_path()).unwrap()); /// -/// let dir = env::current_dir().unwrap(); -/// let mut cornell_box_obj = dir.clone(); -/// cornell_box_obj.push("obj/cornell_box.obj"); -/// let mut cornell_box_file = BufReader::new(File::open(cornell_box_obj.as_path()).unwrap()); +/// let m = tobj::load_obj_buf_async( +/// &mut cornell_box_file, +/// &tobj::LoadOptions { +/// triangulate: true, +/// single_index: true, +/// ..Default::default() +/// }, +/// move |p| { +/// let dir_clone = dir.clone(); +/// async move { +/// let mut cornell_box_mtl1 = dir_clone.clone(); +/// cornell_box_mtl1.push("obj/cornell_box.mtl"); /// -/// let m = tobj::load_obj_buf_async( -/// &mut cornell_box_file, -/// &tobj::LoadOptions { -/// triangulate: true, -/// single_index: true, -/// ..Default::default() -/// }, -/// move |p| { -/// let dir_clone = dir.clone(); -/// async move { -/// let mut cornell_box_mtl1 = dir_clone.clone(); -/// cornell_box_mtl1.push("obj/cornell_box.mtl"); +/// let mut cornell_box_mtl2 = dir_clone.clone(); +/// cornell_box_mtl2.push("obj/cornell_box2.mtl"); /// -/// let mut cornell_box_mtl2 = dir_clone.clone(); -/// cornell_box_mtl2.push("obj/cornell_box2.mtl"); -/// -/// match p.as_str() { -/// "cornell_box.mtl" => { -/// let f = File::open(cornell_box_mtl1.as_path()).unwrap(); -/// tobj::load_mtl_buf(&mut BufReader::new(f)) -/// } -/// "cornell_box2.mtl" => { -/// let f = File::open(cornell_box_mtl2.as_path()).unwrap(); -/// tobj::load_mtl_buf(&mut BufReader::new(f)) -/// } -/// _ => unreachable!(), -/// } -/// } -/// }, -/// ) -/// .await; -///}; +/// match p.as_str() { +/// "cornell_box.mtl" => { +/// let f = File::open(cornell_box_mtl1.as_path()).unwrap(); +/// tobj::load_mtl_buf(&mut BufReader::new(f)) +/// } +/// "cornell_box2.mtl" => { +/// let f = File::open(cornell_box_mtl2.as_path()).unwrap(); +/// tobj::load_mtl_buf(&mut BufReader::new(f)) +/// } +/// _ => unreachable!(), +/// } +/// } +/// }, +/// ) +/// .await; +/// }; /// ``` pub async fn load_obj_buf_async( reader: &mut B,