From 53493a5fb1401130c879899ce7003e42a9d9e9af Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 4 Jun 2025 22:14:19 +0200 Subject: [PATCH 1/5] actually change build flags --- packages.nix | 133 +++++++++++++++++++++++++-------------------------- 1 file changed, 65 insertions(+), 68 deletions(-) diff --git a/packages.nix b/packages.nix index 24cf51f..348d98c 100644 --- a/packages.nix +++ b/packages.nix @@ -11,9 +11,12 @@ let { rustPlatform, pkgs, + buildType ? "release", + buildNoDefaultFeatures ? false, + cargoBuildFlags ? [ ], }: rustPlatform.buildRustPackage (finalAttrs: { - inherit version; + inherit version buildType cargoBuildFlags; pname = "servicepoint-binding-c"; src = lib.filter { @@ -57,9 +60,14 @@ let name, servicepointBinding, pkgs, + EXTRA_CFLAGS ? "", + static ? false, }: + let + staticPkgConfigParam = if static then "--static" else ""; + in pkgs.gccStdenv.mkDerivation { - inherit version; + inherit version EXTRA_CFLAGS; pname = "servicepoint-c-example-${name}"; nativeBuildInputs = [ pkgs.pkg-config ]; buildInputs = [ @@ -71,19 +79,12 @@ let set -e set -x mkdir -p $out/bin - $CC ${name}.c $CFLAGS $EXTRA_CFLAGS $(pkg-config --libs --cflags servicepoint) $(pkg-config --libs --cflags liblzma) -o $out/bin/${name} + $CC ${name}.c ${if static then "-static" else ""} $CFLAGS $EXTRA_CFLAGS \ + $(pkg-config --libs --cflags ${staticPkgConfigParam} servicepoint) \ + $(pkg-config --libs --cflags ${staticPkgConfigParam} liblzma) \ + -o $out/bin/${name} ''; }; - rustPlatform-stable = pkgs.rustPlatform; - rustPlatform-unstable = pkgs.makeRustPlatform { - cargo = fenix.minimal.cargo; - rustc = fenix.minimal.rustc; - }; - rustPlatform-musl-stable = pkgs.pkgsMusl.rustPlatform; - rustPlatform-musl-unstable = pkgs.pkgsMusl.makeRustPlatform { - cargo = fenix.minimal.cargo; - rustc = fenix.minimal.rustc; - }; examples = [ "announce" "brightness_tester" @@ -92,6 +93,12 @@ let "random_stuff" "wiping_clear" ]; + mkAllExamples = + suffix: + pkgs.symlinkJoin { + name = "servicepoint-all-examples"; + paths = builtins.map (e: selfPkgs."${e}${suffix}") examples; + }; size-cflags = [ "-Oz" "-fwrapv" @@ -112,54 +119,57 @@ let "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL" ]; - servicepoint-stable-size-args = { + stable-size-args = { buildType = "size_optimized"; buildNoDefaultFeatures = true; }; - servicepoint-unstable-size-args = { - # TODO: do these override the nix flags? - CARGOFLAGS = ''-Zbuild-std="core,std,alloc,proc_macro,panic_abort" -Zbuild-std-features="panic_immediate_abort"''; + unstable-size-args = { + cargoBuildFlags = ''-Zbuild-std="core,std,alloc,proc_macro,panic_abort" -Zbuild-std-features="panic_immediate_abort"''; # TODO: those override the nix flags # NIX_CFLAGS_COMPILE = builtins.toString ["-Oz" "-fwrapv" "-fomit-frame-pointer" "-fno-stack-protector" "-fno-unroll-loops" "-fno-unwind-tables" "-fno-asynchronous-unwind-tables" "-fmerge-all-constants" "-fvisibility=hidden" "-Bsymbolic" "-fno-ident" "-fno-exceptions" "-ffunction-sections" "-fdata-sections"]; # NIX_CFLAGS_LINK = builtins.toString ["Wl,-z,norelro" "-Wl,--hash-style=gnu" "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL"]; }; - mkAllExamples = - suffix: - pkgs.symlinkJoin { - name = "servicepoint-all-examples"; - paths = builtins.map (e: selfPkgs."${e}${suffix}") examples; - }; -in -rec { - servicepoint-binding-c-stable-release = mkServicepoint { + rustPlatform-stable = pkgs.rustPlatform; + rustPlatform-nightly = pkgs.makeRustPlatform { + inherit (fenix.minimal) cargo rustc; + }; + rustPlatform-musl-stable = pkgs.pkgsMusl.rustPlatform; + rustPlatform-musl-nightly = pkgs.pkgsMusl.makeRustPlatform { + inherit (fenix.minimal) cargo rustc; + }; + stable-release-args = { inherit pkgs; rustPlatform = rustPlatform-stable; }; - servicepoint-binding-c-nightly-release = mkServicepoint { + nightly-release-args = { inherit pkgs; - rustPlatform = rustPlatform-unstable; + rustPlatform = rustPlatform-nightly; }; - servicepoint-binding-c-musl-stable-release = mkServicepoint { + musl-stable-release-args = { pkgs = pkgs.pkgsMusl; rustPlatform = rustPlatform-musl-stable; }; - servicepoint-binding-c-musl-nightly-release = mkServicepoint { + musl-nightly-release-args = { pkgs = pkgs.pkgsMusl; - rustPlatform = rustPlatform-musl-unstable; + rustPlatform = rustPlatform-musl-nightly; }; +in +rec { + servicepoint-binding-c-stable-release = mkServicepoint stable-release-args; + servicepoint-binding-c-nightly-release = mkServicepoint nightly-release-args; + servicepoint-binding-c-musl-stable-release = mkServicepoint musl-stable-release-args; + servicepoint-binding-c-musl-nightly-release = mkServicepoint musl-nightly-release-args; - servicepoint-binding-c-stable-size = - servicepoint-binding-c-stable-release // servicepoint-stable-size-args; - servicepoint-binding-c-nightly-size = - servicepoint-binding-c-nightly-release - // servicepoint-stable-size-args - // servicepoint-unstable-size-args; - servicepoint-binding-c-musl-stable-size = - servicepoint-binding-c-musl-stable-release // servicepoint-stable-size-args; - servicepoint-binding-c-musl-nightly-size = - servicepoint-binding-c-musl-nightly-release - // servicepoint-stable-size-args - // servicepoint-unstable-size-args; + servicepoint-binding-c-stable-size = mkServicepoint (stable-release-args // stable-size-args); + servicepoint-binding-c-nightly-size = mkServicepoint ( + nightly-release-args // stable-size-args // unstable-size-args + ); + servicepoint-binding-c-musl-stable-size = mkServicepoint ( + musl-stable-release-args // stable-size-args + ); + servicepoint-binding-c-musl-nightly-size = mkServicepoint ( + musl-nightly-release-args // stable-size-args // unstable-size-args + ); # default variants servicepoint-binding-c = servicepoint-binding-c-stable-release; @@ -181,26 +191,16 @@ rec { } )) # construct another pakage per example, but optimized for size with unstable rust -// (lib.mapAttrs' - ( - name: value: - lib.nameValuePair ("${name}-size") ( - value - // { - EXTRA_CFLAGS = builtins.toString size-cflags; - } - ) +// (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-size") value) ( + lib.genAttrs examples ( + name: + mkExample { + inherit name pkgs; + servicepointBinding = selfPkgs.servicepoint-binding-c-nightly-size; + EXTRA_CFLAGS = builtins.toString size-cflags; + } ) - ( - lib.genAttrs examples ( - name: - mkExample { - inherit name pkgs; - servicepointBinding = selfPkgs.servicepoint-binding-c-nightly-size; - } - ) - ) -) +)) # construct another pakage per example, but with musl // (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-musl") value) ( lib.genAttrs examples ( @@ -220,9 +220,7 @@ rec { inherit name; pkgs = pkgs.pkgsMusl; servicepointBinding = selfPkgs.servicepoint-binding-c-musl; - } - // { - EXTRA_CFLAGS = "-static"; + static = true; } ) )) @@ -234,9 +232,8 @@ rec { inherit name; pkgs = pkgs.pkgsMusl; servicepointBinding = selfPkgs.servicepoint-binding-c-musl; - } - // { - EXTRA_CFLAGS = "-static" + builtins.toString size-cflags; + static = true; + EXTRA_CFLAGS = builtins.toString size-cflags; } ) )) From db94fecbb333242f80ed69f013f421be2a17a259 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 5 Jun 2025 17:38:54 +0200 Subject: [PATCH 2/5] more fixes --- nix-build-all.sh | 28 +++++++++++----------- packages.nix | 60 +++++++++++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/nix-build-all.sh b/nix-build-all.sh index 2973cbc..8adce87 100755 --- a/nix-build-all.sh +++ b/nix-build-all.sh @@ -3,22 +3,24 @@ set -e set -x -BUILD="nix build" +BUILD="nix build -L" -$BUILD .#servicepoint-binding-c +$BUILD .#servicepoint-binding-c -o result -$BUILD .#servicepoint-binding-c-stable-release -$BUILD .#servicepoint-binding-c-stable-size -$BUILD .#servicepoint-binding-c-nightly-release -$BUILD .#servicepoint-binding-c-nightly-size +$BUILD .#servicepoint-binding-c-stable-release -o result-stable-release +$BUILD .#servicepoint-binding-c-stable-size -o result-stable-size +$BUILD .#servicepoint-binding-c-nightly-release -o result-nightly-release +$BUILD .#servicepoint-binding-c-nightly-size -o result-nightly-size +$BUILD .#servicepoint-binding-c-musl-stable-release -o result-musl-release +$BUILD .#servicepoint-binding-c-musl-stable-size -o result-musl-size -$BUILD .#servicepoint-binding-c-musl-stable-release -$BUILD .#servicepoint-binding-c-musl-stable-size +# do not work yet: # $BUILD .#servicepoint-binding-c-musl-nightly-release # $BUILD .#servicepoint-binding-c-musl-nightly-size -$BUILD .#all-examples -$BUILD .#all-examples-size -$BUILD .#all-examples-musl -$BUILD .#all-examples-musl-static -$BUILD .#all-examples-musl-static-size +$BUILD .#all-examples -o result-examples +$BUILD .#all-examples-size -o result-examples-size +$BUILD .#all-examples-nightly-size -o result-nightly-size +$BUILD .#all-examples-musl -o result-examples-musl +$BUILD .#all-examples-musl-static -o result-examples-musl-static +$BUILD .#all-examples-musl-static-size -o result-examples-musl-static-size diff --git a/packages.nix b/packages.nix index 348d98c..8096f54 100644 --- a/packages.nix +++ b/packages.nix @@ -14,6 +14,7 @@ let buildType ? "release", buildNoDefaultFeatures ? false, cargoBuildFlags ? [ ], + nativeBuildInputs ? [] }: rustPlatform.buildRustPackage (finalAttrs: { inherit version buildType cargoBuildFlags; @@ -38,7 +39,7 @@ let license = lib.licenses.gpl3Plus; pkgConfigModules = [ "servicepoint" ]; }; - nativeBuildInputs = [ pkgs.pkg-config ]; + nativeBuildInputs = [ pkgs.pkg-config ] ++ nativeBuildInputs; buildInputs = [ pkgs.xz ]; preBuild = '' @@ -119,24 +120,10 @@ let "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL" ]; - stable-size-args = { - buildType = "size_optimized"; - buildNoDefaultFeatures = true; - }; - unstable-size-args = { - cargoBuildFlags = ''-Zbuild-std="core,std,alloc,proc_macro,panic_abort" -Zbuild-std-features="panic_immediate_abort"''; - # TODO: those override the nix flags - # NIX_CFLAGS_COMPILE = builtins.toString ["-Oz" "-fwrapv" "-fomit-frame-pointer" "-fno-stack-protector" "-fno-unroll-loops" "-fno-unwind-tables" "-fno-asynchronous-unwind-tables" "-fmerge-all-constants" "-fvisibility=hidden" "-Bsymbolic" "-fno-ident" "-fno-exceptions" "-ffunction-sections" "-fdata-sections"]; - # NIX_CFLAGS_LINK = builtins.toString ["Wl,-z,norelro" "-Wl,--hash-style=gnu" "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL"]; - }; rustPlatform-stable = pkgs.rustPlatform; - rustPlatform-nightly = pkgs.makeRustPlatform { - inherit (fenix.minimal) cargo rustc; - }; + rustPlatform-nightly = pkgs.makeRustPlatform fenix.complete; rustPlatform-musl-stable = pkgs.pkgsMusl.rustPlatform; - rustPlatform-musl-nightly = pkgs.pkgsMusl.makeRustPlatform { - inherit (fenix.minimal) cargo rustc; - }; + rustPlatform-musl-nightly = pkgs.pkgsMusl.makeRustPlatform fenix.complete; stable-release-args = { inherit pkgs; rustPlatform = rustPlatform-stable; @@ -153,6 +140,21 @@ let pkgs = pkgs.pkgsMusl; rustPlatform = rustPlatform-musl-nightly; }; + stable-size-args = { + buildType = "size_optimized"; + buildNoDefaultFeatures = true; + }; + nightly-size-args = { + cargoBuildFlags = [ + "-Zbuild-std=core,std,alloc,proc_macro,panic_abort" + "-Zbuild-std-features=panic_immediate_abort" + ]; + # TODO: remove hard-coded target + nativeBuildInputs = [fenix.targets."x86_64-unknown-linux-gnu".latest.rust-std]; + # TODO: those override the nix flags + # NIX_CFLAGS_COMPILE = builtins.toString ["-Oz" "-fwrapv" "-fomit-frame-pointer" "-fno-stack-protector" "-fno-unroll-loops" "-fno-unwind-tables" "-fno-asynchronous-unwind-tables" "-fmerge-all-constants" "-fvisibility=hidden" "-Bsymbolic" "-fno-ident" "-fno-exceptions" "-ffunction-sections" "-fdata-sections"]; + # NIX_CFLAGS_LINK = builtins.toString ["Wl,-z,norelro" "-Wl,--hash-style=gnu" "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL"]; + }; in rec { servicepoint-binding-c-stable-release = mkServicepoint stable-release-args; @@ -162,13 +164,13 @@ rec { servicepoint-binding-c-stable-size = mkServicepoint (stable-release-args // stable-size-args); servicepoint-binding-c-nightly-size = mkServicepoint ( - nightly-release-args // stable-size-args // unstable-size-args + nightly-release-args // stable-size-args // nightly-size-args ); servicepoint-binding-c-musl-stable-size = mkServicepoint ( musl-stable-release-args // stable-size-args ); servicepoint-binding-c-musl-nightly-size = mkServicepoint ( - musl-nightly-release-args // stable-size-args // unstable-size-args + musl-nightly-release-args // stable-size-args // nightly-size-args ); # default variants @@ -177,6 +179,7 @@ rec { all-examples = mkAllExamples ""; all-examples-size = mkAllExamples "-size"; + all-examples-nightly-size = mkAllExamples "-nightly-size"; # TODO: musl targets do not work on darwin all-examples-musl = mkAllExamples "-musl"; all-examples-musl-static = mkAllExamples "-musl-static"; @@ -190,8 +193,19 @@ rec { servicepointBinding = selfPkgs.servicepoint-binding-c; } )) -# construct another pakage per example, but optimized for size with unstable rust +# construct another pakage per example, but optimized for size with stable rust // (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-size") value) ( + lib.genAttrs examples ( + name: + mkExample { + inherit name pkgs; + servicepointBinding = selfPkgs.servicepoint-binding-c-stable-size; + EXTRA_CFLAGS = builtins.toString size-cflags; + } + ) +)) +# construct another pakage per example, but optimized for size with unstable rust +// (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-unstable-size") value) ( lib.genAttrs examples ( name: mkExample { @@ -208,7 +222,7 @@ rec { mkExample { inherit name; pkgs = pkgs.pkgsMusl; - servicepointBinding = selfPkgs.servicepoint-binding-c-musl; + servicepointBinding = selfPkgs.servicepoint-binding-c-musl-stable-release; } ) )) @@ -219,7 +233,7 @@ rec { mkExample { inherit name; pkgs = pkgs.pkgsMusl; - servicepointBinding = selfPkgs.servicepoint-binding-c-musl; + servicepointBinding = selfPkgs.servicepoint-binding-c-musl-stable-release; static = true; } ) @@ -231,7 +245,7 @@ rec { mkExample { inherit name; pkgs = pkgs.pkgsMusl; - servicepointBinding = selfPkgs.servicepoint-binding-c-musl; + servicepointBinding = selfPkgs.servicepoint-binding-c-musl-stable-size; static = true; EXTRA_CFLAGS = builtins.toString size-cflags; } From 02f629c68b93ecb97f61f0d23f77b7548bd7b425 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 16 Jun 2025 21:26:58 +0200 Subject: [PATCH 3/5] generate some functions with macros, cbindgen 0.29 --- Cargo.lock | 15 ++- Cargo.toml | 5 +- build.rs | 50 ++++----- cbindgen.toml | 3 +- include/servicepoint.h | 129 ++++++++++------------ src/commands/bitmap_command.rs | 22 +--- src/commands/bitvec_command.rs | 22 +--- src/commands/brightness_grid_command.rs | 28 +---- src/commands/cc_only_commands.rs | 22 +--- src/commands/char_grid_command.rs | 26 +---- src/commands/cp437_grid_command.rs | 28 +---- src/commands/generic_command.rs | 2 +- src/commands/global_brightness_command.rs | 25 +---- src/containers/bitmap.rs | 21 +--- src/containers/bitvec.rs | 20 +--- src/containers/brightness_grid.rs | 26 +---- src/containers/byte_slice.rs | 4 +- src/containers/char_grid.rs | 20 +--- src/containers/cp437_grid.rs | 21 +--- src/lib.rs | 1 + src/macros.rs | 25 +++++ src/packet.rs | 20 +--- src/udp.rs | 9 +- 23 files changed, 195 insertions(+), 349 deletions(-) create mode 100644 src/macros.rs diff --git a/Cargo.lock b/Cargo.lock index 121e01f..a4f755c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -106,9 +106,9 @@ dependencies = [ [[package]] name = "cbindgen" -version = "0.28.0" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eadd868a2ce9ca38de7eeafdcec9c7065ef89b42b32f0839278d55f35c54d1ff" +checksum = "975982cdb7ad6a142be15bdf84aea7ec6a9e5d4d797c004d43185b24cfe4e684" dependencies = [ "clap", "heck", @@ -263,9 +263,9 @@ checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3" [[package]] name = "heck" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "indexmap" @@ -368,6 +368,12 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "pkg-config" version = "0.3.32" @@ -540,6 +546,7 @@ version = "0.15.0" dependencies = [ "cbindgen", "env_logger", + "paste", "servicepoint", ] diff --git a/Cargo.toml b/Cargo.toml index f6bd53b..668edcc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ keywords = ["cccb", "cccb-servicepoint", "cbindgen"] crate-type = ["staticlib", "cdylib", "rlib"] [build-dependencies] -cbindgen = "0.28.0" +cbindgen = "0.29.0" [dependencies.servicepoint] version = "0.15.0" @@ -25,6 +25,9 @@ default-features = false version = "0.11.8" optional = true +[dependencies] +paste = "1.0.15" + [features] all_compressions = ["servicepoint/all_compressions"] default = ["all_compressions", "servicepoint/default", "env_logger"] diff --git a/build.rs b/build.rs index 397341d..a353ecd 100644 --- a/build.rs +++ b/build.rs @@ -8,6 +8,13 @@ use std::{env, fs}; fn main() { 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}/build.rs"); println!("cargo::rerun-if-changed={crate_dir}/Cargo.toml"); @@ -18,41 +25,32 @@ fn main() { let config = cbindgen::Config::from_file(crate_dir.clone() + "/cbindgen.toml") .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 bindings = match cbindgen::generate_with_config(crate_dir, config) { - Ok(bindings) => bindings, - Err(e) => { - eprintln!("cargo:warning=Servicepoint header could not be generated: {e:?}"); - return; - } - }; + let bindings = cbindgen::generate_with_config(crate_dir, config) + .expect("Servicepoint header could not be generated"); bindings.write_to_file(&header_file); println!("cargo:include={output_dir}"); - if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") { - 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 + if is_recursive { + return; + } - 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; - } - _ => {} - }; + 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}"); - 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}"); } } diff --git a/cbindgen.toml b/cbindgen.toml index 6edd33f..2341192 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -30,7 +30,8 @@ include = ["servicepoint", "std"] extra_bindings = ["servicepoint", "servicepoint_binding_c"] [parse.expand] -features = ["full"] +crates = ["servicepoint_binding_c", "paste"] +features = [] [export] include = [] diff --git a/include/servicepoint.h b/include/servicepoint.h index 3721527..4caa011 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -1,7 +1,7 @@ #ifndef 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. */ @@ -640,9 +640,9 @@ extern "C" { 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]. @@ -655,9 +655,9 @@ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ bitmap); 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. @@ -801,9 +801,9 @@ struct ByteSlice sp_bitmap_unsafe_data_ref(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]. @@ -816,9 +816,9 @@ BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ bit_vec); 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]. @@ -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); /** - * 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]. @@ -931,9 +931,9 @@ void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ brightness_grid, 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. @@ -1051,9 +1051,9 @@ struct ByteSlice sp_brightness_grid_unsafe_data_ref(BrightnessGrid */*notnull*/ 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]. @@ -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); /** - * 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. @@ -1157,16 +1157,14 @@ void sp_char_grid_set(CharGrid */*notnull*/ char_grid, size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid); /** - * Clones an [BitmapCommand] instance. - * - * returns: a new [BitmapCommand] instance. + *Clones a [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], @@ -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); /** - * Clones an [BitVecCommand] instance. - * - * returns: a new [BitVecCommand] instance. + *Clones a [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]. @@ -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); /** - * Clones an [GlobalBrightnessCommand] instance. - * - * returns: a new [GlobalBrightnessCommand] instance. + *Clones a [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); @@ -1345,16 +1342,14 @@ void sp_cmd_brightness_global_set(struct GlobalBrightnessCommand */*notnull*/ co Brightness brightness); /** - * Clones an [BrightnessGridCommand] instance. - * - * returns: a new [BrightnessGridCommand] instance. + *Clones a [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], @@ -1406,16 +1401,14 @@ void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ size_t origin_y); /** - * Clones an [CharGridCommand] instance. - * - * returns: a new [CharGridCommand] instance. + *Clones a [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], @@ -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); /** - * 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. @@ -1481,16 +1474,14 @@ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ command); struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); /** - * Clones an [Cp437GridCommand] instance. - * - * returns: a new [Cp437GridCommand] instance. + *Clones a [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], @@ -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); /** - * 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. @@ -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); /** - * 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. @@ -1617,9 +1608,9 @@ void sp_cmd_hard_reset_free(struct ClearCommand */*notnull*/ command); 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]. @@ -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); /** - * 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. @@ -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); /** - * Clones a [Packet]. - * - * returns: a new [Packet] instance. + *Clones a [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. @@ -1792,9 +1781,9 @@ bool sp_u16_to_command_code(uint16_t code, 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]. diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 286a6f1..6018b33 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,5 +1,6 @@ -use crate::mem::{ - heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, +use crate::{ + macros::{wrap_clone, wrap_free}, + mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; 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()) } -/// Clones an [BitmapCommand] instance. -/// -/// returns: a new [BitmapCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_clone( - command: NonNull, -) -> NonNull { - unsafe { heap_clone(command) } -} - -/// Deallocates a [BitmapCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_free(command: NonNull) { - unsafe { heap_drop(command) } -} +wrap_clone!(BitmapCommand, sp_cmd_bitmap); +wrap_free!(BitmapCommand, sp_cmd_bitmap); /// Returns a pointer to the provided `BitmapCommand`. /// diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index b49cdc5..b3eb843 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,5 +1,6 @@ -use crate::mem::{ - heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, +use crate::{ + macros::{wrap_clone, wrap_free}, + mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{ 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()) } -/// Clones an [BitVecCommand] instance. -/// -/// returns: a new [BitVecCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_clone( - command: NonNull, -) -> NonNull { - unsafe { heap_clone(command) } -} - -/// Deallocates a [BitVecCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_free(command: NonNull) { - unsafe { heap_drop(command) } -} +wrap_clone!(BitVecCommand, sp_cmd_bitvec); +wrap_free!(BitVecCommand, sp_cmd_bitvec); /// Returns a pointer to the [BitVec] contained in the [BitVecCommand]. #[no_mangle] diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 7410f2d..7803486 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,9 +1,8 @@ -use crate::mem::{ - heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, -}; -use servicepoint::{ - BitmapCommand, BrightnessGrid, BrightnessGridCommand, Origin, Packet, +use crate::{ + macros::{wrap_clone, wrap_free}, + mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; +use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin, Packet}; use std::ptr::NonNull; /// 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()) } -/// Clones an [BrightnessGridCommand] instance. -/// -/// returns: a new [BrightnessGridCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_clone( - command: NonNull, -) -> NonNull { - unsafe { heap_clone(command) } -} - -/// Deallocates a [BitmapCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_free( - command: NonNull, -) { - unsafe { heap_drop(command) } -} +wrap_clone!(BrightnessGridCommand, sp_cmd_brightness_grid); +wrap_free!(BrightnessGridCommand, sp_cmd_brightness_grid); /// Moves the provided [BrightnessGrid] to be contained in the [BrightnessGridCommand]. #[no_mangle] diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index 2a48c12..0acfa68 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -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 std::ptr::NonNull; @@ -12,11 +12,7 @@ pub unsafe extern "C" fn sp_cmd_clear_new() -> NonNull { heap_move_nonnull(ClearCommand) } -/// Deallocates a [ClearCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_clear_free(command: NonNull) { - unsafe { heap_drop(command) } -} +wrap_free!(ClearCommand, sp_cmd_clear); /// 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 { heap_move_nonnull(HardResetCommand) } -/// Deallocates a [HardResetCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_hard_reset_free( - command: NonNull, -) { - unsafe { heap_drop(command) } -} +wrap_free!(HardResetCommand, sp_cmd_hard_reset); /// A yet-to-be-tested command. /// @@ -44,8 +34,4 @@ pub unsafe extern "C" fn sp_cmd_fade_out_new() -> NonNull { heap_move_nonnull(FadeOutCommand) } -/// Deallocates a [FadeOutCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_fade_out_free(command: NonNull) { - unsafe { heap_drop(command) } -} +wrap_free!(FadeOutCommand, sp_cmd_fade_out); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 1fbd66c..8a6c07c 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,7 +1,8 @@ -use crate::mem::{ - heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, +use crate::{ + 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; /// 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()) } -/// Clones an [CharGridCommand] instance. -/// -/// returns: a new [CharGridCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_clone( - command: NonNull, -) -> NonNull { - unsafe { heap_clone(command) } -} - -/// Deallocates a [BitmapCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_free( - command: NonNull, -) { - unsafe { heap_drop(command) } -} +wrap_clone!(CharGridCommand, sp_cmd_char_grid); +wrap_free!(CharGridCommand, sp_cmd_char_grid); /// Moves the provided [CharGrid] to be contained in the [CharGridCommand]. #[no_mangle] diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index f034c6d..fc32f05 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,9 +1,8 @@ -use crate::mem::{ - heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, -}; -use servicepoint::{ - BitmapCommand, Cp437Grid, Cp437GridCommand, Origin, Packet, +use crate::{ + macros::{wrap_clone, wrap_free}, + mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; +use servicepoint::{Cp437Grid, Cp437GridCommand, Origin, Packet}; use std::ptr::NonNull; /// 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()) } -/// Clones an [Cp437GridCommand] instance. -/// -/// returns: a new [Cp437GridCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_clone( - command: NonNull, -) -> NonNull { - unsafe { heap_clone(command) } -} - -/// Deallocates a [Cp437GridCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_free( - command: NonNull, -) { - unsafe { heap_drop(command) } -} +wrap_clone!(Cp437GridCommand, sp_cmd_cp437_grid); +wrap_free!(Cp437GridCommand, sp_cmd_cp437_grid); /// Moves the provided bitmap into the provided command. /// diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 851e897..840900f 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -222,7 +222,7 @@ pub unsafe extern "C" fn sp_cmd_generic_clone(command: SPCommand) -> SPCommand { } /// Deallocates an [SPCommand]. -/// +/// /// Commands with an invalid `tag` do not have to be freed as the `data` pointer should be null. /// /// # Examples diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 6b2643e..48d21d7 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,7 +1,8 @@ -use crate::mem::{heap_clone, heap_drop, heap_move_nonnull, heap_remove}; -use servicepoint::{ - BitmapCommand, Brightness, GlobalBrightnessCommand, Packet, +use crate::{ + macros::{wrap_clone, wrap_free}, + mem::{heap_move_nonnull, heap_remove}, }; +use servicepoint::{Brightness, GlobalBrightnessCommand, Packet}; use std::ptr::NonNull; /// 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()) } -/// Clones an [GlobalBrightnessCommand] instance. -/// -/// returns: a new [GlobalBrightnessCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_global_clone( - command: NonNull, -) -> NonNull { - unsafe { heap_clone(command) } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_global_free( - command: NonNull, -) { - unsafe { heap_drop(command) } -} +wrap_clone!(GlobalBrightnessCommand, sp_cmd_brightness_global); +wrap_free!(GlobalBrightnessCommand, sp_cmd_brightness_global); /// Moves the provided bitmap to be contained in the command. #[no_mangle] diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 7a1b633..63cac85 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,9 +1,7 @@ use crate::{ containers::ByteSlice, - mem::{ - heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, - heap_remove, - }, + macros::{wrap_clone, wrap_free}, + mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ 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)) } -/// Clones a [Bitmap]. -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_clone( - bitmap: NonNull, -) -> NonNull { - unsafe { heap_clone(bitmap) } -} - -/// Deallocates a [Bitmap]. -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_free(bitmap: NonNull) { - unsafe { heap_drop(bitmap) } -} +wrap_clone!(Bitmap, sp_bitmap); +wrap_free!(Bitmap, sp_bitmap); /// Gets the current value at the specified position in the [Bitmap]. /// diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index e9eeb68..5f2d7b0 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,8 +1,7 @@ use crate::{ containers::ByteSlice, - mem::{ - 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::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, @@ -36,19 +35,8 @@ pub unsafe extern "C" fn sp_bitvec_load( heap_move_nonnull(DisplayBitVec::from_slice(data)) } -/// Clones a [DisplayBitVec]. -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_clone( - bit_vec: NonNull, -) -> NonNull { - unsafe { heap_clone(bit_vec) } -} - -/// Deallocates a [DisplayBitVec]. -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_free(bit_vec: NonNull) { - unsafe { heap_drop(bit_vec) } -} +wrap_clone!(DisplayBitVec, sp_bitvec); +wrap_free!(DisplayBitVec, sp_bitvec); /// Gets the value of a bit from the [DisplayBitVec]. /// diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 3a7ae5f..21c3636 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,16 +1,13 @@ use crate::{ containers::ByteSlice, - mem::{ - heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, - heap_remove, - }, + macros::{wrap_clone, wrap_free}, + mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, Origin, Packet, }; -use std::mem::transmute; -use std::ptr::NonNull; +use std::{mem::transmute, ptr::NonNull}; /// Creates a new [BrightnessGrid] with the specified dimensions. /// @@ -55,21 +52,8 @@ pub unsafe extern "C" fn sp_brightness_grid_load( ) } -/// Clones a [BrightnessGrid]. -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_clone( - grid: NonNull, -) -> NonNull { - unsafe { heap_clone(grid) } -} - -/// Deallocates a [BrightnessGrid]. -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_free( - brightness_grid: NonNull, -) { - unsafe { heap_drop(brightness_grid) } -} +wrap_clone!(BrightnessGrid, sp_brightness_grid); +wrap_free!(BrightnessGrid, sp_brightness_grid); /// Gets the current value at the specified position. /// diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index 0668318..0642cfc 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -10,9 +10,9 @@ /// - the lifetime of the `CByteSlice` does not outlive the memory it points to, as described in /// the function returning this type. /// - if `start` is NULL or `length` is 0, do not dereference `start`. -/// +/// /// # Examples -/// +/// /// ```c /// ByteSlice empty = {.start: NULL, .length = 0}; /// ``` diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 3363d43..5c788f4 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,8 +1,7 @@ use crate::{ containers::ByteSlice, - mem::{ - 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::{CharGrid, CharGridCommand, Grid, Origin, Packet}; 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())) } -/// Clones a [CharGrid]. -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_clone( - grid: NonNull, -) -> NonNull { - unsafe { heap_clone(grid) } -} - -/// Deallocates a [CharGrid]. -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_free(char_grid: NonNull) { - unsafe { heap_drop(char_grid) } -} +wrap_clone!(CharGrid, sp_char_grid); +wrap_free!(CharGrid, sp_char_grid); /// Returns the current value at the specified position. /// diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index a636295..bd58981 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,9 +1,7 @@ use crate::{ containers::ByteSlice, - mem::{ - heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, - heap_remove, - }, + macros::{wrap_clone, wrap_free}, + mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ 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)) } -/// Clones a [Cp437Grid]. -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_clone( - grid: NonNull, -) -> NonNull { - unsafe { heap_clone(grid) } -} - -/// Deallocates a [Cp437Grid]. -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: NonNull) { - unsafe { heap_drop(cp437_grid) } -} +wrap_clone!(Cp437Grid, sp_cp437_grid); +wrap_free!(Cp437Grid, sp_cp437_grid); /// Gets the current value at the specified position. /// diff --git a/src/lib.rs b/src/lib.rs index 5c64328..e08fbd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,7 @@ pub mod commands; /// Functions related to [servicepoint::Bitmap], [servicepoint::CharGrid] and friends. pub mod containers; +mod macros; pub(crate) mod mem; /// Functions related to [Packet]. pub mod packet; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..80e19ef --- /dev/null +++ b/src/macros.rs @@ -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}; diff --git a/src/packet.rs b/src/packet.rs index b1269df..658265c 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,6 +1,7 @@ use crate::{ 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 std::ptr::NonNull; @@ -90,21 +91,8 @@ pub unsafe extern "C" fn sp_packet_serialize_to( } } -/// Clones a [Packet]. -/// -/// returns: a new [Packet] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_packet_clone( - packet: NonNull, -) -> NonNull { - unsafe { heap_clone(packet) } -} - -/// Deallocates a [Packet]. -#[no_mangle] -pub unsafe extern "C" fn sp_packet_free(packet: NonNull) { - unsafe { heap_drop(packet) } -} +wrap_clone!(Packet, sp_packet); +wrap_free!(Packet, sp_packet); /// Converts u16 into [CommandCode]. /// diff --git a/src/udp.rs b/src/udp.rs index fe9f876..eef82a8 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,6 +1,7 @@ use crate::{ 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 std::{ @@ -143,8 +144,4 @@ pub unsafe extern "C" fn sp_udp_send_header( .is_ok() } -/// Closes and deallocates a [UdpSocket]. -#[no_mangle] -pub unsafe extern "C" fn sp_udp_free(connection: NonNull) { - unsafe { heap_drop(connection) } -} +wrap_free!(UdpSocket, sp_udp); From f5240386252f24ecbf5017a8d2da49bb700ecdfe Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 16 Jun 2025 22:03:57 +0200 Subject: [PATCH 4/5] generate header by running cbindgen directly --- Cargo.toml | 1 - build.rs | 56 --------------------------------------------- generate-binding.sh | 2 +- 3 files changed, 1 insertion(+), 58 deletions(-) delete mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index 668edcc..d9a1a3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ description = "C bindings for the servicepoint crate." homepage = "https://docs.rs/crate/servicepoint_binding_c" repository = "https://git.berlin.ccc.de/servicepoint/servicepoint" readme = "README.md" -links = "servicepoint" keywords = ["cccb", "cccb-servicepoint", "cbindgen"] [lib] diff --git a/build.rs b/build.rs deleted file mode 100644 index a353ecd..0000000 --- a/build.rs +++ /dev/null @@ -1,56 +0,0 @@ -//! Build script generating the header for the `servicepoint` C library. -//! -//! When the environment variable `SERVICEPOINT_HEADER_OUT` is set, the header is copied there from -//! the out directory. This can be used to use the build script as a command line tool from other -//! build tools. - -use std::{env, fs}; - -fn main() { - 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}/build.rs"); - println!("cargo::rerun-if-changed={crate_dir}/Cargo.toml"); - println!("cargo::rerun-if-changed={crate_dir}/Cargo.lock"); - println!("cargo::rerun-if-changed={crate_dir}/cbindgen.toml"); - println!("cargo::rerun-if-env-changed=SERVICEPOINT_HEADER_OUT"); - - let config = - cbindgen::Config::from_file(crate_dir.clone() + "/cbindgen.toml") - .unwrap(); - - let output_dir = &env::var("OUT_DIR").unwrap(); - let header_file = output_dir.clone() + "/servicepoint.h"; - - let bindings = cbindgen::generate_with_config(crate_dir, config) - .expect("Servicepoint header could not be generated"); - - bindings.write_to_file(&header_file); - println!("cargo:include={output_dir}"); - - if is_recursive { - 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" - ); - } - - let header_copy = header_out + "/servicepoint.h"; - - println!("cargo:warning=Copying header to {header_copy}"); - fs::copy(header_file, &header_copy) - .expect("header could not be copied to SERVICEPOINT_HEADER_OUT"); - println!("cargo::rerun-if-changed={header_copy}"); - } -} diff --git a/generate-binding.sh b/generate-binding.sh index 45301bd..744ad29 100755 --- a/generate-binding.sh +++ b/generate-binding.sh @@ -3,4 +3,4 @@ set -e SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" -SERVICEPOINT_HEADER_OUT="$SCRIPT_PATH/include" cargo build --release +cbindgen --config $SCRIPT_PATH/cbindgen.toml --crate servicepoint_binding_c --output $SCRIPT_PATH/include/servicepoint.h From 9756ef39b757ae1588dc5834afc42c91c49f6d63 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Tue, 17 Jun 2025 21:12:24 +0200 Subject: [PATCH 5/5] wrap normal methods with macro --- include/servicepoint.h | 281 ++++++++++++++---------------- src/containers/bitmap.rs | 143 ++++++--------- src/containers/bitvec.rs | 146 +++++++--------- src/containers/brightness_grid.rs | 163 +++++++---------- src/containers/char_grid.rs | 130 ++++++-------- src/containers/cp437_grid.rs | 143 ++++++--------- src/macros.rs | 62 ++++++- 7 files changed, 477 insertions(+), 591 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 4caa011..6d3b891 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -645,14 +645,24 @@ void init_env_logger(void); struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); /** + * Calls [`servicepoint::Bitmap::data_ref_mut`]. + * + * Gets an unsafe reference to the data of the [Bitmap] instance. + * + * The returned memory is valid for the lifetime of the bitmap. + */ +struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); + +/** + * Calls [`servicepoint::Bitmap::fill`]. + * * Sets the state of all pixels in the [Bitmap]. * * # Arguments * - * - `bitmap`: instance to write to * - `value`: the value to set all pixels to */ -void sp_bitmap_fill(struct Bitmap */*notnull*/ bitmap, bool value); +void sp_bitmap_fill(struct Bitmap */*notnull*/ instance, bool value); /** *Deallocates a [Bitmap] instance. @@ -669,30 +679,29 @@ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); /** - * Gets the current value at the specified position in the [Bitmap]. + * Calls [`servicepoint::Bitmap::get`]. + * + * Gets the current value at the specified position. * * # Arguments * - * - `bitmap`: instance to read from * - `x` and `y`: position of the cell to read * * # Panics * * - when accessing `x` or `y` out of bounds */ -bool sp_bitmap_get(struct Bitmap */*notnull*/ bitmap, size_t x, size_t y); +bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); /** - * Gets the height in pixels of the [Bitmap] instance. + * Calls [`servicepoint::Bitmap::height`]. * - * # Arguments - * - * - `bitmap`: instance to read from + * Gets the height in pixels. */ -size_t sp_bitmap_height(struct Bitmap */*notnull*/ bitmap); +size_t sp_bitmap_height(struct Bitmap */*notnull*/ instance); /** - * Consumes the Bitmap and returns the contained BitVec + * Consumes the Bitmap and returns the contained BitVec. */ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); @@ -757,11 +766,12 @@ struct Bitmap *sp_bitmap_new(size_t width, size_t height); struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); /** - * Sets the value of the specified position in the [Bitmap]. + * Calls [`servicepoint::Bitmap::set`]. + * + * Sets the value of the specified position. * * # Arguments * - * - `bitmap`: instance to write to * - `x` and `y`: position of the cell * - `value`: the value to write to the cell * @@ -769,36 +779,26 @@ struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); * * - when accessing `x` or `y` out of bounds */ -void sp_bitmap_set(struct Bitmap */*notnull*/ bitmap, +void sp_bitmap_set(struct Bitmap */*notnull*/ instance, size_t x, size_t y, bool value); /** - * Gets an unsafe reference to the data of the [Bitmap] instance. + * Calls [`servicepoint::Bitmap::width`]. * - * The returned memory is valid for the lifetime of the bitmap. + * Gets the width in pixels. */ -struct ByteSlice sp_bitmap_unsafe_data_ref(struct Bitmap */*notnull*/ bitmap); +size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); /** - * Gets the width in pixels of the [Bitmap] instance. + * Calls [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. * - * # Arguments + * Gets an unsafe reference to the data of the [DisplayBitVec] instance. * - * - `bitmap`: instance to read from - * - * # Panics - * - * - when `bitmap` is NULL - * - * # Safety - * - * The caller has to make sure that: - * - * - `bitmap` points to a valid [Bitmap] + * The returned memory is valid for the lifetime of the bitvec. */ -size_t sp_bitmap_width(struct Bitmap */*notnull*/ bitmap); +struct ByteSlice sp_bitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); /** *Clones a [DisplayBitVec] instance. @@ -806,14 +806,15 @@ size_t sp_bitmap_width(struct Bitmap */*notnull*/ bitmap); BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); /** - * Sets the value of all bits in the [DisplayBitVec]. + * Calls [`servicepoint::DisplayBitVec::fill`]. + * + * Sets the value of all bits. * * # Arguments * - * - `bit_vec`: instance to write to * - `value`: the value to set all bits to */ -void sp_bitvec_fill(BitVec */*notnull*/ bit_vec, bool value); +void sp_bitvec_fill(BitVec */*notnull*/ instance, bool value); /** *Deallocates a [DisplayBitVec] instance. @@ -821,7 +822,9 @@ void sp_bitvec_fill(BitVec */*notnull*/ bit_vec, bool value); void sp_bitvec_free(BitVec */*notnull*/ instance); /** - * Gets the value of a bit from the [DisplayBitVec]. + * Calls [`servicepoint::DisplayBitVec::get`]. + * + * Gets the value of a bit. * * # Arguments * @@ -834,7 +837,7 @@ void sp_bitvec_free(BitVec */*notnull*/ instance); * * - when accessing `index` out of bounds */ -bool sp_bitvec_get(BitVec */*notnull*/ bit_vec, size_t index); +bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); /** * Creates a [BitVecCommand] and immediately turns that into a [Packet]. @@ -849,22 +852,18 @@ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, CompressionCode compression); /** + * Calls [`servicepoint::DisplayBitVec::is_empty`]. + * * Returns true if length is 0. - * - * # Arguments - * - * - `bit_vec`: instance to write to */ -bool sp_bitvec_is_empty(BitVec */*notnull*/ bit_vec); +bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); /** - * Gets the length of the [DisplayBitVec] in bits. + * Calls [`servicepoint::DisplayBitVec::len`]. * - * # Arguments - * - * - `bit_vec`: instance to write to + * Gets the length in bits. */ -size_t sp_bitvec_len(BitVec */*notnull*/ bit_vec); +size_t sp_bitvec_len(BitVec */*notnull*/ instance); /** * Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. @@ -889,11 +888,12 @@ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); BitVec */*notnull*/ sp_bitvec_new(size_t size); /** - * Sets the value of a bit in the [DisplayBitVec]. + * Calls [`servicepoint::DisplayBitVec::set`]. + * + * Sets the value of a bit. * * # Arguments * - * - `bit_vec`: instance to write to * - `index`: the bit index to edit * - `value`: the value to set the bit to * @@ -901,18 +901,7 @@ BitVec */*notnull*/ sp_bitvec_new(size_t size); * * - when accessing `index` out of bounds */ -void sp_bitvec_set(BitVec */*notnull*/ bit_vec, size_t index, bool value); - -/** - * Gets an unsafe reference to the data of the [DisplayBitVec] instance. - * - * The returned memory is valid for the lifetime of the bitvec. - * - * # Arguments - * - * - `bit_vec`: instance to write to - */ -struct ByteSlice sp_bitvec_unsafe_data_ref(BitVec */*notnull*/ bit_vec); +void sp_bitvec_set(BitVec */*notnull*/ instance, size_t index, bool value); /** *Clones a [BrightnessGrid] instance. @@ -920,14 +909,24 @@ struct ByteSlice sp_bitvec_unsafe_data_ref(BitVec */*notnull*/ bit_vec); BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ instance); /** - * Sets the value of all cells in the [BrightnessGrid]. + * Calls [`servicepoint::BrightnessGrid::data_ref_mut`]. + * + * Gets an unsafe reference to the data of the instance. + * + * The returned memory is valid for the lifetime of the grid. + */ +struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ instance); + +/** + * Calls [`servicepoint::BrightnessGrid::fill`]. + * + * Sets the value of all cells. * * # Arguments * - * - `brightness_grid`: instance to write to * - `value`: the value to set all cells to */ -void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ brightness_grid, +void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ instance, Brightness value); /** @@ -936,11 +935,12 @@ void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ brightness_grid, void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); /** + * Calls [`servicepoint::BrightnessGrid::get`]. + * * Gets the current value at the specified position. * * # Arguments * - * - `brightness_grid`: instance to read from * - `x` and `y`: position of the cell to read * * returns: value at position @@ -948,20 +948,16 @@ void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); * # Panics * - When accessing `x` or `y` out of bounds. */ -Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ brightness_grid, +Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ instance, size_t x, size_t y); /** - * Gets the height of the [BrightnessGrid] instance. + * Calls [`servicepoint::BrightnessGrid::height`]. * - * # Arguments - * - * - `brightness_grid`: instance to read from - * - * returns: height + * Gets the height of the grid. */ -size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ brightness_grid); +size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ instance); /** * Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. @@ -1007,11 +1003,12 @@ BrightnessGrid *sp_brightness_grid_load(size_t width, BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); /** - * Sets the value of the specified position in the [BrightnessGrid]. + * Calls [`servicepoint::BrightnessGrid::set`]. + * + * Sets the value of the specified position. * * # Arguments * - * - `brightness_grid`: instance to write to * - `x` and `y`: position of the cell * - `value`: the value to write to the cell * @@ -1021,34 +1018,17 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); * * - When accessing `x` or `y` out of bounds. */ -void sp_brightness_grid_set(BrightnessGrid */*notnull*/ brightness_grid, +void sp_brightness_grid_set(BrightnessGrid */*notnull*/ instance, size_t x, size_t y, Brightness value); /** - * Gets an unsafe reference to the data of the [BrightnessGrid] instance. + * Calls [`servicepoint::BrightnessGrid::width`]. * - * The returned memory is valid for the lifetime of the brightness grid. - * - * # Arguments - * - * - `brightness_grid`: instance to read from - * - * returns: slice of bytes underlying the `brightness_grid`. + * Gets the width of the grid. */ -struct ByteSlice sp_brightness_grid_unsafe_data_ref(BrightnessGrid */*notnull*/ brightness_grid); - -/** - * Gets the width of the [BrightnessGrid] instance. - * - * # Arguments - * - * - `brightness_grid`: instance to read from - * - * returns: width - */ -size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ brightness_grid); +size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ instance); /** *Clones a [CharGrid] instance. @@ -1056,14 +1036,16 @@ size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ brightness_grid); CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); /** - * Sets the value of all cells in the [CharGrid]. + * Calls [`servicepoint::CharGrid::fill`]. + * + * Sets the value of all cells in the grid. * * # Arguments * - * - `char_grid`: instance to write to * - `value`: the value to set all cells to + * - when providing values that cannot be converted to Rust's `char`. */ -void sp_char_grid_fill(CharGrid */*notnull*/ char_grid, uint32_t value); +void sp_char_grid_fill(CharGrid */*notnull*/ instance, uint32_t value); /** *Deallocates a [CharGrid] instance. @@ -1071,27 +1053,26 @@ void sp_char_grid_fill(CharGrid */*notnull*/ char_grid, uint32_t value); void sp_char_grid_free(CharGrid */*notnull*/ instance); /** + * Calls [`servicepoint::CharGrid::get`]. + * * Returns the current value at the specified position. * * # Arguments * - * - `char_grid`: instance to read from * - `x` and `y`: position of the cell to read * * # Panics * * - when accessing `x` or `y` out of bounds */ -uint32_t sp_char_grid_get(CharGrid */*notnull*/ char_grid, size_t x, size_t y); +uint32_t sp_char_grid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); /** - * Gets the height of the [CharGrid] instance. + * Calls [`servicepoint::CharGrid::height`]. * - * # Arguments - * - * - `char_grid`: instance to read from + * Gets the height of the grid. */ -size_t sp_char_grid_height(CharGrid */*notnull*/ char_grid); +size_t sp_char_grid_height(CharGrid */*notnull*/ instance); /** * Creates a [CharGridCommand] and immediately turns that into a [Packet]. @@ -1128,11 +1109,12 @@ CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); /** - * Sets the value of the specified position in the [CharGrid]. + * Calls [`servicepoint::CharGrid::set`]. + * + * Sets the value of the specified position in the grid. * * # Arguments * - * - `char_grid`: instance to write to * - `x` and `y`: position of the cell * - `value`: the value to write to the cell * @@ -1141,20 +1123,19 @@ CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); * # Panics * * - when accessing `x` or `y` out of bounds + * - when providing values that cannot be converted to Rust's `char`. */ -void sp_char_grid_set(CharGrid */*notnull*/ char_grid, +void sp_char_grid_set(CharGrid */*notnull*/ instance, size_t x, size_t y, uint32_t value); /** - * Gets the width of the [CharGrid] instance. + * Calls [`servicepoint::CharGrid::width`]. * - * # Arguments - * - * - `char_grid`: instance to read from + * Gets the width of the grid. */ -size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid); +size_t sp_char_grid_width(CharGrid */*notnull*/ instance); /** *Clones a [BitmapCommand] instance. @@ -1608,49 +1589,50 @@ void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance); struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); /** - *Clones a [Cp437Grid] instance. + * Calls [`servicepoint::Cp437Grid::data_ref_mut`]. + * + * Gets an unsafe reference to the data of the grid. + * + * The returned memory is valid for the lifetime of the instance. */ -Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); +struct ByteSlice sp_cp437_data_ref_mut(Cp437Grid */*notnull*/ instance); /** - * Sets the value of all cells in the [Cp437Grid]. + * Calls [`servicepoint::Cp437Grid::fill`]. + * + * Sets the value of all cells in the grid. * * # Arguments * * - `cp437_grid`: instance to write to * - `value`: the value to set all cells to */ -void sp_cp437_grid_fill(Cp437Grid */*notnull*/ cp437_grid, uint8_t value); - -/** - *Deallocates a [Cp437Grid] instance. - */ -void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); +void sp_cp437_fill(Cp437Grid */*notnull*/ instance, uint8_t value); /** + * Calls [`servicepoint::Cp437Grid::get`]. + * * Gets the current value at the specified position. * * # Arguments * - * - `cp437_grid`: instance to read from * - `x` and `y`: position of the cell to read * * # Panics * * - when accessing `x` or `y` out of bounds */ -uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ cp437_grid, - size_t x, - size_t y); +uint8_t sp_cp437_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); /** - * Gets the height of the [Cp437Grid] instance. - * - * # Arguments - * - * - `cp437_grid`: instance to read from + *Clones a [Cp437Grid] instance. */ -size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ cp437_grid); +Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); + +/** + *Deallocates a [Cp437Grid] instance. + */ +void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); /** * Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. @@ -1678,11 +1660,19 @@ Cp437Grid *sp_cp437_grid_load(size_t width, Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); /** - * Sets the value of the specified position in the [Cp437Grid]. + * Calls [`servicepoint::Cp437Grid::height`]. + * + * Gets the height of the grid. + */ +size_t sp_cp437_height(Cp437Grid */*notnull*/ instance); + +/** + * Calls [`servicepoint::Cp437Grid::set`]. + * + * Sets the value at the specified position. * * # Arguments * - * - `cp437_grid`: instance to write to * - `x` and `y`: position of the cell * - `value`: the value to write to the cell * @@ -1692,26 +1682,17 @@ Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); * * - when accessing `x` or `y` out of bounds */ -void sp_cp437_grid_set(Cp437Grid */*notnull*/ cp437_grid, - size_t x, - size_t y, - uint8_t value); +void sp_cp437_set(Cp437Grid */*notnull*/ instance, + size_t x, + size_t y, + uint8_t value); /** - * Gets an unsafe reference to the data of the [Cp437Grid] instance. + * Calls [`servicepoint::Cp437Grid::width`]. * - * The returned memory is valid for the lifetime of the grid. + * Gets the width of the grid. */ -struct ByteSlice sp_cp437_grid_unsafe_data_ref(Cp437Grid */*notnull*/ cp437_grid); - -/** - * Gets the width of the [Cp437Grid] instance. - * - * # Arguments - * - * - `cp437_grid`: instance to read from - */ -size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ cp437_grid); +size_t sp_cp437_width(Cp437Grid */*notnull*/ instance); /** *Clones a [Packet] instance. diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 63cac85..ae97597 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_free, wrap_method}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -83,98 +83,67 @@ pub unsafe extern "C" fn sp_bitmap_from_bitvec( wrap_clone!(Bitmap, sp_bitmap); wrap_free!(Bitmap, sp_bitmap); -/// Gets the current value at the specified position in the [Bitmap]. -/// -/// # Arguments -/// -/// - `bitmap`: instance to read from -/// - `x` and `y`: position of the cell to read -/// -/// # Panics -/// -/// - when accessing `x` or `y` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_get( - bitmap: NonNull, - x: usize, - y: usize, -) -> bool { - unsafe { bitmap.as_ref().get(x, y) } -} +wrap_method!( + sp_bitmap :: Bitmap; + /// Gets 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 + ref fn get(x: usize, y: usize) -> bool; +); -/// Sets the value of the specified position in the [Bitmap]. -/// -/// # Arguments -/// -/// - `bitmap`: instance to write to -/// - `x` and `y`: position of the cell -/// - `value`: the value to write to the cell -/// -/// # Panics -/// -/// - when accessing `x` or `y` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_set( - bitmap: NonNull, - x: usize, - y: usize, - value: bool, -) { - unsafe { (*bitmap.as_ptr()).set(x, y, value) }; -} +wrap_method!( + sp_bitmap :: Bitmap; + /// Sets the value of the specified position. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell + /// - `value`: the value to write to the cell + /// + /// # Panics + /// + /// - when accessing `x` or `y` out of bounds + mut fn set(x: usize, y: usize, value: bool); +); -/// Sets the state of all pixels in the [Bitmap]. -/// -/// # Arguments -/// -/// - `bitmap`: instance to write to -/// - `value`: the value to set all pixels to -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_fill(bitmap: NonNull, value: bool) { - unsafe { (*bitmap.as_ptr()).fill(value) }; -} +wrap_method!( + sp_bitmap :: Bitmap; + /// Sets the state of all pixels in the [Bitmap]. + /// + /// # Arguments + /// + /// - `value`: the value to set all pixels to + mut fn fill(value: bool); +); -/// Gets the width in pixels of the [Bitmap] instance. -/// -/// # Arguments -/// -/// - `bitmap`: instance to read from -/// -/// # Panics -/// -/// - when `bitmap` is NULL -/// -/// # Safety -/// -/// The caller has to make sure that: -/// -/// - `bitmap` points to a valid [Bitmap] -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_width(bitmap: NonNull) -> usize { - unsafe { bitmap.as_ref().width() } -} +wrap_method!( + sp_bitmap :: Bitmap; + /// Gets the width in pixels. + ref fn width() -> usize; +); -/// Gets the height in pixels of the [Bitmap] instance. -/// -/// # Arguments -/// -/// - `bitmap`: instance to read from -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_height(bitmap: NonNull) -> usize { - unsafe { bitmap.as_ref().height() } -} +wrap_method!( + sp_bitmap :: Bitmap; + /// Gets the height in pixels. + ref fn height() -> usize; +); -/// Gets an unsafe reference to the data of the [Bitmap] instance. -/// -/// The returned memory is valid for the lifetime of the bitmap. -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_unsafe_data_ref( - mut bitmap: NonNull, -) -> ByteSlice { - unsafe { ByteSlice::from_slice(bitmap.as_mut().data_ref_mut()) } -} +wrap_method!( + sp_bitmap :: Bitmap; + /// Gets an unsafe reference to the data of the [Bitmap] instance. + /// + /// The returned memory is valid for the lifetime of the bitmap. + mut fn data_ref_mut() -> ByteSlice; + |slice| unsafe { ByteSlice::from_slice(slice) }; +); -/// Consumes the Bitmap and returns the contained BitVec +/// Consumes the Bitmap and returns the contained BitVec. #[no_mangle] pub unsafe extern "C" fn sp_bitmap_into_bitvec( bitmap: NonNull, diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 5f2d7b0..ea0ca4b 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_free, wrap_method}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{ @@ -38,97 +38,69 @@ pub unsafe extern "C" fn sp_bitvec_load( wrap_clone!(DisplayBitVec, sp_bitvec); wrap_free!(DisplayBitVec, sp_bitvec); -/// Gets the value of a bit from the [DisplayBitVec]. -/// -/// # Arguments -/// -/// - `bit_vec`: instance to read from -/// - `index`: the bit index to read -/// -/// returns: value of the bit -/// -/// # Panics -/// -/// - when accessing `index` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_get( - bit_vec: NonNull, - index: usize, -) -> bool { - unsafe { *bit_vec.as_ref().get(index).unwrap() } -} +wrap_method!( + sp_bitvec :: DisplayBitVec; + /// Gets the value of a bit. + /// + /// # Arguments + /// + /// - `bit_vec`: instance to read from + /// - `index`: the bit index to read + /// + /// returns: value of the bit + /// + /// # Panics + /// + /// - when accessing `index` out of bounds + ref fn get(index: usize) -> bool; + |result| result.map(|x| *x).unwrap_or(false); +); -/// Sets the value of a bit in the [DisplayBitVec]. -/// -/// # Arguments -/// -/// - `bit_vec`: instance to write to -/// - `index`: the bit index to edit -/// - `value`: the value to set the bit to -/// -/// # Panics -/// -/// - when accessing `index` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_set( - bit_vec: NonNull, - index: usize, - value: bool, -) { - unsafe { (*bit_vec.as_ptr()).set(index, value) } -} +wrap_method!( + sp_bitvec :: DisplayBitVec; + /// Sets the value of a bit. + /// + /// # Arguments + /// + /// - `index`: the bit index to edit + /// - `value`: the value to set the bit to + /// + /// # Panics + /// + /// - when accessing `index` out of bounds + mut fn set(index: usize, value: bool); +); -/// Sets the value of all bits in the [DisplayBitVec]. -/// -/// # Arguments -/// -/// - `bit_vec`: instance to write to -/// - `value`: the value to set all bits to -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_fill( - bit_vec: NonNull, - value: bool, -) { - unsafe { (*bit_vec.as_ptr()).fill(value) } -} +wrap_method!( + sp_bitvec :: DisplayBitVec; + /// Sets the value of all bits. + /// + /// # Arguments + /// + /// - `value`: the value to set all bits to + mut fn fill(value: bool); +); -/// Gets the length of the [DisplayBitVec] in bits. -/// -/// # Arguments -/// -/// - `bit_vec`: instance to write to -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_len( - bit_vec: NonNull, -) -> usize { - unsafe { bit_vec.as_ref().len() } -} +wrap_method!( + sp_bitvec :: DisplayBitVec; + /// Gets the length in bits. + ref fn len() -> usize; +); -/// Returns true if length is 0. -/// -/// # Arguments -/// -/// - `bit_vec`: instance to write to -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_is_empty( - bit_vec: NonNull, -) -> bool { - unsafe { bit_vec.as_ref().is_empty() } -} +wrap_method!( + sp_bitvec :: DisplayBitVec; + /// Returns true if length is 0. + ref fn is_empty() -> bool; +); -/// Gets an unsafe reference to the data of the [DisplayBitVec] instance. -/// -/// The returned memory is valid for the lifetime of the bitvec. -/// -/// # Arguments -/// -/// - `bit_vec`: instance to write to -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_unsafe_data_ref( - bit_vec: NonNull, -) -> ByteSlice { - unsafe { ByteSlice::from_slice((*bit_vec.as_ptr()).as_raw_mut_slice()) } -} +wrap_method!( + sp_bitvec :: DisplayBitVec; + /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. + /// + /// The returned memory is valid for the lifetime of the bitvec. + mut fn as_raw_mut_slice() -> ByteSlice; + |slice| unsafe { ByteSlice::from_slice(slice) }; +); /// Creates a [BitVecCommand] and immediately turns that into a [Packet]. /// diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 21c3636..a1a5e4e 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_free, wrap_method}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -55,112 +55,73 @@ pub unsafe extern "C" fn sp_brightness_grid_load( wrap_clone!(BrightnessGrid, sp_brightness_grid); wrap_free!(BrightnessGrid, sp_brightness_grid); -/// Gets the current value at the specified position. -/// -/// # Arguments -/// -/// - `brightness_grid`: instance to read from -/// - `x` and `y`: position of the cell to read -/// -/// returns: value at position -/// -/// # Panics -/// - When accessing `x` or `y` out of bounds. -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_get( - brightness_grid: NonNull, - x: usize, - y: usize, -) -> Brightness { - unsafe { brightness_grid.as_ref().get(x, y) } -} +wrap_method!( + sp_brightness_grid :: BrightnessGrid; + /// Gets the current value at the specified position. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell to read + /// + /// returns: value at position + /// + /// # Panics + /// - When accessing `x` or `y` out of bounds. + ref fn get(x: usize, y: usize) -> Brightness; +); -/// Sets the value of the specified position in the [BrightnessGrid]. -/// -/// # Arguments -/// -/// - `brightness_grid`: instance to write to -/// - `x` and `y`: position of the cell -/// - `value`: the value to write to the cell -/// -/// returns: old value of the cell -/// -/// # Panics -/// -/// - When accessing `x` or `y` out of bounds. -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_set( - brightness_grid: NonNull, - x: usize, - y: usize, - value: Brightness, -) { - unsafe { (*brightness_grid.as_ptr()).set(x, y, value) }; -} +wrap_method!( + sp_brightness_grid :: BrightnessGrid; + /// Sets the value of the specified position. + /// + /// # 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. + mut fn set(x: usize, y: usize, value: Brightness); +); -/// Sets the value of all cells in the [BrightnessGrid]. -/// -/// # Arguments -/// -/// - `brightness_grid`: instance to write to -/// - `value`: the value to set all cells to -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_fill( - brightness_grid: NonNull, - value: Brightness, -) { - unsafe { (*brightness_grid.as_ptr()).fill(value) }; -} +wrap_method!( + sp_brightness_grid :: BrightnessGrid; + /// Sets the value of all cells. + /// + /// # Arguments + /// + /// - `value`: the value to set all cells to + mut fn fill(value: Brightness); +); -/// Gets the width of the [BrightnessGrid] instance. -/// -/// # Arguments -/// -/// - `brightness_grid`: instance to read from -/// -/// returns: width -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_width( - brightness_grid: NonNull, -) -> usize { - unsafe { brightness_grid.as_ref().width() } -} +wrap_method!( + sp_brightness_grid :: BrightnessGrid; + /// Gets the width of the grid. + ref fn width() -> usize; +); -/// Gets the height of the [BrightnessGrid] instance. -/// -/// # Arguments -/// -/// - `brightness_grid`: instance to read from -/// -/// returns: height -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_height( - brightness_grid: NonNull, -) -> usize { - unsafe { brightness_grid.as_ref().height() } -} +wrap_method!( + sp_brightness_grid :: BrightnessGrid; + /// Gets the height of the grid. + ref fn height() -> usize; +); -/// Gets an unsafe reference to the data of the [BrightnessGrid] instance. -/// -/// The returned memory is valid for the lifetime of the brightness grid. -/// -/// # Arguments -/// -/// - `brightness_grid`: instance to read from -/// -/// returns: slice of bytes underlying the `brightness_grid`. -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_unsafe_data_ref( - brightness_grid: NonNull, -) -> ByteSlice { - //noinspection RsAssertEqual - const _: () = assert!(size_of::() == 1); +wrap_method!( + sp_brightness_grid :: BrightnessGrid; + /// Gets an unsafe reference to the data of the instance. + /// + /// The returned memory is valid for the lifetime of the grid. + mut fn data_ref_mut() -> ByteSlice; + |br_slice| unsafe { + //noinspection RsAssertEqual + const _: () = assert!(size_of::() == 1); - let data = unsafe { (*brightness_grid.as_ptr()).data_ref_mut() }; - unsafe { - ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(data)) - } -} + ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) + }; +); /// Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. /// diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 5c788f4..bc421b5 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_free, wrap_method}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; @@ -42,85 +42,63 @@ pub unsafe extern "C" fn sp_char_grid_load( wrap_clone!(CharGrid, sp_char_grid); wrap_free!(CharGrid, sp_char_grid); -/// Returns the current value at the specified position. -/// -/// # Arguments -/// -/// - `char_grid`: instance to read from -/// - `x` and `y`: position of the cell to read -/// -/// # Panics -/// -/// - when accessing `x` or `y` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_get( - char_grid: NonNull, - x: usize, - y: usize, -) -> u32 { - unsafe { char_grid.as_ref().get(x, y) as u32 } -} +wrap_method!( + sp_char_grid :: CharGrid; + /// 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 + ref fn get(x: usize, y: usize) -> u32; + | char | char as u32 +); -/// Sets the value of the specified position in the [CharGrid]. -/// -/// # Arguments -/// -/// - `char_grid`: instance to write to -/// - `x` and `y`: position of the cell -/// - `value`: the value to write to the cell -/// -/// returns: old value of the cell -/// -/// # Panics -/// -/// - when accessing `x` or `y` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_set( - char_grid: NonNull, - x: usize, - y: usize, - value: u32, -) { - unsafe { (*char_grid.as_ptr()).set(x, y, char::from_u32(value).unwrap()) }; -} +wrap_method!( + sp_char_grid :: CharGrid; + /// 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`. + mut fn set(x: usize, y: usize, value: u32); + let value = char::from_u32(value).unwrap(); +); -/// Sets the value of all cells in the [CharGrid]. -/// -/// # Arguments -/// -/// - `char_grid`: instance to write to -/// - `value`: the value to set all cells to -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_fill( - char_grid: NonNull, - value: u32, -) { - unsafe { (*char_grid.as_ptr()).fill(char::from_u32(value).unwrap()) }; -} +wrap_method!( + sp_char_grid :: CharGrid; + /// 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`. + mut fn fill(value: u32); + let value = char::from_u32(value).unwrap(); +); -/// Gets the width of the [CharGrid] instance. -/// -/// # Arguments -/// -/// - `char_grid`: instance to read from -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_width( - char_grid: NonNull, -) -> usize { - unsafe { char_grid.as_ref().width() } -} +wrap_method!( + sp_char_grid :: CharGrid; + /// Gets the width of the grid. + ref fn width() -> usize; +); -/// Gets the height of the [CharGrid] instance. -/// -/// # Arguments -/// -/// - `char_grid`: instance to read from -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_height( - char_grid: NonNull, -) -> usize { - unsafe { char_grid.as_ref().height() } -} +wrap_method!( + sp_char_grid :: CharGrid; + /// Gets the height of the grid. + ref fn height() -> usize; +); /// Creates a [CharGridCommand] and immediately turns that into a [Packet]. /// diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index bd58981..88fb2b0 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_free, wrap_method}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -33,95 +33,68 @@ pub unsafe extern "C" fn sp_cp437_grid_load( wrap_clone!(Cp437Grid, sp_cp437_grid); wrap_free!(Cp437Grid, sp_cp437_grid); -/// Gets the current value at the specified position. -/// -/// # Arguments -/// -/// - `cp437_grid`: instance to read from -/// - `x` and `y`: position of the cell to read -/// -/// # Panics -/// -/// - when accessing `x` or `y` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_get( - cp437_grid: NonNull, - x: usize, - y: usize, -) -> u8 { - unsafe { cp437_grid.as_ref().get(x, y) } -} +wrap_method!( + sp_cp437 :: Cp437Grid; + /// Gets 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 + ref fn get(x: usize, y: usize) -> u8; +); -/// Sets the value of the specified position in the [Cp437Grid]. -/// -/// # Arguments -/// -/// - `cp437_grid`: instance to write to -/// - `x` and `y`: position of the cell -/// - `value`: the value to write to the cell -/// -/// returns: old value of the cell -/// -/// # Panics -/// -/// - when accessing `x` or `y` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_set( - cp437_grid: NonNull, - x: usize, - y: usize, - value: u8, -) { - unsafe { (*cp437_grid.as_ptr()).set(x, y, value) }; -} +wrap_method!( + sp_cp437 :: Cp437Grid; + /// Sets the value at the specified position. + /// + /// # 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 + mut fn set(x: usize, y: usize, value: u8); +); -/// Sets the value of all cells in the [Cp437Grid]. -/// -/// # Arguments -/// -/// - `cp437_grid`: instance to write to -/// - `value`: the value to set all cells to -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_fill( - cp437_grid: NonNull, - value: u8, -) { - unsafe { (*cp437_grid.as_ptr()).fill(value) }; -} +wrap_method!( + sp_cp437 :: Cp437Grid; + /// Sets the value of all cells in the grid. + /// + /// # Arguments + /// + /// - `cp437_grid`: instance to write to + /// - `value`: the value to set all cells to + mut fn fill(value: u8); +); -/// Gets the width of the [Cp437Grid] instance. -/// -/// # Arguments -/// -/// - `cp437_grid`: instance to read from -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_width( - cp437_grid: NonNull, -) -> usize { - unsafe { cp437_grid.as_ref().width() } -} +wrap_method!( + sp_cp437 :: Cp437Grid; + /// Gets the width of the grid. + ref fn width() -> usize; +); -/// Gets the height of the [Cp437Grid] instance. -/// -/// # Arguments -/// -/// - `cp437_grid`: instance to read from -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_height( - cp437_grid: NonNull, -) -> usize { - unsafe { cp437_grid.as_ref().height() } -} +wrap_method!( + sp_cp437 :: Cp437Grid; + /// Gets the height of the grid. + ref fn height() -> usize; +); -/// Gets an unsafe reference to the data of the [Cp437Grid] instance. -/// -/// The returned memory is valid for the lifetime of the grid. -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref( - cp437_grid: NonNull, -) -> ByteSlice { - unsafe { ByteSlice::from_slice((*cp437_grid.as_ptr()).data_ref_mut()) } -} +wrap_method!( + sp_cp437 :: Cp437Grid; + /// Gets an unsafe reference to the data of the grid. + /// + /// The returned memory is valid for the lifetime of the instance. + mut fn data_ref_mut() -> ByteSlice; + | slice | unsafe { ByteSlice::from_slice(slice) }; +); /// Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. /// diff --git a/src/macros.rs b/src/macros.rs index 80e19ef..c91248e 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,25 +1,77 @@ macro_rules! wrap_free { - ($typ:ident, $prefix:ident) => { + ($typ:ty, $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) } + unsafe { $crate::mem::heap_drop(instance) } } } }; } macro_rules! wrap_clone { - ($typ:ident, $prefix:ident) => { + ($typ:ty, $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) } + unsafe { $crate::mem::heap_clone(instance) } } } }; } -pub(crate) use {wrap_clone, wrap_free}; +macro_rules! nonnull_as_ref { + ($ident:ident) => { + $ident.as_ref() + }; +} + +macro_rules! nonnull_as_mut { + ($ident:ident) => { + &mut *$ident.as_ptr() + }; +} + +// meta required on purpose, because otherwise the added documentation would suppress warnings +macro_rules! wrap_method { + ( + $prefix:ident :: $object_type:ty; + $(#[$meta:meta])+ + $ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*) + $(-> $return_type:ty$(; |$it:ident | $return_expr:expr)?)? + $(; let $param_let_name:ident = $param_let_expr:expr)* + $(;)? + ) => { + paste::paste! { + #[doc = concat!(" Calls [`servicepoint::", stringify!($object_type), + "::", stringify!($function), "`].")] + #[doc = ""] + $(#[$meta])* + #[no_mangle] + pub unsafe extern "C" fn [<$prefix _ $function>]( + instance: NonNull<$object_type>, + $($param_name: $param_type),* + ) $(-> $return_type)? { + let instance = unsafe {$crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance)}; + $(let $param_let_name = $param_let_expr;)* + #[allow( + unused_variables, + reason = "This variable may not be used depending on macro variables" )] + let result = instance.$function($($param_name),*); + $( + $( + let $it = result; + let result = $return_expr; + )? + return result; + )? + } + } + }; +} + +pub(crate) use { + nonnull_as_mut, nonnull_as_ref, wrap_clone, wrap_free, wrap_method, +};