From 341c0ef2cdcde7c0cb0bab5aa2216f92f684f41d Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 7 Sep 2024 13:22:41 +0200 Subject: [PATCH] change cbindgen sorting to None --- crates/servicepoint/src/command.rs | 102 +- crates/servicepoint_binding_c/cbindgen.toml | 8 +- .../examples/lang_c/include/servicepoint.h | 1360 +++++++++-------- crates/servicepoint_binding_c/src/lib.rs | 36 +- 4 files changed, 755 insertions(+), 751 deletions(-) diff --git a/crates/servicepoint/src/command.rs b/crates/servicepoint/src/command.rs index 3a5ab1c..d799696 100644 --- a/crates/servicepoint/src/command.rs +++ b/crates/servicepoint/src/command.rs @@ -192,9 +192,7 @@ impl TryFrom for Command { fn try_from(packet: Packet) -> Result { let Packet { header: Header { - command_code, - a, - .. + command_code, a, .. }, .. } = packet; @@ -274,13 +272,14 @@ impl Command { compression: CompressionCode, ) -> Result { let Packet { - header: Header { - command_code: _, - a: tiles_x, - b: pixels_y, - c: tile_w, - d: pixel_h, - }, + header: + Header { + command_code: _, + a: tiles_x, + b: pixels_y, + c: tile_w, + d: pixel_h, + }, payload, } = packet; @@ -306,13 +305,14 @@ impl Command { command: Command, ) -> Result { let Packet { - header: Header { - command_code: _, - a, - b, - c, - d, - }, + header: + Header { + command_code: _, + a, + b, + c, + d, + }, payload, } = packet; if !payload.is_empty() { @@ -329,12 +329,13 @@ impl Command { packet: Packet, ) -> Result<(SpBitVec, CompressionCode), TryFromPacketError> { let Packet { - header: Header { - b: length, - c: sub, - d: reserved, - .. - }, + header: + Header { + b: length, + c: sub, + d: reserved, + .. + }, payload, } = packet; if reserved != 0 { @@ -363,13 +364,14 @@ impl Command { packet: &Packet, ) -> Result { let Packet { - header: Header { - command_code: _, - a: x, - b: y, - c: width, - d: height, - }, + header: + Header { + command_code: _, + a: x, + b: y, + c: width, + d: height, + }, payload, } = packet; @@ -390,13 +392,14 @@ impl Command { packet: &Packet, ) -> Result { let Packet { - header: Header { - command_code: _, - a, - b, - c, - d, - }, + header: + Header { + command_code: _, + a, + b, + c, + d, + }, payload, } = packet; if payload.len() != 1 { @@ -420,13 +423,14 @@ impl Command { packet: &Packet, ) -> Result { let Packet { - header: Header { - command_code: _, - a, - b, - c, - d, - }, + header: + Header { + command_code: _, + a, + b, + c, + d, + }, payload, } = packet; Ok(Command::Cp437Data( @@ -662,7 +666,7 @@ mod tests { PixelGrid::new(8, 8), compression, ) - .into(); + .into(); let Packet { header, @@ -692,7 +696,7 @@ mod tests { BitVec::repeat(false, 8), compression, ) - .into(); + .into(); let Packet { header, mut payload, @@ -752,7 +756,7 @@ mod tests { BitVec::repeat(false, 8), CompressionCode::Uncompressed, ) - .into(); + .into(); let Header { command_code: command, a: offset, @@ -783,7 +787,7 @@ mod tests { BitVec::repeat(false, 8), CompressionCode::Uncompressed, ) - .into(); + .into(); let Header { command_code: command, a: offset, @@ -814,7 +818,7 @@ mod tests { BitVec::repeat(false, 8), CompressionCode::Uncompressed, ) - .into(); + .into(); let Header { command_code: command, a: offset, diff --git a/crates/servicepoint_binding_c/cbindgen.toml b/crates/servicepoint_binding_c/cbindgen.toml index 1c363cf..5aa39c8 100644 --- a/crates/servicepoint_binding_c/cbindgen.toml +++ b/crates/servicepoint_binding_c/cbindgen.toml @@ -2,6 +2,8 @@ language = "C" include_version = true cpp_compat = true +autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */" + ############################ Code Style Options ################################ braces = "SameLine" @@ -15,7 +17,7 @@ line_endings = "LF" ############################# Codegen Options ################################## style = "both" -sort_by = "Name" +sort_by = "None" usize_is_size_t = true [parse] @@ -23,3 +25,7 @@ parse_deps = false [parse.expand] all_features = true + +[export] +include = [] +exclude = [] diff --git a/crates/servicepoint_binding_c/examples/lang_c/include/servicepoint.h b/crates/servicepoint_binding_c/examples/lang_c/include/servicepoint.h index 4b1cfda..a132c7d 100644 --- a/crates/servicepoint_binding_c/examples/lang_c/include/servicepoint.h +++ b/crates/servicepoint_binding_c/examples/lang_c/include/servicepoint.h @@ -1,31 +1,13 @@ /* Generated with cbindgen:0.26.0 */ +/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */ + #include #include #include #include #include -/** - * pixel count on whole screen - */ -#define SP_PIXEL_COUNT (SP_PIXEL_WIDTH * SP_PIXEL_HEIGHT) - -/** - * Display height in pixels - */ -#define SP_PIXEL_HEIGHT (SP_TILE_HEIGHT * SP_TILE_SIZE) - -/** - * Display width in pixels - */ -#define SP_PIXEL_WIDTH (SP_TILE_WIDTH * SP_TILE_SIZE) - -/** - * Display tile count in the y-direction - */ -#define SP_TILE_HEIGHT 20 - /** * size of a single tile in one dimension */ @@ -36,6 +18,26 @@ */ #define SP_TILE_WIDTH 56 +/** + * Display tile count in the y-direction + */ +#define SP_TILE_HEIGHT 20 + +/** + * Display width in pixels + */ +#define SP_PIXEL_WIDTH (SP_TILE_WIDTH * SP_TILE_SIZE) + +/** + * Display height in pixels + */ +#define SP_PIXEL_HEIGHT (SP_TILE_HEIGHT * SP_TILE_SIZE) + +/** + * pixel count on whole screen + */ +#define SP_PIXEL_COUNT (SP_PIXEL_WIDTH * SP_PIXEL_HEIGHT) + /** * Specifies the kind of compression to use. */ @@ -195,344 +197,118 @@ extern "C" { #endif // __cplusplus /** - * Clones a `BitVec`. + * Tries to turn a `SPPacket` into a `SPCommand`. The packet is deallocated in the process. + * + * Returns: pointer to new `SPCommand` instance or NULL * * # Safety * * The caller has to make sure that: * - * - `this` points to a valid `BitVec` + * - `packet` points to a valid instance of `SPPacket` + * - `packet` is not used concurrently or after this call + * - the result is checked for NULL + * - the returned `SPCommand` instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_command_dealloc`. + */ +struct SPCommand *sp_command_try_from_packet(struct SPPacket *packet); + +/** + * Clones a `SPCommand` instance. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid instance of `Command` * - `this` is not written to concurrently - * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_bit_vec_dealloc`. + * - the returned `Command` instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_command_dealloc`. */ -struct SPBitVec *sp_bit_vec_clone(const struct SPBitVec *this_); +struct SPCommand *sp_command_clone(const struct SPCommand *original); /** - * Deallocates a `BitVec`. + * Allocates a new `Command::Clear` instance. + * + * Set all pixels to the off state. Does not affect brightness. + * + * # Examples + * + * ```C + * sp_connection_send(connection, sp_command_clear()); + * ``` * * # Safety * * The caller has to make sure that: * - * - `this` points to a valid `BitVec` - * - `this` is not used concurrently or after this call - * - `this` was not passed to another consuming function, e.g. to create a `Command` + * - the returned `Command` instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_command_dealloc`. */ -void sp_bit_vec_dealloc(struct SPBitVec *this_); +struct SPCommand *sp_command_clear(void); /** - * Sets the value of all bits in the `BitVec`. + * Allocates a new `Command::HardReset` instance. * - * # Arguments - * - * - `value`: the value to set all bits to + * Kills the udp daemon on the display, which usually results in a restart. + * Please do not send this in your normal program flow. * * # Safety * * The caller has to make sure that: * - * - `this` points to a valid `BitVec` - * - `this` is not written to or read from concurrently + * - the returned `Command` instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_command_dealloc`. */ -void sp_bit_vec_fill(struct SPBitVec *this_, bool value); +struct SPCommand *sp_command_hard_reset(void); /** - * Gets the value of a bit from the `BitVec`. + * Allocates a new `Command::FadeOut` instance. * - * # Arguments + * # Safety * - * - `this`: instance to read from - * - `index`: the bit index to read + * The caller has to make sure that: * - * returns: value of the bit + * - the returned `Command` instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_command_dealloc`. + */ +struct SPCommand *sp_command_fade_out(void); + +/** + * Allocates a new `Command::Brightness` instance for setting the brightness of all tiles to the + * same value. * * # Panics * - * When accessing `index` out of bounds. + * - When the provided brightness value is out of range (0-11). * * # Safety * * The caller has to make sure that: * - * - `this` points to a valid `BitVec` - * - `this` is not written to concurrently + * - the returned `SPCommand` instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_command_dealloc`. */ -bool sp_bit_vec_get(const struct SPBitVec *this_, size_t index); +struct SPCommand *sp_command_brightness(uint8_t brightness); /** - * Returns true if length is 0. + * Allocates a new `Command::CharBrightness` instance. + * The passed `SPBrightnessGrid` gets consumed. + * + * Set the brightness of individual tiles in a rectangular area of the display. * * # Safety * * The caller has to make sure that: * - * - `this` points to a valid `BitVec` + * - `grid` points to a valid instance of `SPBrightnessGrid` + * - `grid` is not used concurrently or after this call + * - the returned `Command` instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_command_dealloc`. */ -bool sp_bit_vec_is_empty(const struct SPBitVec *this_); - -/** - * Gets the length of the `BitVec` in bits. - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `BitVec` - */ -size_t sp_bit_vec_len(const struct SPBitVec *this_); - -/** - * Interpret the data as a series of bits and load then into a new `BitVec` instance. - * - * # Safety - * - * The caller has to make sure that: - * - * - `data` points to a valid memory location of at least `data_length` - * bytes in size. - * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_bit_vec_dealloc`. - */ -struct SPBitVec *sp_bit_vec_load(const uint8_t *data, - size_t data_length); - -/** - * Creates a new `BitVec` instance. - * - * # Arguments - * - * - `size`: size in bits. - * - * returns: `BitVec` with all bits set to false. - * - * # Panics - * - * When `size` is not divisible by 8. - * - * # Safety - * - * The caller has to make sure that: - * - * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_bit_vec_dealloc`. - */ -struct SPBitVec *sp_bit_vec_new(size_t size); - -/** - * Sets the value of a bit in the `BitVec`. - * - * # Arguments - * - * - `this`: instance to write to - * - `index`: the bit index to edit - * - `value`: the value to set the bit to - * - * returns: old value of the bit - * - * # Panics - * - * When accessing `index` out of bounds. - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `BitVec` - * - `this` is not written to or read from concurrently - */ -void sp_bit_vec_set(struct SPBitVec *this_, size_t index, bool value); - -/** - * Gets an unsafe reference to the data of the `BitVec` instance. - * - * ## Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `BitVec` - * - the returned memory range is never accessed after the passed `BitVec` has been freed - * - the returned memory range is never accessed concurrently, either via the `BitVec` or directly - */ -struct SPByteSlice sp_bit_vec_unsafe_data_ref(struct SPBitVec *this_); - -/** - * Clones a `BrightnessGrid`. - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `BrightnessGrid` - * - `this` is not written to concurrently - * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_brightness_grid_dealloc`. - */ -struct SPBrightnessGrid *sp_brightness_grid_clone(const struct SPBrightnessGrid *this_); - -/** - * Deallocates a `BrightnessGrid`. - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `BrightnessGrid` - * - `this` is not used concurrently or after this call - * - `this` was not passed to another consuming function, e.g. to create a `Command` - */ -void sp_brightness_grid_dealloc(struct SPBrightnessGrid *this_); - -/** - * Sets the value of all cells in the `BrightnessGrid`. - * - * # Arguments - * - * - `this`: instance to write to - * - `value`: the value to set all cells to - * - * # Panics - * - * - When providing an invalid brightness value - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `BrightnessGrid` - * - `this` is not written to or read from concurrently - */ -void sp_brightness_grid_fill(struct SPBrightnessGrid *this_, uint8_t value); - -/** - * Gets the current value at the specified position. - * - * # Arguments - * - * - `this`: instance to read from - * - `x` and `y`: position of the cell to read - * - * # Panics - * - * When accessing `x` or `y` out of bounds. - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `BrightnessGrid` - * - `this` is not written to concurrently - */ -uint8_t sp_brightness_grid_get(const struct SPBrightnessGrid *this_, - size_t x, - size_t y); - -/** - * Gets the height of the `BrightnessGrid` instance. - * - * # Arguments - * - * - `this`: instance to read from - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `BrightnessGrid` - */ -size_t sp_brightness_grid_height(const struct SPBrightnessGrid *this_); - -/** - * Loads a `BrightnessGrid` with the specified dimensions from the provided data. - * - * # Panics - * - * When the provided `data_length` is not sufficient for the `height` and `width` - * - * # Safety - * - * The caller has to make sure that: - * - * - `data` points to a valid memory location of at least `data_length` - * bytes in size. - * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_brightness_grid_dealloc`. - */ -struct SPBrightnessGrid *sp_brightness_grid_load(size_t width, - size_t height, - const uint8_t *data, - size_t data_length); - -/** - * Creates a new `BrightnessGrid` with the specified dimensions. - * - * returns: `BrightnessGrid` initialized to 0. - * - * # Safety - * - * The caller has to make sure that: - * - * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_brightness_grid_dealloc`. - */ -struct SPBrightnessGrid *sp_brightness_grid_new(size_t width, - size_t height); - -/** - * Sets the value of the specified position in the `BrightnessGrid`. - * - * # Arguments - * - * - `this`: 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. - * - When providing an invalid brightness value - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `BitVec` - * - `this` is not written to or read from concurrently - */ -void sp_brightness_grid_set(struct SPBrightnessGrid *this_, - size_t x, - size_t y, - uint8_t value); - -/** - * Gets an unsafe reference to the data of the `BrightnessGrid` instance. - * - * ## Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `BrightnessGrid` - * - the returned memory range is never accessed after the passed `BrightnessGrid` has been freed - * - the returned memory range is never accessed concurrently, either via the `BrightnessGrid` or directly - */ -struct SPByteSlice sp_brightness_grid_unsafe_data_ref(struct SPBrightnessGrid *this_); - -/** - * Gets the width of the `BrightnessGrid` instance. - * - * # Arguments - * - * - `this`: instance to read from - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `BrightnessGrid` - */ -size_t sp_brightness_grid_width(const struct SPBrightnessGrid *this_); +struct SPCommand *sp_command_char_brightness(size_t x, + size_t y, + struct SPBrightnessGrid *grid); /** * Allocates a new `Command::BitmapLinear` instance. @@ -609,27 +385,6 @@ struct SPCommand *sp_command_bitmap_linear_or(size_t offset, struct SPBitVec *bit_vec, SPCompressionCode compression); -/** - * Allocates a new `Command::BitmapLinearWin` instance. - * The passed `PixelGrid` gets consumed. - * - * Sets a window of pixels to the specified values - * - * # Safety - * - * The caller has to make sure that: - * - * - `pixel_grid` points to a valid instance of `PixelGrid` - * - `pixel_grid` is not used concurrently or after this call - * - `compression` matches one of the allowed enum values - * - the returned `Command` instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_command_dealloc`. - */ -struct SPCommand *sp_command_bitmap_linear_win(size_t x, - size_t y, - struct SPPixelGrid *pixel_grid, - SPCompressionCode compression_code); - /** * Allocates a new `Command::BitmapLinearXor` instance. * The passed `BitVec` gets consumed. @@ -655,76 +410,6 @@ struct SPCommand *sp_command_bitmap_linear_xor(size_t offset, struct SPBitVec *bit_vec, SPCompressionCode compression); -/** - * Allocates a new `Command::Brightness` instance for setting the brightness of all tiles to the - * same value. - * - * # Panics - * - * - When the provided brightness value is out of range (0-11). - * - * # Safety - * - * The caller has to make sure that: - * - * - the returned `SPCommand` instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_command_dealloc`. - */ -struct SPCommand *sp_command_brightness(uint8_t brightness); - -/** - * Allocates a new `Command::CharBrightness` instance. - * The passed `SPBrightnessGrid` gets consumed. - * - * Set the brightness of individual tiles in a rectangular area of the display. - * - * # Safety - * - * The caller has to make sure that: - * - * - `grid` points to a valid instance of `SPBrightnessGrid` - * - `grid` is not used concurrently or after this call - * - the returned `Command` instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_command_dealloc`. - */ -struct SPCommand *sp_command_char_brightness(size_t x, - size_t y, - struct SPBrightnessGrid *grid); - -/** - * Allocates a new `Command::Clear` instance. - * - * Set all pixels to the off state. Does not affect brightness. - * - * # Examples - * - * ```C - * sp_connection_send(connection, sp_command_clear()); - * ``` - * - * # Safety - * - * The caller has to make sure that: - * - * - the returned `Command` instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_command_dealloc`. - */ -struct SPCommand *sp_command_clear(void); - -/** - * Clones a `SPCommand` instance. - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid instance of `Command` - * - `this` is not written to concurrently - * - the returned `Command` instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_command_dealloc`. - */ -struct SPCommand *sp_command_clone(const struct SPCommand *original); - /** * Allocates a new `Command::Cp437Data` instance. * The passed `ByteGrid` gets consumed. @@ -749,6 +434,27 @@ struct SPCommand *sp_command_cp437_data(size_t x, size_t y, struct SPCp437Grid *byte_grid); +/** + * Allocates a new `Command::BitmapLinearWin` instance. + * The passed `PixelGrid` gets consumed. + * + * Sets a window of pixels to the specified values + * + * # Safety + * + * The caller has to make sure that: + * + * - `pixel_grid` points to a valid instance of `PixelGrid` + * - `pixel_grid` is not used concurrently or after this call + * - `compression` matches one of the allowed enum values + * - the returned `Command` instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_command_dealloc`. + */ +struct SPCommand *sp_command_bitmap_linear_win(size_t x, + size_t y, + struct SPPixelGrid *pixel_grid, + SPCompressionCode compression_code); + /** * Deallocates a `Command`. * @@ -770,60 +476,344 @@ struct SPCommand *sp_command_cp437_data(size_t x, void sp_command_dealloc(struct SPCommand *ptr); /** - * Allocates a new `Command::FadeOut` instance. + * Creates a new `BitVec` instance. + * + * # Arguments + * + * - `size`: size in bits. + * + * returns: `BitVec` with all bits set to false. + * + * # Panics + * + * When `size` is not divisible by 8. * * # Safety * * The caller has to make sure that: * - * - the returned `Command` instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_command_dealloc`. + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_bit_vec_dealloc`. */ -struct SPCommand *sp_command_fade_out(void); +struct SPBitVec *sp_bit_vec_new(size_t size); /** - * Allocates a new `Command::HardReset` instance. - * - * Kills the udp daemon on the display, which usually results in a restart. - * Please do not send this in your normal program flow. + * Interpret the data as a series of bits and load then into a new `BitVec` instance. * * # Safety * * The caller has to make sure that: * - * - the returned `Command` instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_command_dealloc`. + * - `data` points to a valid memory location of at least `data_length` + * bytes in size. + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_bit_vec_dealloc`. */ -struct SPCommand *sp_command_hard_reset(void); +struct SPBitVec *sp_bit_vec_load(const uint8_t *data, + size_t data_length); /** - * Tries to turn a `SPPacket` into a `SPCommand`. The packet is deallocated in the process. - * - * Returns: pointer to new `SPCommand` instance or NULL + * Clones a `BitVec`. * * # Safety * * The caller has to make sure that: * - * - `packet` points to a valid instance of `SPPacket` - * - `packet` is not used concurrently or after this call - * - the result is checked for NULL - * - the returned `SPCommand` instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_command_dealloc`. + * - `this` points to a valid `BitVec` + * - `this` is not written to concurrently + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_bit_vec_dealloc`. */ -struct SPCommand *sp_command_try_from_packet(struct SPPacket *packet); +struct SPBitVec *sp_bit_vec_clone(const struct SPBitVec *this_); /** - * Closes and deallocates a `Connection`. + * Deallocates a `BitVec`. * * # Safety * * The caller has to make sure that: * - * - `this` points to a valid `Connection` + * - `this` points to a valid `BitVec` * - `this` is not used concurrently or after this call + * - `this` was not passed to another consuming function, e.g. to create a `Command` */ -void sp_connection_dealloc(struct SPConnection *ptr); +void sp_bit_vec_dealloc(struct SPBitVec *this_); + +/** + * Gets the value of a bit from the `BitVec`. + * + * # Arguments + * + * - `this`: instance to read from + * - `index`: the bit index to read + * + * returns: value of the bit + * + * # Panics + * + * When accessing `index` out of bounds. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BitVec` + * - `this` is not written to concurrently + */ +bool sp_bit_vec_get(const struct SPBitVec *this_, size_t index); + +/** + * Sets the value of a bit in the `BitVec`. + * + * # Arguments + * + * - `this`: instance to write to + * - `index`: the bit index to edit + * - `value`: the value to set the bit to + * + * returns: old value of the bit + * + * # Panics + * + * When accessing `index` out of bounds. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BitVec` + * - `this` is not written to or read from concurrently + */ +void sp_bit_vec_set(struct SPBitVec *this_, size_t index, bool value); + +/** + * Sets the value of all bits in the `BitVec`. + * + * # Arguments + * + * - `value`: the value to set all bits to + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BitVec` + * - `this` is not written to or read from concurrently + */ +void sp_bit_vec_fill(struct SPBitVec *this_, bool value); + +/** + * Gets the length of the `BitVec` in bits. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BitVec` + */ +size_t sp_bit_vec_len(const struct SPBitVec *this_); + +/** + * Returns true if length is 0. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BitVec` + */ +bool sp_bit_vec_is_empty(const struct SPBitVec *this_); + +/** + * Gets an unsafe reference to the data of the `BitVec` instance. + * + * ## Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BitVec` + * - the returned memory range is never accessed after the passed `BitVec` has been freed + * - the returned memory range is never accessed concurrently, either via the `BitVec` or directly + */ +struct SPByteSlice sp_bit_vec_unsafe_data_ref(struct SPBitVec *this_); + +/** + * Creates a new `BrightnessGrid` with the specified dimensions. + * + * returns: `BrightnessGrid` initialized to 0. + * + * # Safety + * + * The caller has to make sure that: + * + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_brightness_grid_dealloc`. + */ +struct SPBrightnessGrid *sp_brightness_grid_new(size_t width, + size_t height); + +/** + * Loads a `BrightnessGrid` with the specified dimensions from the provided data. + * + * # Panics + * + * When the provided `data_length` is not sufficient for the `height` and `width` + * + * # Safety + * + * The caller has to make sure that: + * + * - `data` points to a valid memory location of at least `data_length` + * bytes in size. + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_brightness_grid_dealloc`. + */ +struct SPBrightnessGrid *sp_brightness_grid_load(size_t width, + size_t height, + const uint8_t *data, + size_t data_length); + +/** + * Clones a `BrightnessGrid`. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BrightnessGrid` + * - `this` is not written to concurrently + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_brightness_grid_dealloc`. + */ +struct SPBrightnessGrid *sp_brightness_grid_clone(const struct SPBrightnessGrid *this_); + +/** + * Deallocates a `BrightnessGrid`. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BrightnessGrid` + * - `this` is not used concurrently or after this call + * - `this` was not passed to another consuming function, e.g. to create a `Command` + */ +void sp_brightness_grid_dealloc(struct SPBrightnessGrid *this_); + +/** + * Gets the current value at the specified position. + * + * # Arguments + * + * - `this`: instance to read from + * - `x` and `y`: position of the cell to read + * + * # Panics + * + * When accessing `x` or `y` out of bounds. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BrightnessGrid` + * - `this` is not written to concurrently + */ +uint8_t sp_brightness_grid_get(const struct SPBrightnessGrid *this_, + size_t x, + size_t y); + +/** + * Sets the value of the specified position in the `BrightnessGrid`. + * + * # Arguments + * + * - `this`: 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. + * - When providing an invalid brightness value + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BitVec` + * - `this` is not written to or read from concurrently + */ +void sp_brightness_grid_set(struct SPBrightnessGrid *this_, + size_t x, + size_t y, + uint8_t value); + +/** + * Sets the value of all cells in the `BrightnessGrid`. + * + * # Arguments + * + * - `this`: instance to write to + * - `value`: the value to set all cells to + * + * # Panics + * + * - When providing an invalid brightness value + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BrightnessGrid` + * - `this` is not written to or read from concurrently + */ +void sp_brightness_grid_fill(struct SPBrightnessGrid *this_, uint8_t value); + +/** + * Gets the width of the `BrightnessGrid` instance. + * + * # Arguments + * + * - `this`: instance to read from + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BrightnessGrid` + */ +size_t sp_brightness_grid_width(const struct SPBrightnessGrid *this_); + +/** + * Gets the height of the `BrightnessGrid` instance. + * + * # Arguments + * + * - `this`: instance to read from + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BrightnessGrid` + */ +size_t sp_brightness_grid_height(const struct SPBrightnessGrid *this_); + +/** + * Gets an unsafe reference to the data of the `BrightnessGrid` instance. + * + * ## Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BrightnessGrid` + * - the returned memory range is never accessed after the passed `BrightnessGrid` has been freed + * - the returned memory range is never accessed concurrently, either via the `BrightnessGrid` or directly + */ +struct SPByteSlice sp_brightness_grid_unsafe_data_ref(struct SPBrightnessGrid *this_); /** * Creates a new instance of `Connection`. @@ -861,201 +851,16 @@ bool sp_connection_send(const struct SPConnection *connection, struct SPPacket *packet); /** - * Clones a `Cp437Grid`. + * Closes and deallocates a `Connection`. * * # Safety * * The caller has to make sure that: * - * - `this` points to a valid `Cp437Grid` - * - `this` is not written to concurrently - * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_cp437_grid_dealloc`. - */ -struct SPCp437Grid *sp_cp437_grid_clone(const struct SPCp437Grid *this_); - -/** - * Deallocates a `Cp437Grid`. - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `Cp437Grid` - * - `this` is not used concurrently or after this call - * - `this` was not passed to another consuming function, e.g. to create a `Command` - */ -void sp_cp437_grid_dealloc(struct SPCp437Grid *this_); - -/** - * Sets the value of all cells in the `Cp437Grid`. - * - * # Arguments - * - * - `this`: instance to write to - * - `value`: the value to set all cells to - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `Cp437Grid` - * - `this` is not written to or read from concurrently - */ -void sp_cp437_grid_fill(struct SPCp437Grid *this_, uint8_t value); - -/** - * Gets the current value at the specified position. - * - * # Arguments - * - * - `this`: instance to read from - * - `x` and `y`: position of the cell to read - * - * # Panics - * - * When accessing `x` or `y` out of bounds. - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `Cp437Grid` - * - `this` is not written to concurrently - */ -uint8_t sp_cp437_grid_get(const struct SPCp437Grid *this_, size_t x, size_t y); - -/** - * Gets the height of the `Cp437Grid` instance. - * - * # Arguments - * - * - `this`: instance to read from - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `Cp437Grid` - */ -size_t sp_cp437_grid_height(const struct SPCp437Grid *this_); - -/** - * Loads a `Cp437Grid` with the specified dimensions from the provided data. - * - * # Panics - * - * When the provided `data_length` is not sufficient for the `height` and `width` - * - * # Safety - * - * The caller has to make sure that: - * - * - `data` points to a valid memory location of at least `data_length` - * bytes in size. - * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_cp437_grid_dealloc`. - */ -struct SPCp437Grid *sp_cp437_grid_load(size_t width, - size_t height, - const uint8_t *data, - size_t data_length); - -/** - * Creates a new `Cp437Grid` with the specified dimensions. - * - * returns: `Cp437Grid` initialized to 0. - * - * # Safety - * - * The caller has to make sure that: - * - * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_cp437_grid_dealloc`. - */ -struct SPCp437Grid *sp_cp437_grid_new(size_t width, - size_t height); - -/** - * Sets the value of the specified position in the `Cp437Grid`. - * - * # Arguments - * - * - `this`: 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. - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `BitVec` - * - `this` is not written to or read from concurrently - */ -void sp_cp437_grid_set(struct SPCp437Grid *this_, - size_t x, - size_t y, - uint8_t value); - -/** - * Gets an unsafe reference to the data of the `Cp437Grid` instance. - * - * ## Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `Cp437Grid` - * - the returned memory range is never accessed after the passed `Cp437Grid` has been freed - * - the returned memory range is never accessed concurrently, either via the `Cp437Grid` or directly - */ -struct SPByteSlice sp_cp437_grid_unsafe_data_ref(struct SPCp437Grid *this_); - -/** - * Gets the width of the `Cp437Grid` instance. - * - * # Arguments - * - * - `this`: instance to read from - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `Cp437Grid` - */ -size_t sp_cp437_grid_width(const struct SPCp437Grid *this_); - -/** - * Clones a `Packet`. - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `Packet` - * - `this` is not written to concurrently - * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_packet_dealloc`. - */ -struct SPPacket *sp_packet_clone(const struct SPPacket *this_); - -/** - * Deallocates a `Packet`. - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `Packet` + * - `this` points to a valid `Connection` * - `this` is not used concurrently or after this call */ -void sp_packet_dealloc(struct SPPacket *this_); +void sp_connection_dealloc(struct SPConnection *ptr); /** * Turns a `Command` into a `Packet`. @@ -1090,84 +895,54 @@ struct SPPacket *sp_packet_try_load(const uint8_t *data, size_t length); /** - * Clones a `PixelGrid`. + * Clones a `Packet`. * * # Safety * * The caller has to make sure that: * - * - `this` points to a valid `PixelGrid` + * - `this` points to a valid `Packet` * - `this` is not written to concurrently * - the returned instance is freed in some way, either by using a consuming function or - * by explicitly calling `sp_pixel_grid_dealloc`. + * by explicitly calling `sp_packet_dealloc`. */ -struct SPPixelGrid *sp_pixel_grid_clone(const struct SPPixelGrid *this_); +struct SPPacket *sp_packet_clone(const struct SPPacket *this_); /** - * Deallocates a `PixelGrid`. + * Deallocates a `Packet`. * * # Safety * * The caller has to make sure that: * - * - `this` points to a valid `PixelGrid` + * - `this` points to a valid `Packet` * - `this` is not used concurrently or after this call - * - `this` was not passed to another consuming function, e.g. to create a `Command` */ -void sp_pixel_grid_dealloc(struct SPPixelGrid *this_); +void sp_packet_dealloc(struct SPPacket *this_); /** - * Sets the state of all pixels in the `PixelGrid`. + * Creates a new `PixelGrid` with the specified dimensions. * * # Arguments * - * - `this`: instance to write to - * - `value`: the value to set all pixels to + * - `width`: size in pixels in x-direction + * - `height`: size in pixels in y-direction * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `PixelGrid` - * - `this` is not written to or read from concurrently - */ -void sp_pixel_grid_fill(struct SPPixelGrid *this_, bool value); - -/** - * Gets the current value at the specified position in the `PixelGrid`. - * - * # Arguments - * - * - `this`: instance to read from - * - `x` and `y`: position of the cell to read + * returns: `PixelGrid` initialized to all pixels off * * # Panics * - * When accessing `x` or `y` out of bounds. + * - when the width is not dividable by 8 * * # Safety * * The caller has to make sure that: * - * - `this` points to a valid `PixelGrid` - * - `this` is not written to concurrently + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_pixel_grid_dealloc`. */ -bool sp_pixel_grid_get(const struct SPPixelGrid *this_, size_t x, size_t y); - -/** - * Gets the height in pixels of the `PixelGrid` instance. - * - * # Arguments - * - * - `this`: instance to read from - * - * # Safety - * - * The caller has to make sure that: - * - * - `this` points to a valid `PixelGrid` - */ -size_t sp_pixel_grid_height(const struct SPPixelGrid *this_); +struct SPPixelGrid *sp_pixel_grid_new(size_t width, + size_t height); /** * Loads a `PixelGrid` with the specified dimensions from the provided data. @@ -1198,28 +973,52 @@ struct SPPixelGrid *sp_pixel_grid_load(size_t width, size_t data_length); /** - * Creates a new `PixelGrid` with the specified dimensions. - * - * # Arguments - * - * - `width`: size in pixels in x-direction - * - `height`: size in pixels in y-direction - * - * returns: `PixelGrid` initialized to all pixels off - * - * # Panics - * - * - when the width is not dividable by 8 + * Clones a `PixelGrid`. * * # Safety * * The caller has to make sure that: * + * - `this` points to a valid `PixelGrid` + * - `this` is not written to concurrently * - the returned instance is freed in some way, either by using a consuming function or * by explicitly calling `sp_pixel_grid_dealloc`. */ -struct SPPixelGrid *sp_pixel_grid_new(size_t width, - size_t height); +struct SPPixelGrid *sp_pixel_grid_clone(const struct SPPixelGrid *this_); + +/** + * Deallocates a `PixelGrid`. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `PixelGrid` + * - `this` is not used concurrently or after this call + * - `this` was not passed to another consuming function, e.g. to create a `Command` + */ +void sp_pixel_grid_dealloc(struct SPPixelGrid *this_); + +/** + * Gets the current value at the specified position in the `PixelGrid`. + * + * # Arguments + * + * - `this`: instance to read from + * - `x` and `y`: position of the cell to read + * + * # Panics + * + * When accessing `x` or `y` out of bounds. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `PixelGrid` + * - `this` is not written to concurrently + */ +bool sp_pixel_grid_get(const struct SPPixelGrid *this_, size_t x, size_t y); /** * Sets the value of the specified position in the `PixelGrid`. @@ -1249,17 +1048,21 @@ void sp_pixel_grid_set(struct SPPixelGrid *this_, bool value); /** - * Gets an unsafe reference to the data of the `PixelGrid` instance. + * Sets the state of all pixels in the `PixelGrid`. * - * ## Safety + * # Arguments + * + * - `this`: instance to write to + * - `value`: the value to set all pixels to + * + * # Safety * * The caller has to make sure that: * * - `this` points to a valid `PixelGrid` - * - the returned memory range is never accessed after the passed `PixelGrid` has been freed - * - the returned memory range is never accessed concurrently, either via the `PixelGrid` or directly + * - `this` is not written to or read from concurrently */ -struct SPByteSlice sp_pixel_grid_unsafe_data_ref(struct SPPixelGrid *this_); +void sp_pixel_grid_fill(struct SPPixelGrid *this_, bool value); /** * Gets the width in pixels of the `PixelGrid` instance. @@ -1276,6 +1079,205 @@ struct SPByteSlice sp_pixel_grid_unsafe_data_ref(struct SPPixelGrid *this_); */ size_t sp_pixel_grid_width(const struct SPPixelGrid *this_); +/** + * Gets the height in pixels of the `PixelGrid` instance. + * + * # Arguments + * + * - `this`: instance to read from + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `PixelGrid` + */ +size_t sp_pixel_grid_height(const struct SPPixelGrid *this_); + +/** + * Gets an unsafe reference to the data of the `PixelGrid` instance. + * + * ## Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `PixelGrid` + * - the returned memory range is never accessed after the passed `PixelGrid` has been freed + * - the returned memory range is never accessed concurrently, either via the `PixelGrid` or directly + */ +struct SPByteSlice sp_pixel_grid_unsafe_data_ref(struct SPPixelGrid *this_); + +/** + * Creates a new `Cp437Grid` with the specified dimensions. + * + * returns: `Cp437Grid` initialized to 0. + * + * # Safety + * + * The caller has to make sure that: + * + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_cp437_grid_dealloc`. + */ +struct SPCp437Grid *sp_cp437_grid_new(size_t width, + size_t height); + +/** + * Loads a `Cp437Grid` with the specified dimensions from the provided data. + * + * # Panics + * + * When the provided `data_length` is not sufficient for the `height` and `width` + * + * # Safety + * + * The caller has to make sure that: + * + * - `data` points to a valid memory location of at least `data_length` + * bytes in size. + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_cp437_grid_dealloc`. + */ +struct SPCp437Grid *sp_cp437_grid_load(size_t width, + size_t height, + const uint8_t *data, + size_t data_length); + +/** + * Clones a `Cp437Grid`. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + * - `this` is not written to concurrently + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_cp437_grid_dealloc`. + */ +struct SPCp437Grid *sp_cp437_grid_clone(const struct SPCp437Grid *this_); + +/** + * Deallocates a `Cp437Grid`. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + * - `this` is not used concurrently or after this call + * - `this` was not passed to another consuming function, e.g. to create a `Command` + */ +void sp_cp437_grid_dealloc(struct SPCp437Grid *this_); + +/** + * Gets the current value at the specified position. + * + * # Arguments + * + * - `this`: instance to read from + * - `x` and `y`: position of the cell to read + * + * # Panics + * + * When accessing `x` or `y` out of bounds. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + * - `this` is not written to concurrently + */ +uint8_t sp_cp437_grid_get(const struct SPCp437Grid *this_, size_t x, size_t y); + +/** + * Sets the value of the specified position in the `Cp437Grid`. + * + * # Arguments + * + * - `this`: 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. + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `BitVec` + * - `this` is not written to or read from concurrently + */ +void sp_cp437_grid_set(struct SPCp437Grid *this_, + size_t x, + size_t y, + uint8_t value); + +/** + * Sets the value of all cells in the `Cp437Grid`. + * + * # Arguments + * + * - `this`: instance to write to + * - `value`: the value to set all cells to + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + * - `this` is not written to or read from concurrently + */ +void sp_cp437_grid_fill(struct SPCp437Grid *this_, uint8_t value); + +/** + * Gets the width of the `Cp437Grid` instance. + * + * # Arguments + * + * - `this`: instance to read from + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + */ +size_t sp_cp437_grid_width(const struct SPCp437Grid *this_); + +/** + * Gets the height of the `Cp437Grid` instance. + * + * # Arguments + * + * - `this`: instance to read from + * + * # Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + */ +size_t sp_cp437_grid_height(const struct SPCp437Grid *this_); + +/** + * Gets an unsafe reference to the data of the `Cp437Grid` instance. + * + * ## Safety + * + * The caller has to make sure that: + * + * - `this` points to a valid `Cp437Grid` + * - the returned memory range is never accessed after the passed `Cp437Grid` has been freed + * - the returned memory range is never accessed concurrently, either via the `Cp437Grid` or directly + */ +struct SPByteSlice sp_cp437_grid_unsafe_data_ref(struct SPCp437Grid *this_); + #ifdef __cplusplus } // extern "C" #endif // __cplusplus diff --git a/crates/servicepoint_binding_c/src/lib.rs b/crates/servicepoint_binding_c/src/lib.rs index fc60693..159b64d 100644 --- a/crates/servicepoint_binding_c/src/lib.rs +++ b/crates/servicepoint_binding_c/src/lib.rs @@ -26,30 +26,22 @@ //! } //! ``` -pub use bit_vec::*; -pub use brightness_grid::*; -pub use byte_slice::*; -pub use command::*; -pub use connection::*; -pub use constants::*; -pub use cp437_grid::*; -pub use packet::*; -pub use pixel_grid::*; +pub use crate::bit_vec::*; +pub use crate::brightness_grid::*; +pub use crate::byte_slice::*; +pub use crate::command::*; +pub use crate::connection::*; +pub use crate::constants::*; +pub use crate::cp437_grid::*; +pub use crate::packet::*; +pub use crate::pixel_grid::*; mod bit_vec; - mod brightness_grid; - -mod command; - -mod connection; - -mod packet; - -mod pixel_grid; - mod byte_slice; - -mod cp437_grid; - +mod command; +mod connection; mod constants; +mod cp437_grid; +mod packet; +mod pixel_grid;