# 错误[E0512]：无法在不同大小或依赖大小的类型之间进行 transmute

- **ID:** `rust/e0512-cannot-transmute-different-sizes`
- **领域:** rust
- **类别:** type_error
- **错误码:** `E0512`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

使用 `std::mem::transmute` 在大小不匹配的类型之间进行转换（例如 `u32` 到 `u64`）。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.64.0 | active | — | — |
| 1.67.0 | active | — | — |
| 1.71.0 | active | — | — |

## 解决方案

1. ```
   使用安全转换：`u64::from(u32_val)` 或 `u32_val as u64` 进行数值拓宽。
   ```
2. ```
   对于结构体，使用 `bytemuck::cast` 或 `unsafe { transmute_copy }` 并显式检查大小。
   ```

## 无效尝试

- **Using `unsafe { transmute::<u32, u64>(val) }` without checking size** — Compiler catches size mismatch; transmute requires same layout and size. (100% 失败率)
- **Adding `#[repr(C)]` to custom types without ensuring alignment** — Doesn't change the size; only affects layout, not total byte count. (90% 失败率)
