use crate::{ containers::{derive_get_width_height, wrap_container, ByteSlice}, macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; wrap_container!(CharGrid); derive_get_width_height!(CharGrid); wrap_functions!(associate CharGrid; /// Creates a new [CharGrid] with the specified dimensions. /// /// returns: [CharGrid] initialized to 0. /// /// # Examples /// /// ```C /// CharGrid grid = sp_char_grid_new(4, 3); /// sp_char_grid_fill(grid, '?'); /// sp_char_grid_set(grid, 0, 0, '!'); /// sp_char_grid_free(grid); /// ``` fn new( width: usize, height: usize, ) -> NonNull { heap_move_nonnull(CharGrid::new(width, height)) } /// Loads a [CharGrid] with the specified dimensions from the provided data. /// /// returns: new CharGrid or NULL in case of an error fn load( width: usize, height: usize, data: ByteSlice, ) -> *mut CharGrid { let data = unsafe { data.as_slice() }; heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) } /// Creates a [CharGridCommand] and immediately turns that into a [Packet]. /// /// The provided [CharGrid] gets consumed. /// /// Returns NULL in case of an error. fn try_into_packet( grid: NonNull, x: usize, y: usize, ) -> *mut Packet { let grid = unsafe { heap_remove(grid) }; heap_move_ok(Packet::try_from(CharGridCommand { grid, origin: Origin::new(x, y), })) } ); wrap_methods!(CharGrid; /// Returns the current value at the specified position. /// /// # Arguments /// /// - `x` and `y`: position of the cell to read /// /// # Panics /// /// - when accessing `x` or `y` out of bounds ref fn get(instance, x: usize, y: usize) -> u32 { instance.get(x, y) as u32 }; /// Sets the value of the specified position in the grid. /// /// # Arguments /// /// - `x` and `y`: position of the cell /// - `value`: the value to write to the cell /// /// returns: old value of the cell /// /// # Panics /// /// - when accessing `x` or `y` out of bounds /// - when providing values that cannot be converted to Rust's `char`. mut fn set(instance, x: usize, y: usize, value: u32) { instance.set(x, y, char::from_u32(value).unwrap()) }; /// Sets the value of all cells in the grid. /// /// # Arguments /// /// - `value`: the value to set all cells to /// - when providing values that cannot be converted to Rust's `char`. mut fn fill(instance, value: u32) { instance.fill(char::from_u32(value).unwrap()) }; );