mirror of
https://github.com/italicsjenga/rust_minifb.git
synced 2025-01-11 03:21:32 +11:00
51bdfcc5ca
* Moved to Rust edition 2018 * Changed redox code as well * Undo typo * Another correction
24 lines
848 B
Rust
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(())
|
|
}
|
|
}
|