use crate::{heap_drop, heap_move, heap_move_nonnull, ByteSlice}; use servicepoint::{Cp437Grid, DataRef, Grid}; use std::ptr::NonNull; /// Creates a new [Cp437Grid] with the specified dimensions. /// /// returns: [Cp437Grid] initialized to 0. #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_new( width: usize, height: usize, ) -> NonNull { heap_move_nonnull(Cp437Grid::new(width, height)) } /// Loads a [Cp437Grid] with the specified dimensions from the provided data. #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_load( width: usize, height: usize, data: ByteSlice, ) -> *mut Cp437Grid { let data = unsafe { data.as_slice() }; let grid = Cp437Grid::load(width, height, data); if let Some(grid) = grid { heap_move(grid) } else { std::ptr::null_mut() } } /// Clones a [Cp437Grid]. #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_clone( cp437_grid: NonNull, ) -> NonNull { heap_move_nonnull(unsafe { cp437_grid.as_ref().clone() }) } /// Deallocates a [Cp437Grid]. #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: NonNull) { unsafe { heap_drop(cp437_grid) } } /// Gets the current value at the specified position. /// /// # Arguments /// /// - `cp437_grid`: instance to read from /// - `x` and `y`: position of the cell to read /// /// # Panics /// /// - when accessing `x` or `y` out of bounds #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_get( cp437_grid: NonNull, x: usize, y: usize, ) -> u8 { unsafe { cp437_grid.as_ref().get(x, y) } } /// Sets the value of the specified position in the [Cp437Grid]. /// /// # Arguments /// /// - `cp437_grid`: instance to write to /// - `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 #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_set( cp437_grid: NonNull, x: usize, y: usize, value: u8, ) { unsafe { (*cp437_grid.as_ptr()).set(x, y, value) }; } /// Sets the value of all cells in the [Cp437Grid]. /// /// # Arguments /// /// - `cp437_grid`: instance to write to /// - `value`: the value to set all cells to #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_fill( cp437_grid: NonNull, value: u8, ) { unsafe { (*cp437_grid.as_ptr()).fill(value) }; } /// Gets the width of the [Cp437Grid] instance. /// /// # Arguments /// /// - `cp437_grid`: instance to read from #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_width( cp437_grid: NonNull, ) -> usize { unsafe { cp437_grid.as_ref().width() } } /// Gets the height of the [Cp437Grid] instance. /// /// # Arguments /// /// - `cp437_grid`: instance to read from #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_height( cp437_grid: NonNull, ) -> usize { unsafe { cp437_grid.as_ref().height() } } /// Gets an unsafe reference to the data of the [Cp437Grid] instance. /// /// The returned memory is valid for the lifetime of the grid. #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref( cp437_grid: NonNull, ) -> ByteSlice { unsafe { ByteSlice::from_slice((*cp437_grid.as_ptr()).data_ref_mut()) } }