Fake allocation in else branch and multiple if: a better way?
⚓ Rust 📅 2025-12-19 👤 surdeus 👁️ 6Yesterday I had to write a little piece of code that uses a byte buffer that can be "raw" or run-length encoded. I wanted to keep the rest of the code independent and I used a pattern that involves a "fake" allocation in the case of the raw buffer:
let data = if tip.compressed {
decode_rle(&abr.buffer, tip.data.start, w, h).unwrap()
} else {
Vec::new()
};
let data = if tip.compressed {
&data
}
else {
&abr.buffer[tip.data.clone()]
};
// Use data as &[u8] from here...
Obviously it work but is there a better way to do this? I don't particularly like the repeated if and the "fake" allocation of an unused vec.
7 posts - 4 participants
🏷️ Rust_feed