How to safely `include_bytes!()` for `&[T]`
⚓ Rust 📅 2025-07-09 👤 surdeus 👁️ 23Hi,
I'm working on a project in which I'm creating several const objects with large arrays:
pub const ENC_EMB_GRU_GRU_WEIGHT_HH_L0_DATA: &[f32] = &[ ... ];
pub const ENC_EMB_GRU_GRU_WEIGHT_HH_L0: Tensor<f32> = Tensor::from_slice_const([768, 256], ENC_EMB_GRU_GRU_WEIGHT_HH_L0_DATA);
The files where I have these constants defined grind Rust Analyzer to a halt. I also had issues with this in the past, where Clippy would error out (no longer an issue in latest stable somehow):
error: maximum number of nodes exceeded in constant params::ENC_EMB_GRU_GRU_WEIGHT_HH_L0
--> /path/to/project/target/debug/build/<hash>/out/model_parameters.rs:192:1
|
192 | pub const ENC_EMB_GRU_GRU_WEIGHT_HH_L0: Tensor<f32> = Tensor::from_slice_const([768, 256], ENC_EMB_GRU_GRU_WEIGHT_HH_L0_DATA);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Due to these issues, I'm looking into alternative ways of defining these constants at compile time. I thought about using include_bytes!() instead of explicitly defining every value of every array in the source file:
pub const ENC_EMB_GRU_GRU_WEIGHT_HH_L0_DATA: &[f32] = include_bytes!(enc_emb_gru_gru_weight_hh_l0_raw_data);
pub const ENC_EMB_GRU_GRU_WEIGHT_HH_L0: Tensor<f32> = Tensor::from_slice_const([768, 256], ENC_EMB_GRU_GRU_WEIGHT_HH_L0_DATA);
I have two questions:
- Would this make Rust/Rust Analyzer happier?
- How can I safely get a
&[T](for now I'm using onlyf32) frominclude_bytes!()? It returns a&'static [u8], but I'm not sure how can I guarantee that the binary file will respectf32's alignment and so on.
Or, alternatively, is there a better way to go about this?
2 posts - 2 participants
🏷️ rust_feed