rust_minifb/src/buffer_helper.rs
Antonino Siena 51bdfcc5ca Moved codebase to edition 2018 (#127)
* Moved to Rust edition 2018

* Changed redox code as well

* Undo typo

* Another correction
2019-12-30 19:34:27 +01:00

24 lines
848 B
Rust

use crate::error::Error;
use crate::Result;
pub fn check_buffer_size(
buffer_width: usize,
buffer_height: usize,
buffer_stride: usize,
buffer: &[u32],
) -> Result<()> {
let width = usize::max(buffer_width, buffer_stride);
let buffer_size = buffer.len() * 4; // len is the number of entries so * 4 as we want bytes
let required_buffer_size = width * buffer_height * 4; // * 4 for 32-bit buffer
if buffer_size < required_buffer_size {
let err = format!(
"Update failed because input buffer is too small. Required size for {} ({} stride) x {} buffer is {}
bytes but the size of the input buffer has the size {} bytes",
buffer_width, buffer_stride, buffer_height, required_buffer_size, buffer_size);
Err(Error::UpdateFailed(err))
} else {
Ok(())
}
}