Rust-GPU / Rust-GPU/rust-gpu

Union cast failed

Open
#241 11 comments 0 reactions 1 assignee View on GitHub

@simensgreen is already working on this.

Since Apr 23, 2025.

bug
Dominant language
Rust
Stars
3.4k
Forks
125
PR merge metrics
No merged PRs in 30d

Description

Expected Behaviour

Can cast [f32; 5] as Data { a: f32, b: [f32; 3], c: f32 }

Example & Steps To Reproduce
#[repr(C)]
#[derive(Clone, Copy)]
struct Data {
    a: f32,
    b: [f32; 3],
    c: f32,
}

union DataOrArray {
    arr: [f32; 5],
    str: Data,
}

impl DataOrArray {
    fn arr(self) -> [f32; 5] { unsafe { self.arr } }
    fn new(arr: [f32; 5]) -> Self { Self { arr } }
}

in shader:

let dora = DataOrArray::new([0.0, 0.0, 0.0, 0.0, 0.0]);
let arr = dora.arr();

Produces:

error: cannot cast between pointer types
         from `*struct types::Data { a: f32, b: [f32; 3], c: f32 }`
           to `*[f32; 5]`

System Info

  • Rust: rustc 1.84.0-nightly (b19329a37 2024-11-21)
  • OS: MacOS 15.3.2
  • GPU: M2 Max
  • rust-gpu: 698f10ac14b7c952394ac5620004e4e973308902

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.