servicepoint_binding_c 0.15.0

C bindings for the servicepoint crate.
Documentation
use crate::{containers::ByteSlice, macros::wrap};
use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet};
use std::ptr::NonNull;

wrap! {
    CharGrid {
    derives: crate::containers::derive_container, crate::containers::derive_get_width_height;
    functions:
        /// 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: val usize, height: val usize) -> move NonNull<CharGrid> {
            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: val usize, height: val usize, data: slice ByteSlice) -> move_ok *mut CharGrid {
            CharGrid::load_utf8(width, height, data.to_vec())
        };
    methods:
        /// 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
        fn get(ref instance, x: val usize, y: val usize) -> val 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`.
        fn set(mut instance, x: val usize, y: val usize, value: val 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`.
        fn fill(mut instance, value: val u32) {
            instance.fill(char::from_u32(value).unwrap())
        };

        /// 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(move grid, x: val usize, y: val usize) -> move_ok *mut Packet {
            Packet::try_from(CharGridCommand {
                grid,
                origin: Origin::new(x, y),
            })
        };
    }
}