Skip to content

Commit

Permalink
Use bytemuck::must_cast_* when possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Aug 16, 2024
1 parent dbbdd86 commit ff1da68
Show file tree
Hide file tree
Showing 15 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ async_fn_traits = "0.1.1"
base64 = { version = "0.22.1", default-features = false }
bitflags = { version = "2.6.0", default-features = false }
bitvec = { version = "1.0.0", default-features = false, features = ["alloc"] }
# Note that this excludes the "derive" feature but some crates need it.
bytemuck = { version = "1.13.1", default-features = false }
bytemuck = { version = "1.17.0", default-features = false, features = ["derive", "must_cast"] }
cfg-if = { version = "1.0.0" }
# When upgrading clap, beware text output changes causing integration tests to fail.
clap = { version = "4.2.4", default-features = false, features = ["cargo", "deprecated", "derive", "help", "std", "suggestions", "usage", "wrap_help"] }
Expand Down
8 changes: 4 additions & 4 deletions all-is-cubes-base/src/math/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,25 +271,25 @@ mod conversion {
impl AsRef<[GridCoordinate; 3]> for Cube {
#[inline]
fn as_ref(&self) -> &[GridCoordinate; 3] {
bytemuck::cast_ref(self)
bytemuck::must_cast_ref(self)
}
}
impl AsMut<[GridCoordinate; 3]> for Cube {
#[inline]
fn as_mut(&mut self) -> &mut [GridCoordinate; 3] {
bytemuck::cast_mut(self)
bytemuck::must_cast_mut(self)
}
}
impl core::borrow::Borrow<[GridCoordinate; 3]> for Cube {
#[inline]
fn borrow(&self) -> &[GridCoordinate; 3] {
bytemuck::cast_ref(self)
bytemuck::must_cast_ref(self)
}
}
impl core::borrow::BorrowMut<[GridCoordinate; 3]> for Cube {
#[inline]
fn borrow_mut(&mut self) -> &mut [GridCoordinate; 3] {
bytemuck::cast_mut(self)
bytemuck::must_cast_mut(self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-gpu/src/in_wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ impl<I: time::Instant> EverythingRenderer<I> {
bwp.reborrow(),
&wgpu::util::BufferInitDescriptor {
label: Some("EverythingRenderer::lines_buffer"),
contents: bytemuck::cast_slice::<WgpuLinesVertex, u8>(&v),
contents: bytemuck::must_cast_slice::<WgpuLinesVertex, u8>(&v),
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
},
);
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-gpu/src/in_wgpu/frame_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<In, Out: Copy + Default + bytemuck::Pod> DrawableTexture<In, Out> {
},
aspect: wgpu::TextureAspect::All,
},
bytemuck::cast_slice(
bytemuck::must_cast_slice::<Out, u8>(
&self.local_buffer.data()[full_width as usize
* dirty_rect.top_left.y as usize
+ dirty_rect.top_left.x as usize..],
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-gpu/src/in_wgpu/glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn write_texture_by_aab<T: Pod, U>(
origin: point_to_origin(region.min),
aspect: wgpu::TextureAspect::All,
},
bytemuck::cast_slice::<T, u8>(data),
bytemuck::must_cast_slice::<T, u8>(data),
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(size_of::<T>() as u32 * size.width),
Expand Down
1 change: 1 addition & 0 deletions all-is-cubes-gpu/src/in_wgpu/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ where
let mut texel_vector: Vec<C> = Vec::with_capacity(element_count);
for row in 0..dimensions.height {
let byte_start_of_row = padded_bytes_per_row * row;
// TODO: this cast_slice() could fail if `C`’s alignment is higher than the buffer.
texel_vector.extend(bytemuck::cast_slice::<u8, C>(
&mapped[byte_start_of_row as usize..][..dense_bytes_per_row as usize],
));
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-gpu/src/in_wgpu/skybox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn compute_skybox(queue: &wgpu::Queue, texture: &wgpu::Texture, sky: &Sky) {

queue.write_texture(
texture.as_image_copy(),
bytemuck::cast_slice::<[Component; CHANNELS], u8>(&data[..]),
bytemuck::must_cast_slice::<[Component; CHANNELS], u8>(&data[..]),
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(resolution * (size_of::<Component>() * CHANNELS) as u32),
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-gpu/src/in_wgpu/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ impl<I: time::Instant> SpaceRenderer<I> {
queue.write_buffer(
buffer,
0,
bytemuck::cast_slice::<WgpuInstanceData, u8>(instance_data),
bytemuck::must_cast_slice::<WgpuInstanceData, u8>(instance_data),
);
}
}
Expand Down Expand Up @@ -919,7 +919,7 @@ fn update_chunk_buffers<I: time::Instant>(
}

let new_vertices_data: &[u8] =
bytemuck::cast_slice::<WgpuBlockVertex, u8>(update.mesh.vertices());
bytemuck::must_cast_slice::<WgpuBlockVertex, u8>(update.mesh.vertices());
// TODO: assert INDEX_FORMAT matches this type
let new_indices: IndexSlice<'_> = update.mesh.indices();

Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-mesh/src/index_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ impl<'a> IndexSlice<'a> {
#[inline]
pub fn as_bytes(&self) -> &'a [u8] {
match self {
IndexSlice::U16(slice) => bytemuck::cast_slice::<u16, u8>(slice),
IndexSlice::U32(slice) => bytemuck::cast_slice::<u32, u8>(slice),
IndexSlice::U16(slice) => bytemuck::must_cast_slice::<u16, u8>(slice),
IndexSlice::U32(slice) => bytemuck::must_cast_slice::<u32, u8>(slice),
}
}

Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-port/src/gltf/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ where
return None;
}

let vertex_bytes = bytemuck::cast_slice::<GltfVertex, u8>(mesh.vertices());
let vertex_bytes = bytemuck::must_cast_slice::<GltfVertex, u8>(mesh.vertices());
let index_type = match mesh.indices() {
IndexSlice::U16(_) => gltf_json::accessor::ComponentType::U16,
IndexSlice::U32(_) => gltf_json::accessor::ComponentType::U32,
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion all-is-cubes/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn write_light_propagation_chart(chart: Vec<chart_schema::Steps>) {
fn writemuck<T: bytemuck::NoUninit>(out_relative_path: &Path, data: &[T]) {
assert!(out_relative_path.is_relative());
let path = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join(out_relative_path);
if let Err(e) = fs::write(&path, bytemuck::cast_slice::<T, u8>(data)) {
if let Err(e) = fs::write(&path, bytemuck::must_cast_slice::<T, u8>(data)) {
panic!(
"failed to write generated data to {path}: {e}",
path = path.display()
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes/src/content/load_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'a> PngAdapter<'a> {
// Note: this could be FnMut, at the price of forcing all callers to write `&mut`
pixel_function: &dyn Fn(Srgba) -> VoxelBrush<'brush>,
) -> Self {
// Group into whole pixels
// Group into whole pixels.
let rgba_image_data = bytemuck::cast_slice::<u8, [u8; 4]>(data);

let mut color_map: HashMap<Srgba, VoxelBrush<'a>> = HashMap::new();
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes/src/save/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<'a, T: bytemuck::NoUninit> serde::Serialize for GzSerde<'a, T> {
where
S: serde::Serializer,
{
let uncompressed_bytes = bytemuck::cast_slice::<T, u8>(self.0.as_ref());
let uncompressed_bytes = bytemuck::must_cast_slice::<T, u8>(self.0.as_ref());

let compression = flate2::Compression::fast();

Expand Down

0 comments on commit ff1da68

Please sign in to comment.