Skip to content

Commit a4dc348

Browse files
authored
Use bytemuck for slice cast (#36)
Use the `bytemuck` package for slice casting to byte, instead of rolling some manual unsafe one. Fixes #29
1 parent 249b1ee commit a4dc348

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

bin/weldr/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ serde_json = "1.0"
3535
serde_repr = "0.1"
3636
ordered-float = "2.0"
3737
log = "0.4"
38-
atty = "0.2"
38+
atty = "0.2"
39+
bytemuck = { version = "1", features = ["derive"] }

bin/weldr/src/convert.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Action to convert an LDraw file to another format.
22
33
use crate::{
4-
as_u8_slice,
54
error::{Error, Utf8Error},
65
gltf, Action, App, DiskResolver, GeometryCache,
76
};
@@ -252,8 +251,9 @@ impl ConvertCommand {
252251
gltf: &mut gltf::Gltf,
253252
buffer: &mut Vec<u8>,
254253
) {
254+
// TODO: glTF is LE only; should convert on BE platforms
255255
let vertices = &geometry_cache.vertices;
256-
let vertices_bytes: &[u8] = unsafe { as_u8_slice(vertices) };
256+
let vertices_bytes: &[u8] = bytemuck::cast_slice(&vertices[..]);
257257

258258
// TODO: Line indices?
259259
let vertex_buffer_view_index = gltf.buffer_views.len() as u32;
@@ -293,16 +293,19 @@ impl ConvertCommand {
293293
let attributes = HashMap::from([("POSITION".to_string(), gltf.accessors.len() as u32)]);
294294
gltf.accessors.push(vertex_accessor);
295295

296-
// TODO: Use bytemuck instead.
297-
let triangle_indices = &geometry_cache.triangle_indices;
298-
let triangle_indices_bytes: &[u8] = unsafe { as_u8_slice(triangle_indices) };
296+
// TODO: glTF is LE only; should convert on BE platforms
297+
let triangle_indices_bytes: &[u8] =
298+
bytemuck::cast_slice(&geometry_cache.triangle_indices[..]);
299299

300+
let byte_offset = buffer.len() as u32;
301+
let byte_length = triangle_indices_bytes.len() as u32;
300302
let index_buffer_view_index = gltf.buffer_views.len() as u32;
303+
301304
gltf.buffer_views.push(gltf::BufferView {
302305
name: Some("index_buffer".to_string()),
303306
buffer_index: 0,
304-
byte_length: triangle_indices_bytes.len() as u32,
305-
byte_offset: buffer.len() as u32,
307+
byte_length,
308+
byte_offset,
306309
byte_stride: None,
307310
target: Some(gltf::BufferTarget::ElementArrayBuffer as u32),
308311
});

bin/weldr/src/weldr.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,6 @@ impl GeometryCache {
337337
}
338338
}
339339

340-
/// Transform a slice of something sized into a slice of u8, for binary writing.
341-
unsafe fn as_u8_slice<T: Sized>(p: &[T]) -> &[u8] {
342-
::std::slice::from_raw_parts(
343-
p.as_ptr() as *const u8,
344-
::std::mem::size_of::<T>() * p.len(),
345-
)
346-
}
347-
348340
#[cfg(not(target_os = "windows"))]
349341
fn is_tty() -> bool {
350342
atty::is(atty::Stream::Stderr)
@@ -400,7 +392,7 @@ mod tests {
400392
fn test_as_u8_slice() {
401393
assert_eq!(12, std::mem::size_of::<Vec3>());
402394
let v = vec![Vec3::new(1.0, 2.0, 4.0), Vec3::new(1.0, 2.0, 4.0)];
403-
let b: &[u8] = unsafe { as_u8_slice(&v[..]) };
395+
let b: &[u8] = bytemuck::cast_slice(&v[..]);
404396
assert_eq!(24, b.len());
405397
}
406398

lib/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ path = "src/lib.rs"
2828

2929
[dependencies]
3030
nom = "7.1.3"
31-
glam = "0.23.0"
31+
glam = { version = "0.23.0", features = ["bytemuck"] }
3232
log = "0.4"
3333
base64 = "0.21.0"

0 commit comments

Comments
 (0)