From a418e4f0c3dcf3aaa26193bec72f016afc98d112 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 12 Apr 2025 18:49:12 +0200 Subject: [PATCH] sp_brightness_grid_load ignore out of range --- cbindgen.toml | 2 +- src/brightness_grid.rs | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cbindgen.toml b/cbindgen.toml index 363d001..221e915 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -24,7 +24,7 @@ sort_by = "Name" [parse] parse_deps = true -include = ["servicepoint"] +include = ["servicepoint", "std"] extra_bindings = ["servicepoint"] [parse.expand] diff --git a/src/brightness_grid.rs b/src/brightness_grid.rs index a283d0a..823a1fd 100644 --- a/src/brightness_grid.rs +++ b/src/brightness_grid.rs @@ -30,6 +30,8 @@ pub unsafe extern "C" fn sp_brightness_grid_new( /// Loads a [BrightnessGrid] with the specified dimensions from the provided data. /// +/// Any out of range values will be set to [Brightness::MAX] or [Brightness::MIN]. +/// /// returns: new [BrightnessGrid] instance, or NULL in case of an error. #[no_mangle] pub unsafe extern "C" fn sp_brightness_grid_load( @@ -38,14 +40,12 @@ pub unsafe extern "C" fn sp_brightness_grid_load( data: ByteSlice, ) -> *mut BrightnessGrid { let data = unsafe { data.as_slice() }; - let grid = match ByteGrid::load(width, height, data) { - None => return std::ptr::null_mut(), - Some(grid) => grid, - }; - if let Ok(grid) = BrightnessGrid::try_from(grid) { - heap_move(grid) - } else { - std::ptr::null_mut() + + match ByteGrid::load(width, height, data) + .map(move |grid| grid.map(Brightness::saturating_from)) + { + None => std::ptr::null_mut(), + Some(grid) => heap_move(grid), } }