servicepoint_binding_c 0.15.0

C bindings for the servicepoint crate.
Documentation
use crate::{containers::ByteSlice, macros::wrap};
use servicepoint::{
    Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid,
    Origin, Packet,
};
use std::ptr::NonNull;

wrap! {
    Bitmap {
    derives: crate::containers::derive_container, crate::containers::derive_grid[bool];
    functions:
        /// 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);
        /// ```
        fn new(width: val usize, height: val usize) -> move_some *mut Bitmap {
            Bitmap::new(width, height)
        };

        /// Creates a new [Bitmap] with a size matching the screen.
        ///
        /// returns: [Bitmap] initialized to all pixels off.
        fn new_max_sized() -> move NonNull<Bitmap> {
            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.
        fn load(
            width: val usize,
            height: val usize,
            data: slice ByteSlice,
        ) -> move_ok *mut Bitmap {
            Bitmap::load(width, height, data)
        };

        /// Tries to convert the BitVec to a Bitmap.
        ///
        /// The provided BitVec gets consumed.
        ///
        /// Returns NULL in case of error.
        fn from_bitvec(
            width: val usize,
            bitvec: move NonNull<DisplayBitVec>,
        ) -> move_ok *mut Bitmap {
            Bitmap::from_bitvec(width, bitvec)
        };

    methods:
        /// Consumes the Bitmap and returns the contained BitVec.
        fn into_bitvec(move bitmap) -> move NonNull<DisplayBitVec> {
            bitmap.into()
        };

        /// Creates a [BitmapCommand] and immediately turns that into a [Packet].
        ///
        /// The provided [Bitmap] gets consumed.
        ///
        /// Returns NULL in case of an error.
        fn try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> move_ok *mut Packet {
            Packet::try_from(BitmapCommand {
                bitmap,
                origin: Origin::new(x, y),
                compression,
            })
        };

        /// Gets an unsafe reference to the data of the [Bitmap] instance.
        ///
        /// The returned memory is valid for the lifetime of the bitmap.
        fn data_ref_mut(mut instance) -> slice ByteSlice;
    }
}