generate some functions with macros, cbindgen 0.29

This commit is contained in:
Vinzenz Schroeter 2025-06-16 21:26:58 +02:00
parent db94fecbb3
commit 02f629c68b
23 changed files with 195 additions and 349 deletions

15
Cargo.lock generated
View file

@ -106,9 +106,9 @@ dependencies = [
[[package]] [[package]]
name = "cbindgen" name = "cbindgen"
version = "0.28.0" version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eadd868a2ce9ca38de7eeafdcec9c7065ef89b42b32f0839278d55f35c54d1ff" checksum = "975982cdb7ad6a142be15bdf84aea7ec6a9e5d4d797c004d43185b24cfe4e684"
dependencies = [ dependencies = [
"clap", "clap",
"heck", "heck",
@ -263,9 +263,9 @@ checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3"
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.4.1" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]] [[package]]
name = "indexmap" name = "indexmap"
@ -368,6 +368,12 @@ version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad"
[[package]]
name = "paste"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.32" version = "0.3.32"
@ -540,6 +546,7 @@ version = "0.15.0"
dependencies = [ dependencies = [
"cbindgen", "cbindgen",
"env_logger", "env_logger",
"paste",
"servicepoint", "servicepoint",
] ]

View file

@ -15,7 +15,7 @@ keywords = ["cccb", "cccb-servicepoint", "cbindgen"]
crate-type = ["staticlib", "cdylib", "rlib"] crate-type = ["staticlib", "cdylib", "rlib"]
[build-dependencies] [build-dependencies]
cbindgen = "0.28.0" cbindgen = "0.29.0"
[dependencies.servicepoint] [dependencies.servicepoint]
version = "0.15.0" version = "0.15.0"
@ -25,6 +25,9 @@ default-features = false
version = "0.11.8" version = "0.11.8"
optional = true optional = true
[dependencies]
paste = "1.0.15"
[features] [features]
all_compressions = ["servicepoint/all_compressions"] all_compressions = ["servicepoint/all_compressions"]
default = ["all_compressions", "servicepoint/default", "env_logger"] default = ["all_compressions", "servicepoint/default", "env_logger"]

View file

@ -8,6 +8,13 @@ use std::{env, fs};
fn main() { fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let is_recursive = env::var("SERVICEPOINT_IS_BUILDING")
.unwrap_or("".to_string())
== "true";
unsafe {
env::set_var("SERVICEPOINT_IS_BUILDING", "true");
}
println!("cargo::rerun-if-changed={crate_dir}/src"); println!("cargo::rerun-if-changed={crate_dir}/src");
println!("cargo::rerun-if-changed={crate_dir}/build.rs"); println!("cargo::rerun-if-changed={crate_dir}/build.rs");
println!("cargo::rerun-if-changed={crate_dir}/Cargo.toml"); println!("cargo::rerun-if-changed={crate_dir}/Cargo.toml");
@ -18,41 +25,32 @@ fn main() {
let config = let config =
cbindgen::Config::from_file(crate_dir.clone() + "/cbindgen.toml") cbindgen::Config::from_file(crate_dir.clone() + "/cbindgen.toml")
.unwrap(); .unwrap();
let output_dir = env::var("OUT_DIR").unwrap();
let output_dir = &env::var("OUT_DIR").unwrap();
let header_file = output_dir.clone() + "/servicepoint.h"; let header_file = output_dir.clone() + "/servicepoint.h";
let bindings = match cbindgen::generate_with_config(crate_dir, config) { let bindings = cbindgen::generate_with_config(crate_dir, config)
Ok(bindings) => bindings, .expect("Servicepoint header could not be generated");
Err(e) => {
eprintln!("cargo:warning=Servicepoint header could not be generated: {e:?}");
return;
}
};
bindings.write_to_file(&header_file); bindings.write_to_file(&header_file);
println!("cargo:include={output_dir}"); println!("cargo:include={output_dir}");
if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") { if is_recursive {
let header_copy = header_out + "/servicepoint.h";
if fs::exists(&header_copy).unwrap_or(false) {
// check if content changed to prevent rebuild of dependents if not
let mut bindings_text = Vec::new();
bindings.write(&mut bindings_text);
match fs::read(&header_copy) {
Ok(old_content) if old_content == bindings_text => {
println!("cargo:warning=Header did not change, not updating timestamp");
return; return;
} }
_ => {}
}; if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") {
if !fs::exists(&header_out).unwrap() {
panic!(
"SERVICEPOINT_HEADER_OUT is not set to an existing directory"
);
} }
// file does not exist or is different let header_copy = header_out + "/servicepoint.h";
println!("cargo:warning=Copying header to {header_copy}"); println!("cargo:warning=Copying header to {header_copy}");
fs::copy(header_file, &header_copy).unwrap(); fs::copy(header_file, &header_copy)
.expect("header could not be copied to SERVICEPOINT_HEADER_OUT");
println!("cargo::rerun-if-changed={header_copy}"); println!("cargo::rerun-if-changed={header_copy}");
} }
} }

View file

@ -30,7 +30,8 @@ include = ["servicepoint", "std"]
extra_bindings = ["servicepoint", "servicepoint_binding_c"] extra_bindings = ["servicepoint", "servicepoint_binding_c"]
[parse.expand] [parse.expand]
features = ["full"] crates = ["servicepoint_binding_c", "paste"]
features = []
[export] [export]
include = [] include = []

View file

@ -1,7 +1,7 @@
#ifndef SERVICEPOINT_BINDINGS_C #ifndef SERVICEPOINT_BINDINGS_C
#define SERVICEPOINT_BINDINGS_C #define SERVICEPOINT_BINDINGS_C
/* Generated with cbindgen:0.28.0 */ /* Generated with cbindgen:0.29.0 */
/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */ /* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */
@ -640,9 +640,9 @@ extern "C" {
void init_env_logger(void); void init_env_logger(void);
/** /**
* Clones a [Bitmap]. *Clones a [Bitmap] instance.
*/ */
struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ bitmap); struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance);
/** /**
* Sets the state of all pixels in the [Bitmap]. * Sets the state of all pixels in the [Bitmap].
@ -655,9 +655,9 @@ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ bitmap);
void sp_bitmap_fill(struct Bitmap */*notnull*/ bitmap, bool value); void sp_bitmap_fill(struct Bitmap */*notnull*/ bitmap, bool value);
/** /**
* Deallocates a [Bitmap]. *Deallocates a [Bitmap] instance.
*/ */
void sp_bitmap_free(struct Bitmap */*notnull*/ bitmap); void sp_bitmap_free(struct Bitmap */*notnull*/ instance);
/** /**
* Tries to convert the BitVec to a Bitmap. * Tries to convert the BitVec to a Bitmap.
@ -801,9 +801,9 @@ struct ByteSlice sp_bitmap_unsafe_data_ref(struct Bitmap */*notnull*/ bitmap);
size_t sp_bitmap_width(struct Bitmap */*notnull*/ bitmap); size_t sp_bitmap_width(struct Bitmap */*notnull*/ bitmap);
/** /**
* Clones a [DisplayBitVec]. *Clones a [DisplayBitVec] instance.
*/ */
BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ bit_vec); BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance);
/** /**
* Sets the value of all bits in the [DisplayBitVec]. * Sets the value of all bits in the [DisplayBitVec].
@ -816,9 +816,9 @@ BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ bit_vec);
void sp_bitvec_fill(BitVec */*notnull*/ bit_vec, bool value); void sp_bitvec_fill(BitVec */*notnull*/ bit_vec, bool value);
/** /**
* Deallocates a [DisplayBitVec]. *Deallocates a [DisplayBitVec] instance.
*/ */
void sp_bitvec_free(BitVec */*notnull*/ bit_vec); void sp_bitvec_free(BitVec */*notnull*/ instance);
/** /**
* Gets the value of a bit from the [DisplayBitVec]. * Gets the value of a bit from the [DisplayBitVec].
@ -915,9 +915,9 @@ void sp_bitvec_set(BitVec */*notnull*/ bit_vec, size_t index, bool value);
struct ByteSlice sp_bitvec_unsafe_data_ref(BitVec */*notnull*/ bit_vec); struct ByteSlice sp_bitvec_unsafe_data_ref(BitVec */*notnull*/ bit_vec);
/** /**
* Clones a [BrightnessGrid]. *Clones a [BrightnessGrid] instance.
*/ */
BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ grid); BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ instance);
/** /**
* Sets the value of all cells in the [BrightnessGrid]. * Sets the value of all cells in the [BrightnessGrid].
@ -931,9 +931,9 @@ void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ brightness_grid,
Brightness value); Brightness value);
/** /**
* Deallocates a [BrightnessGrid]. *Deallocates a [BrightnessGrid] instance.
*/ */
void sp_brightness_grid_free(BrightnessGrid */*notnull*/ brightness_grid); void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance);
/** /**
* Gets the current value at the specified position. * Gets the current value at the specified position.
@ -1051,9 +1051,9 @@ struct ByteSlice sp_brightness_grid_unsafe_data_ref(BrightnessGrid */*notnull*/
size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ brightness_grid); size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ brightness_grid);
/** /**
* Clones a [CharGrid]. *Clones a [CharGrid] instance.
*/ */
CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ grid); CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance);
/** /**
* Sets the value of all cells in the [CharGrid]. * Sets the value of all cells in the [CharGrid].
@ -1066,9 +1066,9 @@ CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ grid);
void sp_char_grid_fill(CharGrid */*notnull*/ char_grid, uint32_t value); void sp_char_grid_fill(CharGrid */*notnull*/ char_grid, uint32_t value);
/** /**
* Deallocates a [CharGrid]. *Deallocates a [CharGrid] instance.
*/ */
void sp_char_grid_free(CharGrid */*notnull*/ char_grid); void sp_char_grid_free(CharGrid */*notnull*/ instance);
/** /**
* Returns the current value at the specified position. * Returns the current value at the specified position.
@ -1157,16 +1157,14 @@ void sp_char_grid_set(CharGrid */*notnull*/ char_grid,
size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid); size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid);
/** /**
* Clones an [BitmapCommand] instance. *Clones a [BitmapCommand] instance.
*
* returns: a new [BitmapCommand] instance.
*/ */
struct BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(struct BitmapCommand */*notnull*/ command); struct BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(struct BitmapCommand */*notnull*/ instance);
/** /**
*Deallocates a [BitmapCommand] instance. *Deallocates a [BitmapCommand] instance.
*/ */
void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ command); void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance);
/** /**
* Move the provided [Bitmap] into a new [BitmapCommand], * Move the provided [Bitmap] into a new [BitmapCommand],
@ -1237,16 +1235,14 @@ void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command,
struct Packet *sp_cmd_bitmap_try_into_packet(struct BitmapCommand */*notnull*/ command); struct Packet *sp_cmd_bitmap_try_into_packet(struct BitmapCommand */*notnull*/ command);
/** /**
* Clones an [BitVecCommand] instance. *Clones a [BitVecCommand] instance.
*
* returns: a new [BitVecCommand] instance.
*/ */
struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*notnull*/ command); struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*notnull*/ instance);
/** /**
* Deallocates a [BitVecCommand]. *Deallocates a [BitVecCommand] instance.
*/ */
void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ command); void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance);
/** /**
* Returns a pointer to the [BitVec] contained in the [BitVecCommand]. * Returns a pointer to the [BitVec] contained in the [BitVecCommand].
@ -1319,13 +1315,14 @@ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ command,
struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command);
/** /**
* Clones an [GlobalBrightnessCommand] instance. *Clones a [GlobalBrightnessCommand] instance.
*
* returns: a new [GlobalBrightnessCommand] instance.
*/ */
struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struct GlobalBrightnessCommand */*notnull*/ command); struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struct GlobalBrightnessCommand */*notnull*/ instance);
void sp_cmd_brightness_global_free(struct BitmapCommand */*notnull*/ command); /**
*Deallocates a [GlobalBrightnessCommand] instance.
*/
void sp_cmd_brightness_global_free(struct GlobalBrightnessCommand */*notnull*/ instance);
Brightness sp_cmd_brightness_global_get(struct GlobalBrightnessCommand */*notnull*/ command); Brightness sp_cmd_brightness_global_get(struct GlobalBrightnessCommand */*notnull*/ command);
@ -1345,16 +1342,14 @@ void sp_cmd_brightness_global_set(struct GlobalBrightnessCommand */*notnull*/ co
Brightness brightness); Brightness brightness);
/** /**
* Clones an [BrightnessGridCommand] instance. *Clones a [BrightnessGridCommand] instance.
*
* returns: a new [BrightnessGridCommand] instance.
*/ */
struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(struct BrightnessGridCommand */*notnull*/ command); struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(struct BrightnessGridCommand */*notnull*/ instance);
/** /**
* Deallocates a [BitmapCommand]. *Deallocates a [BrightnessGridCommand] instance.
*/ */
void sp_cmd_brightness_grid_free(struct BitmapCommand */*notnull*/ command); void sp_cmd_brightness_grid_free(struct BrightnessGridCommand */*notnull*/ instance);
/** /**
* Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand],
@ -1406,16 +1401,14 @@ void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/
size_t origin_y); size_t origin_y);
/** /**
* Clones an [CharGridCommand] instance. *Clones a [CharGridCommand] instance.
*
* returns: a new [CharGridCommand] instance.
*/ */
struct CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(struct CharGridCommand */*notnull*/ command); struct CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(struct CharGridCommand */*notnull*/ instance);
/** /**
* Deallocates a [BitmapCommand]. *Deallocates a [CharGridCommand] instance.
*/ */
void sp_cmd_char_grid_free(struct BitmapCommand */*notnull*/ command); void sp_cmd_char_grid_free(struct CharGridCommand */*notnull*/ instance);
/** /**
* Moves the provided [CharGrid] into a new [CharGridCommand], * Moves the provided [CharGrid] into a new [CharGridCommand],
@ -1467,9 +1460,9 @@ void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command,
struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command); struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command);
/** /**
* Deallocates a [ClearCommand]. *Deallocates a [ClearCommand] instance.
*/ */
void sp_cmd_clear_free(struct ClearCommand */*notnull*/ command); void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance);
/** /**
* Set all pixels to the off state. * Set all pixels to the off state.
@ -1481,16 +1474,14 @@ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ command);
struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); struct ClearCommand */*notnull*/ sp_cmd_clear_new(void);
/** /**
* Clones an [Cp437GridCommand] instance. *Clones a [Cp437GridCommand] instance.
*
* returns: a new [Cp437GridCommand] instance.
*/ */
struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(struct Cp437GridCommand */*notnull*/ command); struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(struct Cp437GridCommand */*notnull*/ instance);
/** /**
* Deallocates a [Cp437GridCommand]. *Deallocates a [Cp437GridCommand] instance.
*/ */
void sp_cmd_cp437_grid_free(struct BitmapCommand */*notnull*/ command); void sp_cmd_cp437_grid_free(struct Cp437GridCommand */*notnull*/ instance);
/** /**
* Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * Moves the provided [Cp437Grid] into a new [Cp437GridCommand],
@ -1553,9 +1544,9 @@ void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command,
struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command);
/** /**
* Deallocates a [FadeOutCommand]. *Deallocates a [FadeOutCommand] instance.
*/ */
void sp_cmd_fade_out_free(struct ClearCommand */*notnull*/ command); void sp_cmd_fade_out_free(struct FadeOutCommand */*notnull*/ instance);
/** /**
* A yet-to-be-tested command. * A yet-to-be-tested command.
@ -1603,9 +1594,9 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command);
struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet);
/** /**
* Deallocates a [HardResetCommand]. *Deallocates a [HardResetCommand] instance.
*/ */
void sp_cmd_hard_reset_free(struct ClearCommand */*notnull*/ command); void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance);
/** /**
* Kills the udp daemon on the display, which usually results in a restart. * Kills the udp daemon on the display, which usually results in a restart.
@ -1617,9 +1608,9 @@ void sp_cmd_hard_reset_free(struct ClearCommand */*notnull*/ command);
struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void);
/** /**
* Clones a [Cp437Grid]. *Clones a [Cp437Grid] instance.
*/ */
Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ grid); Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance);
/** /**
* Sets the value of all cells in the [Cp437Grid]. * Sets the value of all cells in the [Cp437Grid].
@ -1632,9 +1623,9 @@ Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ grid);
void sp_cp437_grid_fill(Cp437Grid */*notnull*/ cp437_grid, uint8_t value); void sp_cp437_grid_fill(Cp437Grid */*notnull*/ cp437_grid, uint8_t value);
/** /**
* Deallocates a [Cp437Grid]. *Deallocates a [Cp437Grid] instance.
*/ */
void sp_cp437_grid_free(Cp437Grid */*notnull*/ cp437_grid); void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance);
/** /**
* Gets the current value at the specified position. * Gets the current value at the specified position.
@ -1723,16 +1714,14 @@ struct ByteSlice sp_cp437_grid_unsafe_data_ref(Cp437Grid */*notnull*/ cp437_grid
size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ cp437_grid); size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ cp437_grid);
/** /**
* Clones a [Packet]. *Clones a [Packet] instance.
*
* returns: a new [Packet] instance.
*/ */
struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ packet); struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ instance);
/** /**
* Deallocates a [Packet]. *Deallocates a [Packet] instance.
*/ */
void sp_packet_free(struct Packet */*notnull*/ packet); void sp_packet_free(struct Packet */*notnull*/ instance);
/** /**
* Creates a raw [Packet] from parts. * Creates a raw [Packet] from parts.
@ -1792,9 +1781,9 @@ bool sp_u16_to_command_code(uint16_t code,
CommandCode *result); CommandCode *result);
/** /**
* Closes and deallocates a [UdpSocket]. *Deallocates a [UdpSocket] instance.
*/ */
void sp_udp_free(struct UdpSocket */*notnull*/ connection); void sp_udp_free(struct UdpSocket */*notnull*/ instance);
/** /**
* Creates a new instance of [UdpSocket]. * Creates a new instance of [UdpSocket].

View file

@ -1,5 +1,6 @@
use crate::mem::{ use crate::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
}; };
use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet};
use std::ptr::NonNull; use std::ptr::NonNull;
@ -44,21 +45,8 @@ pub unsafe extern "C" fn sp_cmd_bitmap_try_into_packet(
heap_move_ok(unsafe { heap_remove(command) }.try_into()) heap_move_ok(unsafe { heap_remove(command) }.try_into())
} }
/// Clones an [BitmapCommand] instance. wrap_clone!(BitmapCommand, sp_cmd_bitmap);
/// wrap_free!(BitmapCommand, sp_cmd_bitmap);
/// returns: a new [BitmapCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitmap_clone(
command: NonNull<BitmapCommand>,
) -> NonNull<BitmapCommand> {
unsafe { heap_clone(command) }
}
/// Deallocates a [BitmapCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitmap_free(command: NonNull<BitmapCommand>) {
unsafe { heap_drop(command) }
}
/// Returns a pointer to the provided `BitmapCommand`. /// Returns a pointer to the provided `BitmapCommand`.
/// ///

View file

@ -1,5 +1,6 @@
use crate::mem::{ use crate::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
}; };
use servicepoint::{ use servicepoint::{
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset,
@ -44,21 +45,8 @@ pub unsafe extern "C" fn sp_cmd_bitvec_try_into_packet(
heap_move_ok(unsafe { heap_remove(command) }.try_into()) heap_move_ok(unsafe { heap_remove(command) }.try_into())
} }
/// Clones an [BitVecCommand] instance. wrap_clone!(BitVecCommand, sp_cmd_bitvec);
/// wrap_free!(BitVecCommand, sp_cmd_bitvec);
/// returns: a new [BitVecCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitvec_clone(
command: NonNull<BitVecCommand>,
) -> NonNull<BitVecCommand> {
unsafe { heap_clone(command) }
}
/// Deallocates a [BitVecCommand].
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitvec_free(command: NonNull<BitVecCommand>) {
unsafe { heap_drop(command) }
}
/// Returns a pointer to the [BitVec] contained in the [BitVecCommand]. /// Returns a pointer to the [BitVec] contained in the [BitVecCommand].
#[no_mangle] #[no_mangle]

View file

@ -1,9 +1,8 @@
use crate::mem::{ use crate::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, macros::{wrap_clone, wrap_free},
}; mem::{heap_move_nonnull, heap_move_ok, heap_remove},
use servicepoint::{
BitmapCommand, BrightnessGrid, BrightnessGridCommand, Origin, Packet,
}; };
use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin, Packet};
use std::ptr::NonNull; use std::ptr::NonNull;
/// Set the brightness of individual tiles in a rectangular area of the display. /// Set the brightness of individual tiles in a rectangular area of the display.
@ -42,23 +41,8 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_into_packet(
heap_move_ok(unsafe { heap_remove(command) }.try_into()) heap_move_ok(unsafe { heap_remove(command) }.try_into())
} }
/// Clones an [BrightnessGridCommand] instance. wrap_clone!(BrightnessGridCommand, sp_cmd_brightness_grid);
/// wrap_free!(BrightnessGridCommand, sp_cmd_brightness_grid);
/// returns: a new [BrightnessGridCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_grid_clone(
command: NonNull<BrightnessGridCommand>,
) -> NonNull<BrightnessGridCommand> {
unsafe { heap_clone(command) }
}
/// Deallocates a [BitmapCommand].
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_grid_free(
command: NonNull<BitmapCommand>,
) {
unsafe { heap_drop(command) }
}
/// Moves the provided [BrightnessGrid] to be contained in the [BrightnessGridCommand]. /// Moves the provided [BrightnessGrid] to be contained in the [BrightnessGridCommand].
#[no_mangle] #[no_mangle]

View file

@ -1,4 +1,4 @@
use crate::mem::{heap_drop, heap_move_nonnull}; use crate::{macros::wrap_free, mem::heap_move_nonnull};
use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand};
use std::ptr::NonNull; use std::ptr::NonNull;
@ -12,11 +12,7 @@ pub unsafe extern "C" fn sp_cmd_clear_new() -> NonNull<ClearCommand> {
heap_move_nonnull(ClearCommand) heap_move_nonnull(ClearCommand)
} }
/// Deallocates a [ClearCommand]. wrap_free!(ClearCommand, sp_cmd_clear);
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_clear_free(command: NonNull<ClearCommand>) {
unsafe { heap_drop(command) }
}
/// Kills the udp daemon on the display, which usually results in a restart. /// Kills the udp daemon on the display, which usually results in a restart.
/// ///
@ -28,13 +24,7 @@ pub unsafe extern "C" fn sp_cmd_hard_reset_new() -> NonNull<HardResetCommand> {
heap_move_nonnull(HardResetCommand) heap_move_nonnull(HardResetCommand)
} }
/// Deallocates a [HardResetCommand]. wrap_free!(HardResetCommand, sp_cmd_hard_reset);
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_hard_reset_free(
command: NonNull<ClearCommand>,
) {
unsafe { heap_drop(command) }
}
/// A yet-to-be-tested command. /// A yet-to-be-tested command.
/// ///
@ -44,8 +34,4 @@ pub unsafe extern "C" fn sp_cmd_fade_out_new() -> NonNull<FadeOutCommand> {
heap_move_nonnull(FadeOutCommand) heap_move_nonnull(FadeOutCommand)
} }
/// Deallocates a [FadeOutCommand]. wrap_free!(FadeOutCommand, sp_cmd_fade_out);
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_fade_out_free(command: NonNull<ClearCommand>) {
unsafe { heap_drop(command) }
}

View file

@ -1,7 +1,8 @@
use crate::mem::{ use crate::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
}; };
use servicepoint::{BitmapCommand, CharGrid, CharGridCommand, Origin, Packet}; use servicepoint::{CharGrid, CharGridCommand, Origin, Packet};
use std::ptr::NonNull; use std::ptr::NonNull;
/// Show UTF-8 encoded text on the screen. /// Show UTF-8 encoded text on the screen.
@ -40,23 +41,8 @@ pub unsafe extern "C" fn sp_cmd_char_grid_try_into_packet(
heap_move_ok(unsafe { heap_remove(command) }.try_into()) heap_move_ok(unsafe { heap_remove(command) }.try_into())
} }
/// Clones an [CharGridCommand] instance. wrap_clone!(CharGridCommand, sp_cmd_char_grid);
/// wrap_free!(CharGridCommand, sp_cmd_char_grid);
/// returns: a new [CharGridCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_clone(
command: NonNull<CharGridCommand>,
) -> NonNull<CharGridCommand> {
unsafe { heap_clone(command) }
}
/// Deallocates a [BitmapCommand].
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_free(
command: NonNull<BitmapCommand>,
) {
unsafe { heap_drop(command) }
}
/// Moves the provided [CharGrid] to be contained in the [CharGridCommand]. /// Moves the provided [CharGrid] to be contained in the [CharGridCommand].
#[no_mangle] #[no_mangle]

View file

@ -1,9 +1,8 @@
use crate::mem::{ use crate::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, macros::{wrap_clone, wrap_free},
}; mem::{heap_move_nonnull, heap_move_ok, heap_remove},
use servicepoint::{
BitmapCommand, Cp437Grid, Cp437GridCommand, Origin, Packet,
}; };
use servicepoint::{Cp437Grid, Cp437GridCommand, Origin, Packet};
use std::ptr::NonNull; use std::ptr::NonNull;
/// Show text on the screen. /// Show text on the screen.
@ -42,23 +41,8 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_try_into_packet(
heap_move_ok(unsafe { heap_remove(command) }.try_into()) heap_move_ok(unsafe { heap_remove(command) }.try_into())
} }
/// Clones an [Cp437GridCommand] instance. wrap_clone!(Cp437GridCommand, sp_cmd_cp437_grid);
/// wrap_free!(Cp437GridCommand, sp_cmd_cp437_grid);
/// returns: a new [Cp437GridCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_cp437_grid_clone(
command: NonNull<Cp437GridCommand>,
) -> NonNull<Cp437GridCommand> {
unsafe { heap_clone(command) }
}
/// Deallocates a [Cp437GridCommand].
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_cp437_grid_free(
command: NonNull<BitmapCommand>,
) {
unsafe { heap_drop(command) }
}
/// Moves the provided bitmap into the provided command. /// Moves the provided bitmap into the provided command.
/// ///

View file

@ -1,7 +1,8 @@
use crate::mem::{heap_clone, heap_drop, heap_move_nonnull, heap_remove}; use crate::{
use servicepoint::{ macros::{wrap_clone, wrap_free},
BitmapCommand, Brightness, GlobalBrightnessCommand, Packet, mem::{heap_move_nonnull, heap_remove},
}; };
use servicepoint::{Brightness, GlobalBrightnessCommand, Packet};
use std::ptr::NonNull; use std::ptr::NonNull;
/// Set the brightness of all tiles to the same value. /// Set the brightness of all tiles to the same value.
@ -21,22 +22,8 @@ pub unsafe extern "C" fn sp_cmd_brightness_global_into_packet(
heap_move_nonnull(unsafe { heap_remove(command) }.into()) heap_move_nonnull(unsafe { heap_remove(command) }.into())
} }
/// Clones an [GlobalBrightnessCommand] instance. wrap_clone!(GlobalBrightnessCommand, sp_cmd_brightness_global);
/// wrap_free!(GlobalBrightnessCommand, sp_cmd_brightness_global);
/// returns: a new [GlobalBrightnessCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_global_clone(
command: NonNull<GlobalBrightnessCommand>,
) -> NonNull<GlobalBrightnessCommand> {
unsafe { heap_clone(command) }
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_global_free(
command: NonNull<BitmapCommand>,
) {
unsafe { heap_drop(command) }
}
/// Moves the provided bitmap to be contained in the command. /// Moves the provided bitmap to be contained in the command.
#[no_mangle] #[no_mangle]

View file

@ -1,9 +1,7 @@
use crate::{ use crate::{
containers::ByteSlice, containers::ByteSlice,
mem::{ macros::{wrap_clone, wrap_free},
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
heap_remove,
},
}; };
use servicepoint::{ use servicepoint::{
Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid, Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid,
@ -82,19 +80,8 @@ pub unsafe extern "C" fn sp_bitmap_from_bitvec(
heap_move_ok(Bitmap::from_bitvec(width, bitvec)) heap_move_ok(Bitmap::from_bitvec(width, bitvec))
} }
/// Clones a [Bitmap]. wrap_clone!(Bitmap, sp_bitmap);
#[no_mangle] wrap_free!(Bitmap, sp_bitmap);
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]. /// Gets the current value at the specified position in the [Bitmap].
/// ///

View file

@ -1,8 +1,7 @@
use crate::{ use crate::{
containers::ByteSlice, containers::ByteSlice,
mem::{ macros::{wrap_clone, wrap_free},
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, mem::{heap_move_nonnull, heap_move_ok, heap_remove},
},
}; };
use servicepoint::{ use servicepoint::{
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet,
@ -36,19 +35,8 @@ pub unsafe extern "C" fn sp_bitvec_load(
heap_move_nonnull(DisplayBitVec::from_slice(data)) heap_move_nonnull(DisplayBitVec::from_slice(data))
} }
/// Clones a [DisplayBitVec]. wrap_clone!(DisplayBitVec, sp_bitvec);
#[no_mangle] wrap_free!(DisplayBitVec, sp_bitvec);
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]. /// Gets the value of a bit from the [DisplayBitVec].
/// ///

View file

@ -1,16 +1,13 @@
use crate::{ use crate::{
containers::ByteSlice, containers::ByteSlice,
mem::{ macros::{wrap_clone, wrap_free},
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
heap_remove,
},
}; };
use servicepoint::{ use servicepoint::{
Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid,
Origin, Packet, Origin, Packet,
}; };
use std::mem::transmute; use std::{mem::transmute, ptr::NonNull};
use std::ptr::NonNull;
/// Creates a new [BrightnessGrid] with the specified dimensions. /// Creates a new [BrightnessGrid] with the specified dimensions.
/// ///
@ -55,21 +52,8 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
) )
} }
/// Clones a [BrightnessGrid]. wrap_clone!(BrightnessGrid, sp_brightness_grid);
#[no_mangle] wrap_free!(BrightnessGrid, sp_brightness_grid);
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. /// Gets the current value at the specified position.
/// ///

View file

@ -1,8 +1,7 @@
use crate::{ use crate::{
containers::ByteSlice, containers::ByteSlice,
mem::{ macros::{wrap_clone, wrap_free},
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, mem::{heap_move_nonnull, heap_move_ok, heap_remove},
},
}; };
use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet};
use std::ptr::NonNull; use std::ptr::NonNull;
@ -40,19 +39,8 @@ pub unsafe extern "C" fn sp_char_grid_load(
heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec()))
} }
/// Clones a [CharGrid]. wrap_clone!(CharGrid, sp_char_grid);
#[no_mangle] wrap_free!(CharGrid, sp_char_grid);
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. /// Returns the current value at the specified position.
/// ///

View file

@ -1,9 +1,7 @@
use crate::{ use crate::{
containers::ByteSlice, containers::ByteSlice,
mem::{ macros::{wrap_clone, wrap_free},
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
heap_remove,
},
}; };
use servicepoint::{ use servicepoint::{
Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet,
@ -32,19 +30,8 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
heap_move_some(Cp437Grid::load(width, height, data)) heap_move_some(Cp437Grid::load(width, height, data))
} }
/// Clones a [Cp437Grid]. wrap_clone!(Cp437Grid, sp_cp437_grid);
#[no_mangle] wrap_free!(Cp437Grid, sp_cp437_grid);
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. /// Gets the current value at the specified position.
/// ///

View file

@ -29,6 +29,7 @@
pub mod commands; pub mod commands;
/// Functions related to [servicepoint::Bitmap], [servicepoint::CharGrid] and friends. /// Functions related to [servicepoint::Bitmap], [servicepoint::CharGrid] and friends.
pub mod containers; pub mod containers;
mod macros;
pub(crate) mod mem; pub(crate) mod mem;
/// Functions related to [Packet]. /// Functions related to [Packet].
pub mod packet; pub mod packet;

25
src/macros.rs Normal file
View file

@ -0,0 +1,25 @@
macro_rules! wrap_free {
($typ:ident, $prefix:ident) => {
paste::paste! {
#[doc = concat!("Deallocates a [", stringify!($typ), "] instance.")]
#[no_mangle]
pub unsafe extern "C" fn [<$prefix _free>](instance: NonNull<$typ>) {
unsafe { crate::mem::heap_drop(instance) }
}
}
};
}
macro_rules! wrap_clone {
($typ:ident, $prefix:ident) => {
paste::paste! {
#[doc = concat!("Clones a [", stringify!($typ), "] instance.")]
#[no_mangle]
pub unsafe extern "C" fn [<$prefix _clone>](instance: NonNull<$typ>) -> NonNull<$typ> {
unsafe { crate::mem::heap_clone(instance) }
}
}
};
}
pub(crate) use {wrap_clone, wrap_free};

View file

@ -1,6 +1,7 @@
use crate::{ use crate::{
containers::ByteSlice, containers::ByteSlice,
mem::{heap_clone, heap_drop, heap_move_nonnull, heap_move_ok}, macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok},
}; };
use servicepoint::{CommandCode, Header, Packet}; use servicepoint::{CommandCode, Header, Packet};
use std::ptr::NonNull; use std::ptr::NonNull;
@ -90,21 +91,8 @@ pub unsafe extern "C" fn sp_packet_serialize_to(
} }
} }
/// Clones a [Packet]. wrap_clone!(Packet, sp_packet);
/// wrap_free!(Packet, sp_packet);
/// returns: a new [Packet] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_packet_clone(
packet: NonNull<Packet>,
) -> NonNull<Packet> {
unsafe { heap_clone(packet) }
}
/// Deallocates a [Packet].
#[no_mangle]
pub unsafe extern "C" fn sp_packet_free(packet: NonNull<Packet>) {
unsafe { heap_drop(packet) }
}
/// Converts u16 into [CommandCode]. /// Converts u16 into [CommandCode].
/// ///

View file

@ -1,6 +1,7 @@
use crate::{ use crate::{
commands::{CommandTag, SPCommand}, commands::{CommandTag, SPCommand},
mem::{heap_drop, heap_move_ok, heap_remove}, macros::wrap_free,
mem::{heap_move_ok, heap_remove},
}; };
use servicepoint::{Header, Packet, UdpSocketExt}; use servicepoint::{Header, Packet, UdpSocketExt};
use std::{ use std::{
@ -143,8 +144,4 @@ pub unsafe extern "C" fn sp_udp_send_header(
.is_ok() .is_ok()
} }
/// Closes and deallocates a [UdpSocket]. wrap_free!(UdpSocket, sp_udp);
#[no_mangle]
pub unsafe extern "C" fn sp_udp_free(connection: NonNull<UdpSocket>) {
unsafe { heap_drop(connection) }
}