Skip to content

Commit 5b52ffa

Browse files
Some super minor tweaks to loading
- Since Rust 1.76, use offset_of! macro from std lib - Update comments and control-flow formatting in 'lines_escaped' - Make 'trim_line_comment' a general-purpose function in preparation for parsing different types of model files
1 parent 5eb9786 commit 5b52ffa

File tree

3 files changed

+30
-43
lines changed

3 files changed

+30
-43
lines changed

gloog/src/loader/mod.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,35 @@ fn lines_escaped<R: BufRead>(reader: R) -> impl Iterator<Item = io::Result<(Line
2626
let mut processed: Option<String> = None;
2727

2828
let escaped_line = loop {
29-
raw_line_num += 1; // Fine to increment first; line numbers start at 1.
29+
raw_line_num += 1; // Fine to increment before grabbing from `raw_lines` iterator; line numbers start at 1.
3030

3131
let raw_line = match raw_lines.next() {
32+
// Next line in file is just regular text.
3233
Some(Ok(text)) => text,
33-
// If we hit an error, short-circuit this function. Can be caught by the consumer.
34+
// Failed to read next line; throw the error up to the consumer.
3435
Some(Err(err)) => return Some(Err(err)),
35-
None => {
36-
// If the last line of the file has a backslash on it, we might run out of lines while we're
37-
// currently holding onto processed line(s). We
38-
match processed {
39-
Some(text) => break text,
40-
None => return None,
41-
};
42-
},
36+
// There are no more lines in the file. If `processed` is still Some, that means that the previous line
37+
// ended with a backslash, and so we _should_ have found another line here. Otherwise, we can just
38+
// return `None` and stop iteration.
39+
None if processed.is_some() => return Some(Err(io::ErrorKind::UnexpectedEof.into())),
40+
None => return None,
4341
};
4442

45-
// If we have any processed text currently pending, append this line. Otherwise, our new pending text is
46-
// just the freshly read value on its own.
43+
// If we have any processed text currently pending, append this line to it. Otherwise, our new pending text
44+
// is just the freshly read value on its own.
4745
let mut pending = match processed {
4846
Some(prev) => prev + &raw_line,
4947
None => raw_line,
5048
};
5149

52-
// Then, check if the newly appended-to
50+
// If our collection of lines still ends with a backslash, trim it off and keep looping.
5351
if pending.ends_with('\\') {
54-
// If our escaped text still ends with a backslash, trim off that backslash and keep looping. We are
55-
// safe to truncate by -1 because we now the last character is a plain ASCII backslash.
52+
// Safe to truncate by 1 byte since we know the last character is a plain ASCII backslash
5653
pending.truncate(pending.len() - 1);
5754
processed = Some(pending);
5855
num_escaped += 1;
5956
} else {
60-
// Otherwise, what we've got is the current line.
57+
// Otherwise, what we've got is the final line.
6158
break pending;
6259
}
6360
};
@@ -70,6 +67,15 @@ fn lines_escaped<R: BufRead>(reader: R) -> impl Iterator<Item = io::Result<(Line
7067
}
7168

7269

70+
/// Grab everything in a line up to the first line-comment character(s). Also trims the start and end of the string.
71+
fn trim_line_comment<'a>(line: &'a str, comment: &str) -> &'a str {
72+
match line.find(comment) {
73+
Some(i) => line[0..i].trim(),
74+
None => line[0..].trim(),
75+
}
76+
}
77+
78+
7379
/// Formats a range of line numbers as either `line N` or `lines N to M` if the range is longer than one line.
7480
fn fmt_line_range(range: &LineRange) -> String {
7581
// Range is exclusive, hence - 1

gloog/src/loader/obj/mod.rs

+5-24
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use log::{debug, info, log, trace};
3030

3131
use self::error::{ObjLoadError, ObjResult};
3232
use crate::loader::obj::mtl::parse_mtl_file;
33-
use crate::loader::{lines_escaped, LineRange};
33+
use crate::loader::{lines_escaped, trim_line_comment, LineRange};
3434

3535

3636
// cspell:words curv interp stech ctech scrv cstype bmat newmtl usemtl mtllib maplib usemap
@@ -125,22 +125,11 @@ pub struct ObjVertex {
125125
tex_coord: Vec2,
126126
}
127127

128-
macro_rules! vertex_offset {
129-
($field:ident) => {{
130-
// We don't really need to care if the memory we're getting pointers to has been initialized properly, so we
131-
// just allocate some zeroes.
132-
let vert = unsafe { std::mem::MaybeUninit::<ObjVertex>::zeroed().assume_init() };
133-
let base = std::ptr::addr_of!(vert) as *const u8;
134-
let field = std::ptr::addr_of!(vert.$field) as *const u8;
135-
unsafe { field.offset_from(base) as usize }
136-
}};
137-
}
138-
139128
impl ObjVertex {
140129
pub const STRIDE: isize = std::mem::size_of::<ObjVertex>() as isize;
141-
pub const OFFSET_POSITION: usize = vertex_offset!(position);
142-
pub const OFFSET_TEX_COORD: usize = vertex_offset!(tex_coord);
143-
pub const OFFSET_NORMAL: usize = vertex_offset!(normal);
130+
pub const OFFSET_POSITION: usize = std::mem::offset_of!(ObjVertex, position);
131+
pub const OFFSET_TEX_COORD: usize = std::mem::offset_of!(ObjVertex, tex_coord);
132+
pub const OFFSET_NORMAL: usize = std::mem::offset_of!(ObjVertex, normal);
144133
}
145134

146135
/// Used to configure uniforms before executing draw call.
@@ -195,14 +184,6 @@ impl Debug for ObjMaterial {
195184
}
196185

197186

198-
/// Grab everything in a line up to the first `#` (and also trim the starts and ends).
199-
fn trim_comment(line: &str) -> &str {
200-
match line.find('#') {
201-
Some(i) => line[0..i].trim(),
202-
None => line[0..].trim(),
203-
}
204-
}
205-
206187
impl ObjModel {
207188
pub fn from_file(
208189
path: impl AsRef<Path>,
@@ -239,7 +220,7 @@ impl ObjModel {
239220

240221
for line_result in lines_escaped(file) {
241222
let (line_nums, line) = line_result?;
242-
let line = trim_comment(&line);
223+
let line = trim_line_comment(&line, "#");
243224

244225
// If our line is empty after removing the comment, we can ignore it
245226
if line.len() == 0 {

gloog/src/loader/obj/mtl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use image::ImageResult;
1010
use log::{debug, info, warn};
1111

1212
use super::error::{MtlLoadError, MtlResult};
13-
use super::{read_ws_verts, trim_comment, CachedImage, ObjMaterial};
14-
use crate::loader::{lines_escaped, LineRange};
13+
use super::{read_ws_verts, CachedImage, ObjMaterial};
14+
use crate::loader::{lines_escaped, trim_line_comment, LineRange};
1515

1616
// cspell:words newmtl usemtl
1717

@@ -49,7 +49,7 @@ pub fn parse_mtl_file(
4949
Err(err) => return Err(MtlLoadError::IOReadError(err)),
5050
};
5151

52-
let line = trim_comment(&line);
52+
let line = trim_line_comment(&line, "#");
5353
if line.len() == 0 {
5454
continue;
5555
}

0 commit comments

Comments
 (0)