move containers to own mod

This commit is contained in:
Vinzenz Schroeter 2025-05-07 08:43:13 +02:00
parent e7cad5b5a3
commit cf6e6385ec
11 changed files with 28 additions and 17 deletions

214
src/containers/bitmap.rs Normal file
View file

@ -0,0 +1,214 @@
use crate::{
containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull,
heap_move_ok, heap_move_some, heap_remove,
};
use servicepoint::{
Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid,
Origin, Packet,
};
use std::ptr::NonNull;
/// Creates a new [Bitmap] with the specified dimensions.
///
/// # Arguments
///
/// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction
///
/// returns: [Bitmap] initialized to all pixels off, or NULL in case of an error.
///
/// # Errors
///
/// In the following cases, this function will return NULL:
///
/// - when the width is not dividable by 8
///
/// # Examples
///
/// ```C
/// Cp437Grid grid = sp_bitmap_new(8, 3);
/// sp_bitmap_fill(grid, true);
/// sp_bitmap_set(grid, 0, 0, false);
/// sp_bitmap_free(grid);
/// ```
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_new(
width: usize,
height: usize,
) -> *mut Bitmap {
heap_move_some(Bitmap::new(width, height))
}
/// Creates a new [Bitmap] with a size matching the screen.
///
/// returns: [Bitmap] initialized to all pixels off.
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_new_max_sized() -> NonNull<Bitmap> {
heap_move_nonnull(Bitmap::max_sized())
}
/// Loads a [Bitmap] with the specified dimensions from the provided data.
///
/// # Arguments
///
/// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction
///
/// returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error.
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_load(
width: usize,
height: usize,
data: ByteSlice,
) -> *mut Bitmap {
let data = unsafe { data.as_slice() };
heap_move_ok(Bitmap::load(width, height, data))
}
/// Tries to convert the BitVec to a Bitmap.
///
/// The provided BitVec gets consumed.
///
/// Returns NULL in case of error.
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_from_bitvec(
width: usize,
bitvec: NonNull<DisplayBitVec>,
) -> *mut Bitmap {
let bitvec = unsafe { heap_remove(bitvec) };
heap_move_ok(Bitmap::from_bitvec(width, bitvec))
}
/// Clones a [Bitmap].
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_clone(
bitmap: NonNull<Bitmap>,
) -> NonNull<Bitmap> {
unsafe { heap_clone(bitmap) }
}
/// Deallocates a [Bitmap].
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_free(bitmap: NonNull<Bitmap>) {
unsafe { heap_drop(bitmap) }
}
/// Gets the current value at the specified position in the [Bitmap].
///
/// # Arguments
///
/// - `bitmap`: 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_bitmap_get(
bitmap: NonNull<Bitmap>,
x: usize,
y: usize,
) -> bool {
unsafe { bitmap.as_ref().get(x, y) }
}
/// Sets the value of the specified position in the [Bitmap].
///
/// # Arguments
///
/// - `bitmap`: instance to write to
/// - `x` and `y`: position of the cell
/// - `value`: the value to write to the cell
///
/// # Panics
///
/// - when accessing `x` or `y` out of bounds
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_set(
bitmap: NonNull<Bitmap>,
x: usize,
y: usize,
value: bool,
) {
unsafe { (*bitmap.as_ptr()).set(x, y, value) };
}
/// Sets the state of all pixels in the [Bitmap].
///
/// # Arguments
///
/// - `bitmap`: instance to write to
/// - `value`: the value to set all pixels to
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_fill(bitmap: NonNull<Bitmap>, value: bool) {
unsafe { (*bitmap.as_ptr()).fill(value) };
}
/// Gets the width in pixels of the [Bitmap] instance.
///
/// # Arguments
///
/// - `bitmap`: instance to read from
///
/// # Panics
///
/// - when `bitmap` is NULL
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `bitmap` points to a valid [Bitmap]
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_width(bitmap: NonNull<Bitmap>) -> usize {
unsafe { bitmap.as_ref().width() }
}
/// Gets the height in pixels of the [Bitmap] instance.
///
/// # Arguments
///
/// - `bitmap`: instance to read from
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_height(bitmap: NonNull<Bitmap>) -> usize {
unsafe { bitmap.as_ref().height() }
}
/// Gets an unsafe reference to the data of the [Bitmap] instance.
///
/// The returned memory is valid for the lifetime of the bitmap.
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_unsafe_data_ref(
mut bitmap: NonNull<Bitmap>,
) -> ByteSlice {
unsafe { ByteSlice::from_slice(bitmap.as_mut().data_ref_mut()) }
}
/// Consumes the Bitmap and returns the contained BitVec
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_into_bitvec(
bitmap: NonNull<Bitmap>,
) -> NonNull<DisplayBitVec> {
let bitmap = unsafe { heap_remove(bitmap) };
heap_move_nonnull(bitmap.into())
}
/// Creates a [BitmapCommand] and immediately turns that into a [Packet].
///
/// The provided [Bitmap] gets consumed.
///
/// Returns NULL in case of an error.
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_into_packet(
bitmap: NonNull<Bitmap>,
x: usize,
y: usize,
compression: CompressionCode,
) -> *mut Packet {
let bitmap = unsafe { heap_remove(bitmap) };
heap_move_ok(Packet::try_from(BitmapCommand {
bitmap,
origin: Origin::new(x, y),
compression,
}))
}

162
src/containers/bitvec.rs Normal file
View file

@ -0,0 +1,162 @@
use crate::{
containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull,
heap_move_ok, heap_remove,
};
use servicepoint::{
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet,
};
use std::ptr::NonNull;
/// Creates a new [DisplayBitVec] instance.
///
/// # Arguments
///
/// - `size`: size in bits.
///
/// returns: [DisplayBitVec] with all bits set to false.
///
/// # Panics
///
/// - when `size` is not divisible by 8.
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_new(size: usize) -> NonNull<DisplayBitVec> {
heap_move_nonnull(DisplayBitVec::repeat(false, size))
}
/// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance.
///
/// returns: [DisplayBitVec] instance containing data.
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_load(
data: ByteSlice,
) -> NonNull<DisplayBitVec> {
let data = unsafe { data.as_slice() };
heap_move_nonnull(DisplayBitVec::from_slice(data))
}
/// Clones a [DisplayBitVec].
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_clone(
bit_vec: NonNull<DisplayBitVec>,
) -> NonNull<DisplayBitVec> {
unsafe { heap_clone(bit_vec) }
}
/// Deallocates a [DisplayBitVec].
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_free(bit_vec: NonNull<DisplayBitVec>) {
unsafe { heap_drop(bit_vec) }
}
/// Gets the value of a bit from the [DisplayBitVec].
///
/// # Arguments
///
/// - `bit_vec`: instance to read from
/// - `index`: the bit index to read
///
/// returns: value of the bit
///
/// # Panics
///
/// - when accessing `index` out of bounds
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_get(
bit_vec: NonNull<DisplayBitVec>,
index: usize,
) -> bool {
unsafe { *bit_vec.as_ref().get(index).unwrap() }
}
/// Sets the value of a bit in the [DisplayBitVec].
///
/// # Arguments
///
/// - `bit_vec`: instance to write to
/// - `index`: the bit index to edit
/// - `value`: the value to set the bit to
///
/// # Panics
///
/// - when accessing `index` out of bounds
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_set(
bit_vec: NonNull<DisplayBitVec>,
index: usize,
value: bool,
) {
unsafe { (*bit_vec.as_ptr()).set(index, value) }
}
/// Sets the value of all bits in the [DisplayBitVec].
///
/// # Arguments
///
/// - `bit_vec`: instance to write to
/// - `value`: the value to set all bits to
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_fill(
bit_vec: NonNull<DisplayBitVec>,
value: bool,
) {
unsafe { (*bit_vec.as_ptr()).fill(value) }
}
/// Gets the length of the [DisplayBitVec] in bits.
///
/// # Arguments
///
/// - `bit_vec`: instance to write to
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_len(
bit_vec: NonNull<DisplayBitVec>,
) -> usize {
unsafe { bit_vec.as_ref().len() }
}
/// Returns true if length is 0.
///
/// # Arguments
///
/// - `bit_vec`: instance to write to
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_is_empty(
bit_vec: NonNull<DisplayBitVec>,
) -> bool {
unsafe { bit_vec.as_ref().is_empty() }
}
/// Gets an unsafe reference to the data of the [DisplayBitVec] instance.
///
/// The returned memory is valid for the lifetime of the bitvec.
///
/// # Arguments
///
/// - `bit_vec`: instance to write to
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_unsafe_data_ref(
bit_vec: NonNull<DisplayBitVec>,
) -> ByteSlice {
unsafe { ByteSlice::from_slice((*bit_vec.as_ptr()).as_raw_mut_slice()) }
}
/// Creates a [BitVecCommand] and immediately turns that into a [Packet].
///
/// The provided [DisplayBitVec] gets consumed.
///
/// Returns NULL in case of an error.
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_into_packet(
bitvec: NonNull<DisplayBitVec>,
offset: usize,
operation: BinaryOperation,
compression: CompressionCode,
) -> *mut Packet {
let bitvec = unsafe { heap_remove(bitvec) };
heap_move_ok(Packet::try_from(BitVecCommand {
bitvec,
offset,
operation,
compression,
}))
}

View file

@ -0,0 +1,194 @@
use crate::{
containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull,
heap_move_ok, heap_move_some, heap_remove,
};
use servicepoint::{
Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid,
Origin, Packet,
};
use std::mem::transmute;
use std::ptr::NonNull;
/// Creates a new [BrightnessGrid] with the specified dimensions.
///
/// returns: [BrightnessGrid] initialized to 0.
///
/// # Examples
/// ```C
/// UdpSocket *connection = sp_udp_open("127.0.0.1:2342");
/// if (connection == NULL)
/// return 1;
///
/// BrightnessGrid *grid = sp_brightness_grid_new(2, 2);
/// sp_brightness_grid_set(grid, 0, 0, 0);
/// sp_brightness_grid_set(grid, 1, 1, 10);
///
/// TypedCommand *command = sp_command_char_brightness(grid);
/// sp_udp_free(connection);
/// ```
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_new(
width: usize,
height: usize,
) -> NonNull<BrightnessGrid> {
heap_move_nonnull(BrightnessGrid::new(width, height))
}
/// 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(
width: usize,
height: usize,
data: ByteSlice,
) -> *mut BrightnessGrid {
let data = unsafe { data.as_slice() };
heap_move_some(
ByteGrid::load(width, height, data)
.map(move |grid| grid.map(Brightness::saturating_from)),
)
}
/// Clones a [BrightnessGrid].
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_clone(
grid: NonNull<BrightnessGrid>,
) -> NonNull<BrightnessGrid> {
unsafe { heap_clone(grid) }
}
/// Deallocates a [BrightnessGrid].
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_free(
brightness_grid: NonNull<BrightnessGrid>,
) {
unsafe { heap_drop(brightness_grid) }
}
/// Gets the current value at the specified position.
///
/// # Arguments
///
/// - `brightness_grid`: instance to read from
/// - `x` and `y`: position of the cell to read
///
/// returns: value at position
///
/// # Panics
/// - When accessing `x` or `y` out of bounds.
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_get(
brightness_grid: NonNull<BrightnessGrid>,
x: usize,
y: usize,
) -> Brightness {
unsafe { brightness_grid.as_ref().get(x, y) }
}
/// Sets the value of the specified position in the [BrightnessGrid].
///
/// # Arguments
///
/// - `brightness_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_brightness_grid_set(
brightness_grid: NonNull<BrightnessGrid>,
x: usize,
y: usize,
value: Brightness,
) {
unsafe { (*brightness_grid.as_ptr()).set(x, y, value) };
}
/// Sets the value of all cells in the [BrightnessGrid].
///
/// # Arguments
///
/// - `brightness_grid`: instance to write to
/// - `value`: the value to set all cells to
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_fill(
brightness_grid: NonNull<BrightnessGrid>,
value: Brightness,
) {
unsafe { (*brightness_grid.as_ptr()).fill(value) };
}
/// Gets the width of the [BrightnessGrid] instance.
///
/// # Arguments
///
/// - `brightness_grid`: instance to read from
///
/// returns: width
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_width(
brightness_grid: NonNull<BrightnessGrid>,
) -> usize {
unsafe { brightness_grid.as_ref().width() }
}
/// Gets the height of the [BrightnessGrid] instance.
///
/// # Arguments
///
/// - `brightness_grid`: instance to read from
///
/// returns: height
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_height(
brightness_grid: NonNull<BrightnessGrid>,
) -> usize {
unsafe { brightness_grid.as_ref().height() }
}
/// Gets an unsafe reference to the data of the [BrightnessGrid] instance.
///
/// The returned memory is valid for the lifetime of the brightness grid.
///
/// # Arguments
///
/// - `brightness_grid`: instance to read from
///
/// returns: slice of bytes underlying the `brightness_grid`.
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_unsafe_data_ref(
brightness_grid: NonNull<BrightnessGrid>,
) -> ByteSlice {
//noinspection RsAssertEqual
const _: () = assert!(size_of::<Brightness>() == 1);
let data = unsafe { (*brightness_grid.as_ptr()).data_ref_mut() };
unsafe {
ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(data))
}
}
/// Creates a [BrightnessGridCommand] and immediately turns that into a [Packet].
///
/// The provided [BrightnessGrid] gets consumed.
///
/// Returns NULL in case of an error.
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_into_packet(
grid: NonNull<BrightnessGrid>,
x: usize,
y: usize,
) -> *mut Packet {
let grid = unsafe { heap_remove(grid) };
heap_move_ok(Packet::try_from(BrightnessGridCommand {
grid,
origin: Origin::new(x, y),
}))
}

View file

@ -0,0 +1,39 @@
//! FFI slice helper
use std::ptr::NonNull;
/// Represents a span of memory (`&mut [u8]` ) as a struct.
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - accesses to the memory pointed to with `start` is never accessed outside `length`
/// - the lifetime of the `CByteSlice` does not outlive the memory it points to, as described in
/// the function returning this type.
#[repr(C)]
pub struct ByteSlice {
/// The start address of the memory.
pub start: NonNull<u8>,
/// The amount of memory in bytes.
pub length: usize,
}
impl ByteSlice {
pub(crate) unsafe fn as_slice(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.start.as_ptr(), self.length) }
}
pub(crate) unsafe fn as_slice_mut(&mut self) -> &mut [u8] {
unsafe {
std::slice::from_raw_parts_mut(self.start.as_ptr(), self.length)
}
}
pub(crate) unsafe fn from_slice(slice: &mut [u8]) -> Self {
Self {
start: NonNull::new(slice.as_mut_ptr()).unwrap(),
length: slice.len(),
}
}
}

151
src/containers/char_grid.rs Normal file
View file

@ -0,0 +1,151 @@
use crate::{
containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull,
heap_move_ok, heap_remove,
};
use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet};
use std::ptr::NonNull;
/// 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);
/// ```
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_new(
width: usize,
height: usize,
) -> NonNull<CharGrid> {
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
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_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()))
}
/// Clones a [CharGrid].
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_clone(
grid: NonNull<CharGrid>,
) -> NonNull<CharGrid> {
unsafe { heap_clone(grid) }
}
/// Deallocates a [CharGrid].
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_free(char_grid: NonNull<CharGrid>) {
unsafe { heap_drop(char_grid) }
}
/// Returns the current value at the specified position.
///
/// # Arguments
///
/// - `char_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_char_grid_get(
char_grid: NonNull<CharGrid>,
x: usize,
y: usize,
) -> u32 {
unsafe { char_grid.as_ref().get(x, y) as u32 }
}
/// Sets the value of the specified position in the [CharGrid].
///
/// # Arguments
///
/// - `char_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_char_grid_set(
char_grid: NonNull<CharGrid>,
x: usize,
y: usize,
value: u32,
) {
unsafe { (*char_grid.as_ptr()).set(x, y, char::from_u32(value).unwrap()) };
}
/// Sets the value of all cells in the [CharGrid].
///
/// # Arguments
///
/// - `char_grid`: instance to write to
/// - `value`: the value to set all cells to
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_fill(
char_grid: NonNull<CharGrid>,
value: u32,
) {
unsafe { (*char_grid.as_ptr()).fill(char::from_u32(value).unwrap()) };
}
/// Gets the width of the [CharGrid] instance.
///
/// # Arguments
///
/// - `char_grid`: instance to read from
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_width(
char_grid: NonNull<CharGrid>,
) -> usize {
unsafe { char_grid.as_ref().width() }
}
/// Gets the height of the [CharGrid] instance.
///
/// # Arguments
///
/// - `char_grid`: instance to read from
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_height(
char_grid: NonNull<CharGrid>,
) -> usize {
unsafe { char_grid.as_ref().height() }
}
/// Creates a [CharGridCommand] and immediately turns that into a [Packet].
///
/// The provided [CharGrid] gets consumed.
///
/// Returns NULL in case of an error.
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_into_packet(
grid: NonNull<CharGrid>,
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),
}))
}

View file

@ -0,0 +1,152 @@
use crate::{
containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull,
heap_move_ok, heap_move_some, heap_remove,
};
use servicepoint::{
Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet,
};
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<Cp437Grid> {
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() };
heap_move_some(Cp437Grid::load(width, height, data))
}
/// Clones a [Cp437Grid].
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_clone(
grid: NonNull<Cp437Grid>,
) -> NonNull<Cp437Grid> {
unsafe { heap_clone(grid) }
}
/// Deallocates a [Cp437Grid].
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: NonNull<Cp437Grid>) {
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<Cp437Grid>,
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<Cp437Grid>,
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<Cp437Grid>,
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<Cp437Grid>,
) -> 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<Cp437Grid>,
) -> 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<Cp437Grid>,
) -> ByteSlice {
unsafe { ByteSlice::from_slice((*cp437_grid.as_ptr()).data_ref_mut()) }
}
/// Creates a [Cp437GridCommand] and immediately turns that into a [Packet].
///
/// The provided [Cp437Grid] gets consumed.
///
/// Returns NULL in case of an error.
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_into_packet(
grid: NonNull<Cp437Grid>,
x: usize,
y: usize,
) -> *mut Packet {
let grid = unsafe { heap_remove(grid) };
heap_move_ok(Packet::try_from(Cp437GridCommand {
grid,
origin: Origin::new(x, y),
}))
}

13
src/containers/mod.rs Normal file
View file

@ -0,0 +1,13 @@
mod bitmap;
mod bitvec;
mod brightness_grid;
mod byte_slice;
mod char_grid;
mod cp437_grid;
pub use bitmap::*;
pub use bitvec::*;
pub use brightness_grid::*;
pub use byte_slice::*;
pub use char_grid::*;
pub use cp437_grid::*;