diff --git a/Cargo.lock b/Cargo.lock index 40ece4f..9fcfa8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,9 +98,9 @@ dependencies = [ [[package]] name = "cbindgen" -version = "0.28.0" +version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eadd868a2ce9ca38de7eeafdcec9c7065ef89b42b32f0839278d55f35c54d1ff" +checksum = "3fce8dd7fcfcbf3a0a87d8f515194b49d6135acab73e18bd380d1d93bb1a15eb" dependencies = [ "clap", "heck", @@ -407,8 +407,9 @@ dependencies = [ [[package]] name = "servicepoint" -version = "0.13.2" -source = "git+https://git.berlin.ccc.de/servicepoint/servicepoint/?branch=next#fe67160974d9fed542eb37e5e9a202eaf6fe00dc" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93b52049be55a15fe37c13249d7f96aa8a5de56e1a41838e74a822ee8316a0c4" dependencies = [ "bitvec", "bzip2", diff --git a/Cargo.toml b/Cargo.toml index 7e5ed5d..7750abc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,31 +15,14 @@ keywords = ["cccb", "cccb-servicepoint", "cbindgen"] crate-type = ["staticlib", "cdylib", "rlib"] [build-dependencies] -cbindgen = "0.28.0" +cbindgen = "0.27.0" [dependencies.servicepoint] -package = "servicepoint" -version = "0.13.2" -default-features = false -features = ["protocol_udp"] -git = "https://git.berlin.ccc.de/servicepoint/servicepoint/" -branch = "next" - -[features] -full = ["servicepoint/all_compressions", "servicepoint/default"] -default = ["full"] +version = "0.13.1" +features = ["all_compressions"] [lints.rust] missing-docs = "warn" -unsafe_op_in_unsafe_fn = "warn" [package.metadata.docs.rs] all-features = true - -[profile.size-optimized] -inherits = "release" -opt-level = 'z' # Optimize for size -lto = true # Enable link-time optimization -codegen-units = 1 # Reduce number of codegen units to increase optimizations -panic = 'abort' # Abort on panic -strip = true # Strip symbols from binary diff --git a/cbindgen.toml b/cbindgen.toml index bf2377c..7fc0fdf 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -26,7 +26,7 @@ sort_by = "Name" parse_deps = false [parse.expand] -features = ["full"] +all_features = true [export] include = [] diff --git a/examples/lang_c/Cargo.lock b/examples/lang_c/Cargo.lock deleted file mode 100644 index 9435574..0000000 --- a/examples/lang_c/Cargo.lock +++ /dev/null @@ -1,695 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "adler2" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" - -[[package]] -name = "anstream" -version = "0.6.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" -dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "is_terminal_polyfill", - "utf8parse", -] - -[[package]] -name = "anstyle" -version = "1.0.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" - -[[package]] -name = "anstyle-parse" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" -dependencies = [ - "utf8parse", -] - -[[package]] -name = "anstyle-query" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" -dependencies = [ - "windows-sys", -] - -[[package]] -name = "anstyle-wincon" -version = "3.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" -dependencies = [ - "anstyle", - "once_cell", - "windows-sys", -] - -[[package]] -name = "bitflags" -version = "2.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" - -[[package]] -name = "bitvec" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" -dependencies = [ - "funty", - "radium", - "tap", - "wyz", -] - -[[package]] -name = "bzip2" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49ecfb22d906f800d4fe833b6282cf4dc1c298f5057ca0b5445e5c209735ca47" -dependencies = [ - "bzip2-sys", -] - -[[package]] -name = "bzip2-sys" -version = "0.1.13+1.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "225bff33b2141874fe80d71e07d6eec4f85c5c216453dd96388240f96e1acc14" -dependencies = [ - "cc", - "pkg-config", -] - -[[package]] -name = "cbindgen" -version = "0.28.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eadd868a2ce9ca38de7eeafdcec9c7065ef89b42b32f0839278d55f35c54d1ff" -dependencies = [ - "clap", - "heck", - "indexmap", - "log", - "proc-macro2", - "quote", - "serde", - "serde_json", - "syn", - "tempfile", - "toml", -] - -[[package]] -name = "cc" -version = "1.2.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e3a13707ac958681c13b39b458c073d0d9bc8a22cb1b2f4c8e55eb72c13f362" -dependencies = [ - "jobserver", - "libc", - "shlex", -] - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "clap" -version = "4.5.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8aa86934b44c19c50f87cc2790e19f54f7a67aedb64101c2e1a2e5ecfb73944" -dependencies = [ - "clap_builder", -] - -[[package]] -name = "clap_builder" -version = "4.5.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2414dbb2dd0695280da6ea9261e327479e9d37b0630f6b53ba2a11c60c679fd9" -dependencies = [ - "anstream", - "anstyle", - "clap_lex", - "strsim", -] - -[[package]] -name = "clap_lex" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" - -[[package]] -name = "colorchoice" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" - -[[package]] -name = "crc32fast" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "equivalent" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" - -[[package]] -name = "errno" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "976dd42dc7e85965fe702eb8164f21f450704bdde31faefd6471dba214cb594e" -dependencies = [ - "libc", - "windows-sys", -] - -[[package]] -name = "fastrand" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" - -[[package]] -name = "flate2" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ced92e76e966ca2fd84c8f7aa01a4aea65b0eb6648d72f7c8f3e2764a67fece" -dependencies = [ - "crc32fast", - "miniz_oxide", -] - -[[package]] -name = "funty" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" - -[[package]] -name = "getrandom" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" -dependencies = [ - "cfg-if", - "libc", - "r-efi", - "wasi", -] - -[[package]] -name = "hashbrown" -version = "0.15.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" - -[[package]] -name = "heck" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" - -[[package]] -name = "indexmap" -version = "2.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e" -dependencies = [ - "equivalent", - "hashbrown", -] - -[[package]] -name = "is_terminal_polyfill" -version = "1.70.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" - -[[package]] -name = "itoa" -version = "1.0.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" - -[[package]] -name = "jobserver" -version = "0.1.33" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38f262f097c174adebe41eb73d66ae9c06b2844fb0da69969647bbddd9b0538a" -dependencies = [ - "getrandom", - "libc", -] - -[[package]] -name = "lang_c" -version = "0.1.0" -dependencies = [ - "cc", - "servicepoint_binding_c", -] - -[[package]] -name = "libc" -version = "0.2.171" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" - -[[package]] -name = "linux-raw-sys" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" - -[[package]] -name = "log" -version = "0.4.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" - -[[package]] -name = "memchr" -version = "2.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" - -[[package]] -name = "miniz_oxide" -version = "0.8.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3be647b768db090acb35d5ec5db2b0e1f1de11133ca123b9eacf5137868f892a" -dependencies = [ - "adler2", -] - -[[package]] -name = "once_cell" -version = "1.21.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" - -[[package]] -name = "pkg-config" -version = "0.3.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" - -[[package]] -name = "proc-macro2" -version = "1.0.94" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "r-efi" -version = "5.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" - -[[package]] -name = "radium" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" - -[[package]] -name = "rust-lzma" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d62915608f6cee1d7f2fc00f28b4f058ff79d6e4ec3c2fe0006b09b52437c84" -dependencies = [ - "pkg-config", - "vcpkg", -] - -[[package]] -name = "rustix" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d97817398dd4bb2e6da002002db259209759911da105da92bec29ccb12cf58bf" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys", -] - -[[package]] -name = "ryu" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" - -[[package]] -name = "serde" -version = "1.0.219" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.219" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.140" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" -dependencies = [ - "itoa", - "memchr", - "ryu", - "serde", -] - -[[package]] -name = "serde_spanned" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" -dependencies = [ - "serde", -] - -[[package]] -name = "servicepoint" -version = "0.13.2" -source = "git+https://git.berlin.ccc.de/servicepoint/servicepoint/?branch=next#75d24f658764dea251e28c7067446f3ccfbb89b0" -dependencies = [ - "bitvec", - "bzip2", - "flate2", - "log", - "once_cell", - "rust-lzma", - "thiserror", - "zstd", -] - -[[package]] -name = "servicepoint_binding_c" -version = "0.13.1" -dependencies = [ - "cbindgen", - "servicepoint", -] - -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "strsim" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" - -[[package]] -name = "syn" -version = "2.0.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "tap" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" - -[[package]] -name = "tempfile" -version = "3.19.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7437ac7763b9b123ccf33c338a5cc1bac6f69b45a136c19bdd8a65e3916435bf" -dependencies = [ - "fastrand", - "getrandom", - "once_cell", - "rustix", - "windows-sys", -] - -[[package]] -name = "thiserror" -version = "2.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "2.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "toml" -version = "0.8.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd87a5cdd6ffab733b2f74bc4fd7ee5fff6634124999ac278c35fc78c6120148" -dependencies = [ - "serde", - "serde_spanned", - "toml_datetime", - "toml_edit", -] - -[[package]] -name = "toml_datetime" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" -dependencies = [ - "serde", -] - -[[package]] -name = "toml_edit" -version = "0.22.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17b4795ff5edd201c7cd6dca065ae59972ce77d1b80fa0a84d94950ece7d1474" -dependencies = [ - "indexmap", - "serde", - "serde_spanned", - "toml_datetime", - "winnow", -] - -[[package]] -name = "unicode-ident" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" - -[[package]] -name = "utf8parse" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" - -[[package]] -name = "vcpkg" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" - -[[package]] -name = "wasi" -version = "0.14.2+wasi-0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" -dependencies = [ - "wit-bindgen-rt", -] - -[[package]] -name = "windows-sys" -version = "0.59.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" - -[[package]] -name = "winnow" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63d3fcd9bba44b03821e7d699eeee959f3126dcc4aa8e4ae18ec617c2a5cea10" -dependencies = [ - "memchr", -] - -[[package]] -name = "wit-bindgen-rt" -version = "0.39.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" -dependencies = [ - "bitflags", -] - -[[package]] -name = "wyz" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" -dependencies = [ - "tap", -] - -[[package]] -name = "zstd" -version = "0.13.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a" -dependencies = [ - "zstd-safe", -] - -[[package]] -name = "zstd-safe" -version = "7.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f49c4d5f0abb602a93fb8736af2a4f4dd9512e36f7f570d66e65ff867ed3b9d" -dependencies = [ - "zstd-sys", -] - -[[package]] -name = "zstd-sys" -version = "2.0.15+zstd.1.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb81183ddd97d0c74cedf1d50d85c8d08c1b8b68ee863bdee9e706eedba1a237" -dependencies = [ - "cc", - "pkg-config", -] diff --git a/examples/lang_c/Makefile b/examples/lang_c/Makefile index 1be4009..6b15722 100644 --- a/examples/lang_c/Makefile +++ b/examples/lang_c/Makefile @@ -1,47 +1,7 @@ CC := gcc -CARGO := rustup run nightly cargo THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) -REPO_ROOT := $(THIS_DIR)/../../ -RUST_TARGET_DIR := $(REPO_ROOT)/target/x86_64-unknown-linux-musl/size-optimized - -RUSTFLAGS := -Zlocation-detail=none \ - -Zfmt-debug=none \ - -C linker=musl-gcc \ - -C link-arg=-s \ - -C link-arg=--gc-sections \ - -C link-arg=-z,norelro \ - -C link-arg=--hash-style=gnu \ - --crate-type=staticlib \ - -C panic=abort - -CARGOFLAGS := --manifest-path=$(REPO_ROOT)/Cargo.toml \ - --profile=size-optimized \ - --no-default-features \ - --target=x86_64-unknown-linux-musl \ - -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \ - -Zbuild-std-features="panic_immediate_abort" \ - -CCFLAGS := -static -Os \ - -ffunction-sections -fdata-sections \ - -fwrapv -fomit-frame-pointer -fno-stack-protector\ - -fwhole-program \ - -nodefaultlibs -lservicepoint_binding_c -lc \ - -Wl,--gc-sections \ - -fno-unroll-loops \ - -fno-unwind-tables -fno-asynchronous-unwind-tables \ - -fmerge-all-constants \ - -Wl,-z,norelro \ - -Wl,--hash-style=gnu \ - -fvisibility=hidden \ - -Bsymbolic \ - -Wl,--exclude-libs,ALL \ - -fno-ident \ - #-fuse-ld=gold \ - -fno-exceptions - #-Wl,--icf=all \ - -export SERVICEPOINT_HEADER_OUT := $(THIS_DIR)/include +REPO_ROOT := $(THIS_DIR)/../../../.. build: out/lang_c @@ -55,19 +15,20 @@ run: out/lang_c PHONY: build clean dependencies run -out/lang_c_unstripped: dependencies src/main.c +out/lang_c: dependencies src/main.c mkdir -p out || true ${CC} src/main.c \ - -I $(SERVICEPOINT_HEADER_OUT) \ - -L $(RUST_TARGET_DIR)\ - $(CCFLAGS) \ - -o out/lang_c_unstripped -out/lang_c: out/lang_c_unstripped - strip -s -R .comment -R .gnu.version --strip-unneeded out/lang_c_unstripped -o out/lang_c -#strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag + -I include \ + -L $(REPO_ROOT)/target/release \ + -Wl,-Bstatic -lservicepoint_binding_c \ + -Wl,-Bdynamic -llzma \ + -o out/lang_c dependencies: FORCE mkdir -p include || true # generate servicepoint header and binary to link against - ${CARGO} rustc $(CARGOFLAGS) -- $(RUSTFLAGS) + SERVICEPOINT_HEADER_OUT=$(THIS_DIR)/include cargo build \ + --manifest-path=$(REPO_ROOT)/crates/servicepoint_binding_c/Cargo.toml \ + --release + FORCE: ; diff --git a/examples/lang_c/include/servicepoint.h b/examples/lang_c/include/servicepoint.h index ca235ca..d9cbe57 100644 --- a/examples/lang_c/include/servicepoint.h +++ b/examples/lang_c/include/servicepoint.h @@ -1,4 +1,4 @@ -/* Generated with cbindgen:0.28.0 */ +/* Generated with cbindgen:0.27.0 */ /* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */ @@ -341,18 +341,13 @@ size_t sp_bitmap_height(const SPBitmap *bitmap); * - `width`: size in pixels in x-direction * - `height`: size in pixels in y-direction * - * returns: [SPBitmap] that contains a copy of the provided data, or NULL in case of an error. - * - * # Errors - * - * In the following cases, this function will return NULL: - * - * - when the dimensions and data size do not match exactly. - * - when the width is not dividable by 8 + * returns: [SPBitmap] that contains a copy of the provided data. Will never return NULL. * * # Panics * * - when `data` is NULL + * - when the dimensions and data size do not match exactly. + * - when the width is not dividable by 8 * * # Safety * @@ -375,11 +370,9 @@ SPBitmap *sp_bitmap_load(size_t width, * - `width`: size in pixels in x-direction * - `height`: size in pixels in y-direction * - * returns: [SPBitmap] initialized to all pixels off, or NULL in case of an error. + * returns: [SPBitmap] initialized to all pixels off. Will never return NULL. * - * # Errors - * - * In the following cases, this function will return NULL: + * # Panics * * - when the width is not dividable by 8 * @@ -1213,7 +1206,7 @@ SPCommand *sp_command_bitmap_linear_or(size_t offset, SPCommand *sp_command_bitmap_linear_win(size_t x, size_t y, SPBitmap *bitmap, - SPCompressionCode compression); + SPCompressionCode compression_code); /** * Set pixel data according to a xor-mask starting at the offset. @@ -1455,6 +1448,20 @@ SPCommand *sp_command_utf8_data(size_t x, size_t y, SPCharGrid *grid); +/** + * Creates a new instance of [SPConnection] for testing that does not actually send anything. + * + * returns: a new instance. Will never return NULL. + * + * # Safety + * + * The caller has to make sure that: + * + * - the returned instance is freed in some way, either by using a consuming function or + * by explicitly calling `sp_connection_free`. + */ +SPConnection *sp_connection_fake(void); + /** * Closes and deallocates a [SPConnection]. * diff --git a/examples/lang_c/src/main.c b/examples/lang_c/src/main.c index 6a4b30e..1454804 100644 --- a/examples/lang_c/src/main.c +++ b/examples/lang_c/src/main.c @@ -7,15 +7,9 @@ int main(void) { return 1; SPBitmap *pixels = sp_bitmap_new(SP_PIXEL_WIDTH, SP_PIXEL_HEIGHT); - if (pixels == NULL) - return 1; - sp_bitmap_fill(pixels, true); SPCommand *command = sp_command_bitmap_linear_win(0, 0, pixels, SP_COMPRESSION_CODE_UNCOMPRESSED); - if (command == NULL) - return 1; - sp_connection_send_command(connection, command); sp_connection_free(connection); diff --git a/flake.nix b/flake.nix index 1f0ac97..9318237 100644 --- a/flake.nix +++ b/flake.nix @@ -33,35 +33,25 @@ { pkgs, system }: { default = pkgs.mkShell rec { - buildInputs = with pkgs;[ - xe - xz - libgcc - #glibc.static - musl - libunwind - ]; - - nativeBuildInputs = with pkgs;[ + packages = with pkgs; [ (pkgs.symlinkJoin { name = "rust-toolchain"; paths = with pkgs; [ - #rustc - #cargo - #rustPlatform.rustcSrc - #rustfmt - #clippy + rustc + cargo + rustPlatform.rustcSrc + rustfmt + clippy cargo-expand cargo-tarpaulin - rustup ]; }) gcc gnumake + xe + xz pkg-config - ]; - RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; }; } diff --git a/src/bitmap.rs b/src/bitmap.rs index 8ac5192..3313385 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -3,7 +3,7 @@ //! prefix `sp_bitmap_` use servicepoint::{DataRef, Grid}; -use std::ptr::{NonNull}; +use std::ptr::NonNull; use crate::byte_slice::SPByteSlice; @@ -26,11 +26,9 @@ pub struct SPBitmap(pub(crate) servicepoint::Bitmap); /// - `width`: size in pixels in x-direction /// - `height`: size in pixels in y-direction /// -/// returns: [SPBitmap] initialized to all pixels off, or NULL in case of an error. +/// returns: [SPBitmap] initialized to all pixels off. Will never return NULL. /// -/// # Errors -/// -/// In the following cases, this function will return NULL: +/// # Panics /// /// - when the width is not dividable by 8 /// @@ -44,12 +42,9 @@ pub struct SPBitmap(pub(crate) servicepoint::Bitmap); pub unsafe extern "C" fn sp_bitmap_new( width: usize, height: usize, -) -> *mut SPBitmap { - if let Some(bitmap) = servicepoint::Bitmap::new(width, height) { - Box::leak(Box::new(SPBitmap(bitmap))) - }else { - std::ptr::null_mut() - } +) -> NonNull { + let result = Box::new(SPBitmap(servicepoint::Bitmap::new(width, height))); + NonNull::from(Box::leak(result)) } /// Creates a new [SPBitmap] with a size matching the screen. @@ -75,18 +70,13 @@ pub unsafe extern "C" fn sp_bitmap_new_screen_sized() -> NonNull { /// - `width`: size in pixels in x-direction /// - `height`: size in pixels in y-direction /// -/// returns: [SPBitmap] that contains a copy of the provided data, or NULL in case of an error. -/// -/// # Errors -/// -/// In the following cases, this function will return NULL: -/// -/// - when the dimensions and data size do not match exactly. -/// - when the width is not dividable by 8 +/// returns: [SPBitmap] that contains a copy of the provided data. Will never return NULL. /// /// # Panics /// /// - when `data` is NULL +/// - when the dimensions and data size do not match exactly. +/// - when the width is not dividable by 8 /// /// # Safety /// @@ -101,14 +91,12 @@ pub unsafe extern "C" fn sp_bitmap_load( height: usize, data: *const u8, data_length: usize, -) -> *mut SPBitmap { +) -> NonNull { assert!(!data.is_null()); - let data = unsafe {std::slice::from_raw_parts(data, data_length)}; - if let Ok(bitmap) = servicepoint::Bitmap::load(width, height, data) { - Box::leak(Box::new(SPBitmap(bitmap))) - }else { - std::ptr::null_mut() - } + let data = std::slice::from_raw_parts(data, data_length); + let result = + Box::new(SPBitmap(servicepoint::Bitmap::load(width, height, data))); + NonNull::from(Box::leak(result)) } /// Clones a [SPBitmap]. @@ -132,7 +120,7 @@ pub unsafe extern "C" fn sp_bitmap_clone( bitmap: *const SPBitmap, ) -> NonNull { assert!(!bitmap.is_null()); - let result = Box::new(SPBitmap(unsafe {(*bitmap).0.clone()})); + let result = Box::new(SPBitmap((*bitmap).0.clone())); NonNull::from(Box::leak(result)) } @@ -154,7 +142,7 @@ pub unsafe extern "C" fn sp_bitmap_clone( #[no_mangle] pub unsafe extern "C" fn sp_bitmap_free(bitmap: *mut SPBitmap) { assert!(!bitmap.is_null()); - _ = unsafe {Box::from_raw(bitmap)}; + _ = Box::from_raw(bitmap); } /// Gets the current value at the specified position in the [SPBitmap]. @@ -182,7 +170,7 @@ pub unsafe extern "C" fn sp_bitmap_get( y: usize, ) -> bool { assert!(!bitmap.is_null()); - unsafe {(*bitmap).0.get(x, y)} + (*bitmap).0.get(x, y) } /// Sets the value of the specified position in the [SPBitmap]. @@ -214,7 +202,7 @@ pub unsafe extern "C" fn sp_bitmap_set( value: bool, ) { assert!(!bitmap.is_null()); - unsafe {(*bitmap).0.set(x, y, value)}; + (*bitmap).0.set(x, y, value); } /// Sets the state of all pixels in the [SPBitmap]. @@ -237,7 +225,7 @@ pub unsafe extern "C" fn sp_bitmap_set( #[no_mangle] pub unsafe extern "C" fn sp_bitmap_fill(bitmap: *mut SPBitmap, value: bool) { assert!(!bitmap.is_null()); - unsafe {(*bitmap).0.fill(value)}; + (*bitmap).0.fill(value); } /// Gets the width in pixels of the [SPBitmap] instance. @@ -258,7 +246,7 @@ pub unsafe extern "C" fn sp_bitmap_fill(bitmap: *mut SPBitmap, value: bool) { #[no_mangle] pub unsafe extern "C" fn sp_bitmap_width(bitmap: *const SPBitmap) -> usize { assert!(!bitmap.is_null()); - unsafe {(*bitmap).0.width()} + (*bitmap).0.width() } /// Gets the height in pixels of the [SPBitmap] instance. @@ -279,7 +267,7 @@ pub unsafe extern "C" fn sp_bitmap_width(bitmap: *const SPBitmap) -> usize { #[no_mangle] pub unsafe extern "C" fn sp_bitmap_height(bitmap: *const SPBitmap) -> usize { assert!(!bitmap.is_null()); - unsafe {(*bitmap).0.height()} + (*bitmap).0.height() } /// Gets an unsafe reference to the data of the [SPBitmap] instance. @@ -300,7 +288,7 @@ pub unsafe extern "C" fn sp_bitmap_unsafe_data_ref( bitmap: *mut SPBitmap, ) -> SPByteSlice { assert!(!bitmap.is_null()); - let data = unsafe {(*bitmap).0.data_ref_mut()}; + let data = (*bitmap).0.data_ref_mut(); SPByteSlice { start: NonNull::new(data.as_mut_ptr_range().start).unwrap(), length: data.len(), diff --git a/src/bitvec.rs b/src/bitvec.rs index ed96be5..484e849 100644 --- a/src/bitvec.rs +++ b/src/bitvec.rs @@ -79,7 +79,7 @@ pub unsafe extern "C" fn sp_bitvec_load( data_length: usize, ) -> NonNull { assert!(!data.is_null()); - let data = unsafe {std::slice::from_raw_parts(data, data_length)}; + let data = std::slice::from_raw_parts(data, data_length); let result = Box::new(SPBitVec(servicepoint::BitVec::from_slice(data))); NonNull::from(Box::leak(result)) } @@ -105,7 +105,7 @@ pub unsafe extern "C" fn sp_bitvec_clone( bit_vec: *const SPBitVec, ) -> NonNull { assert!(!bit_vec.is_null()); - let result = Box::new(unsafe {(*bit_vec).clone()}); + let result = Box::new((*bit_vec).clone()); NonNull::from(Box::leak(result)) } @@ -127,7 +127,7 @@ pub unsafe extern "C" fn sp_bitvec_clone( #[no_mangle] pub unsafe extern "C" fn sp_bitvec_free(bit_vec: *mut SPBitVec) { assert!(!bit_vec.is_null()); - _ = unsafe {Box::from_raw(bit_vec)}; + _ = Box::from_raw(bit_vec); } /// Gets the value of a bit from the [SPBitVec]. @@ -156,7 +156,7 @@ pub unsafe extern "C" fn sp_bitvec_get( index: usize, ) -> bool { assert!(!bit_vec.is_null()); - unsafe {*(*bit_vec).0.get(index).unwrap()} + *(*bit_vec).0.get(index).unwrap() } /// Sets the value of a bit in the [SPBitVec]. @@ -185,7 +185,7 @@ pub unsafe extern "C" fn sp_bitvec_set( value: bool, ) { assert!(!bit_vec.is_null()); - unsafe {(*bit_vec).0.set(index, value)} + (*bit_vec).0.set(index, value) } /// Sets the value of all bits in the [SPBitVec]. @@ -208,7 +208,7 @@ pub unsafe extern "C" fn sp_bitvec_set( #[no_mangle] pub unsafe extern "C" fn sp_bitvec_fill(bit_vec: *mut SPBitVec, value: bool) { assert!(!bit_vec.is_null()); - unsafe {(*bit_vec).0.fill(value)} + (*bit_vec).0.fill(value) } /// Gets the length of the [SPBitVec] in bits. @@ -229,7 +229,7 @@ pub unsafe extern "C" fn sp_bitvec_fill(bit_vec: *mut SPBitVec, value: bool) { #[no_mangle] pub unsafe extern "C" fn sp_bitvec_len(bit_vec: *const SPBitVec) -> usize { assert!(!bit_vec.is_null()); - unsafe {(*bit_vec).0.len()} + (*bit_vec).0.len() } /// Returns true if length is 0. @@ -250,7 +250,7 @@ pub unsafe extern "C" fn sp_bitvec_len(bit_vec: *const SPBitVec) -> usize { #[no_mangle] pub unsafe extern "C" fn sp_bitvec_is_empty(bit_vec: *const SPBitVec) -> bool { assert!(!bit_vec.is_null()); - unsafe {(*bit_vec).0.is_empty()} + (*bit_vec).0.is_empty() } /// Gets an unsafe reference to the data of the [SPBitVec] instance. @@ -275,7 +275,7 @@ pub unsafe extern "C" fn sp_bitvec_unsafe_data_ref( bit_vec: *mut SPBitVec, ) -> SPByteSlice { assert!(!bit_vec.is_null()); - let data = unsafe {(*bit_vec).0.as_raw_mut_slice()}; + let data = (*bit_vec).0.as_raw_mut_slice(); SPByteSlice { start: NonNull::new(data.as_mut_ptr_range().start).unwrap(), length: data.len(), diff --git a/src/brightness_grid.rs b/src/brightness_grid.rs index 6087db0..83af008 100644 --- a/src/brightness_grid.rs +++ b/src/brightness_grid.rs @@ -5,7 +5,7 @@ use crate::SPByteSlice; use servicepoint::{DataRef, Grid}; use std::convert::Into; -use std::mem::transmute; +use std::intrinsics::transmute; use std::ptr::NonNull; /// see [servicepoint::Brightness::MIN] @@ -77,18 +77,14 @@ pub unsafe extern "C" fn sp_brightness_grid_load( height: usize, data: *const u8, data_length: usize, -) -> *mut SPBrightnessGrid { +) -> NonNull { assert!(!data.is_null()); - let data =unsafe { std::slice::from_raw_parts(data, data_length)}; - let grid = match servicepoint::ByteGrid::load(width, height, data) { - None => return std::ptr::null_mut(), - Some(grid) => grid - }; - if let Ok(grid )= servicepoint::BrightnessGrid::try_from(grid) { - Box::leak(Box::new(SPBrightnessGrid(grid))) - }else { - std::ptr::null_mut() - } + let data = std::slice::from_raw_parts(data, data_length); + let grid = servicepoint::ByteGrid::load(width, height, data); + let grid = servicepoint::BrightnessGrid::try_from(grid) + .expect("invalid brightness value"); + let result = Box::new(SPBrightnessGrid(grid)); + NonNull::from(Box::leak(result)) } /// Clones a [SPBrightnessGrid]. @@ -116,7 +112,7 @@ pub unsafe extern "C" fn sp_brightness_grid_clone( brightness_grid: *const SPBrightnessGrid, ) -> NonNull { assert!(!brightness_grid.is_null()); - let result = Box::new(unsafe {(*brightness_grid).clone()}); + let result = Box::new((*brightness_grid).clone()); NonNull::from(Box::leak(result)) } @@ -144,7 +140,7 @@ pub unsafe extern "C" fn sp_brightness_grid_free( brightness_grid: *mut SPBrightnessGrid, ) { assert!(!brightness_grid.is_null()); - _ = unsafe {Box::from_raw(brightness_grid)}; + _ = Box::from_raw(brightness_grid); } /// Gets the current value at the specified position. @@ -174,7 +170,7 @@ pub unsafe extern "C" fn sp_brightness_grid_get( y: usize, ) -> u8 { assert!(!brightness_grid.is_null()); - unsafe {(*brightness_grid).0.get(x, y)}.into() + (*brightness_grid).0.get(x, y).into() } /// Sets the value of the specified position in the [SPBrightnessGrid]. @@ -209,7 +205,7 @@ pub unsafe extern "C" fn sp_brightness_grid_set( assert!(!brightness_grid.is_null()); let brightness = servicepoint::Brightness::try_from(value) .expect("invalid brightness value"); - unsafe {(*brightness_grid).0.set(x, y, brightness)}; + (*brightness_grid).0.set(x, y, brightness); } /// Sets the value of all cells in the [SPBrightnessGrid]. @@ -238,7 +234,7 @@ pub unsafe extern "C" fn sp_brightness_grid_fill( assert!(!brightness_grid.is_null()); let brightness = servicepoint::Brightness::try_from(value) .expect("invalid brightness value"); - unsafe {(*brightness_grid).0.fill(brightness)}; + (*brightness_grid).0.fill(brightness); } /// Gets the width of the [SPBrightnessGrid] instance. @@ -263,7 +259,7 @@ pub unsafe extern "C" fn sp_brightness_grid_width( brightness_grid: *const SPBrightnessGrid, ) -> usize { assert!(!brightness_grid.is_null()); - unsafe {(*brightness_grid).0.width()} + (*brightness_grid).0.width() } /// Gets the height of the [SPBrightnessGrid] instance. @@ -288,7 +284,7 @@ pub unsafe extern "C" fn sp_brightness_grid_height( brightness_grid: *const SPBrightnessGrid, ) -> usize { assert!(!brightness_grid.is_null()); - unsafe {(*brightness_grid).0.height()} + (*brightness_grid).0.height() } /// Gets an unsafe reference to the data of the [SPBrightnessGrid] instance. @@ -316,9 +312,9 @@ pub unsafe extern "C" fn sp_brightness_grid_unsafe_data_ref( ) -> SPByteSlice { assert!(!brightness_grid.is_null()); assert_eq!(core::mem::size_of::(), 1); - let data = unsafe {(*brightness_grid).0.data_ref_mut()}; + let data = (*brightness_grid).0.data_ref_mut(); // this assumes more about the memory layout than rust guarantees. yikes! - let data: &mut [u8] = unsafe {transmute(data)}; + let data: &mut [u8] = transmute(data); SPByteSlice { start: NonNull::new(data.as_mut_ptr_range().start).unwrap(), length: data.len(), diff --git a/src/char_grid.rs b/src/char_grid.rs index 3c51e89..dfaf225 100644 --- a/src/char_grid.rs +++ b/src/char_grid.rs @@ -74,7 +74,7 @@ pub unsafe extern "C" fn sp_char_grid_load( data_length: usize, ) -> NonNull { assert!(data.is_null()); - let data = unsafe {std::slice::from_raw_parts(data, data_length)}; + let data = std::slice::from_raw_parts(data, data_length); let result = Box::new(SPCharGrid( servicepoint::CharGrid::load_utf8(width, height, data.to_vec()) .unwrap(), @@ -103,7 +103,7 @@ pub unsafe extern "C" fn sp_char_grid_clone( char_grid: *const SPCharGrid, ) -> NonNull { assert!(!char_grid.is_null()); - let result = Box::new(unsafe{(*char_grid).clone()}); + let result = Box::new((*char_grid).clone()); NonNull::from(Box::leak(result)) } @@ -125,7 +125,7 @@ pub unsafe extern "C" fn sp_char_grid_clone( #[no_mangle] pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut SPCharGrid) { assert!(!char_grid.is_null()); - _ = unsafe {Box::from_raw(char_grid)}; + _ = Box::from_raw(char_grid); } /// Gets the current value at the specified position. @@ -153,7 +153,7 @@ pub unsafe extern "C" fn sp_char_grid_get( y: usize, ) -> u32 { assert!(!char_grid.is_null()); - unsafe {(*char_grid).0.get(x, y) as u32} + (*char_grid).0.get(x, y) as u32 } /// Sets the value of the specified position in the [SPCharGrid]. @@ -187,7 +187,7 @@ pub unsafe extern "C" fn sp_char_grid_set( value: u32, ) { assert!(!char_grid.is_null()); - unsafe {(*char_grid).0.set(x, y, char::from_u32(value).unwrap())}; + (*char_grid).0.set(x, y, char::from_u32(value).unwrap()); } /// Sets the value of all cells in the [SPCharGrid]. @@ -213,7 +213,7 @@ pub unsafe extern "C" fn sp_char_grid_fill( value: u32, ) { assert!(!char_grid.is_null()); - unsafe {(*char_grid).0.fill(char::from_u32(value).unwrap())}; + (*char_grid).0.fill(char::from_u32(value).unwrap()); } /// Gets the width of the [SPCharGrid] instance. @@ -236,7 +236,7 @@ pub unsafe extern "C" fn sp_char_grid_width( char_grid: *const SPCharGrid, ) -> usize { assert!(!char_grid.is_null()); - unsafe {(*char_grid).0.width()} + (*char_grid).0.width() } /// Gets the height of the [SPCharGrid] instance. @@ -259,5 +259,5 @@ pub unsafe extern "C" fn sp_char_grid_height( char_grid: *const SPCharGrid, ) -> usize { assert!(!char_grid.is_null()); - unsafe {(*char_grid).0.height()} + (*char_grid).0.height() } diff --git a/src/command.rs b/src/command.rs index 0720db6..f7e50ea 100644 --- a/src/command.rs +++ b/src/command.rs @@ -2,12 +2,12 @@ //! //! prefix `sp_command_` +use std::ptr::{null_mut, NonNull}; + use crate::{ SPBitVec, SPBitmap, SPBrightnessGrid, SPCharGrid, SPCompressionCode, SPCp437Grid, SPPacket, }; -use servicepoint::{BinaryOperation, GlobalBrightnessCommand}; -use std::ptr::NonNull; /// A low-level display command. /// @@ -23,7 +23,7 @@ use std::ptr::NonNull; /// ``` /// /// [SPConnection]: [crate::SPConnection] -pub struct SPCommand(pub(crate) servicepoint::TypedCommand); +pub struct SPCommand(pub(crate) servicepoint::Command); impl Clone for SPCommand { fn clone(&self) -> Self { @@ -54,9 +54,9 @@ impl Clone for SPCommand { pub unsafe extern "C" fn sp_command_try_from_packet( packet: *mut SPPacket, ) -> *mut SPCommand { - let packet = *unsafe{Box::from_raw(packet)}; - match servicepoint::TypedCommand::try_from(packet.0) { - Err(_) => std::ptr::null_mut(), + let packet = *Box::from_raw(packet); + match servicepoint::Command::try_from(packet.0) { + Err(_) => null_mut(), Ok(command) => Box::into_raw(Box::new(SPCommand(command))), } } @@ -82,7 +82,7 @@ pub unsafe extern "C" fn sp_command_clone( command: *const SPCommand, ) -> NonNull { assert!(!command.is_null()); - let result = Box::new(unsafe { (*command).clone() }); + let result = Box::new((*command).clone()); NonNull::from(Box::leak(result)) } @@ -106,7 +106,7 @@ pub unsafe extern "C" fn sp_command_clone( /// by explicitly calling `sp_command_free`. #[no_mangle] pub unsafe extern "C" fn sp_command_clear() -> NonNull { - let result = Box::new(SPCommand(servicepoint::ClearCommand.into())); + let result = Box::new(SPCommand(servicepoint::Command::Clear)); NonNull::from(Box::leak(result)) } @@ -124,7 +124,7 @@ pub unsafe extern "C" fn sp_command_clear() -> NonNull { /// by explicitly calling `sp_command_free`. #[no_mangle] pub unsafe extern "C" fn sp_command_hard_reset() -> NonNull { - let result = Box::new(SPCommand(servicepoint::HardResetCommand.into())); + let result = Box::new(SPCommand(servicepoint::Command::HardReset)); NonNull::from(Box::leak(result)) } @@ -140,7 +140,7 @@ pub unsafe extern "C" fn sp_command_hard_reset() -> NonNull { /// by explicitly calling `sp_command_free`. #[no_mangle] pub unsafe extern "C" fn sp_command_fade_out() -> NonNull { - let result = Box::new(SPCommand(servicepoint::FadeOutCommand.into())); + let result = Box::new(SPCommand(servicepoint::Command::FadeOut)); NonNull::from(Box::leak(result)) } @@ -164,7 +164,8 @@ pub unsafe extern "C" fn sp_command_brightness( ) -> NonNull { let brightness = servicepoint::Brightness::try_from(brightness) .expect("invalid brightness"); - let result = Box::new(SPCommand(GlobalBrightnessCommand::from(brightness).into())); + let result = + Box::new(SPCommand(servicepoint::Command::Brightness(brightness))); NonNull::from(Box::leak(result)) } @@ -193,11 +194,11 @@ pub unsafe extern "C" fn sp_command_char_brightness( grid: *mut SPBrightnessGrid, ) -> NonNull { assert!(!grid.is_null()); - let byte_grid = unsafe { *Box::from_raw(grid) }; - let result = Box::new(SPCommand(servicepoint::BrightnessGridCommand { - origin: servicepoint::Origin::new(x, y), - grid: byte_grid.0, - }.into())); + let byte_grid = *Box::from_raw(grid); + let result = Box::new(SPCommand(servicepoint::Command::CharBrightness( + servicepoint::Origin::new(x, y), + byte_grid.0, + ))); NonNull::from(Box::leak(result)) } @@ -231,8 +232,15 @@ pub unsafe extern "C" fn sp_command_bitmap_linear( offset: usize, bit_vec: *mut SPBitVec, compression: SPCompressionCode, -) -> *mut SPCommand { - unsafe {sp_command_bitmap_linear_internal(offset, bit_vec, compression, BinaryOperation::Overwrite)} +) -> NonNull { + assert!(!bit_vec.is_null()); + let bit_vec = *Box::from_raw(bit_vec); + let result = Box::new(SPCommand(servicepoint::Command::BitmapLinear( + offset, + bit_vec.into(), + compression.try_into().expect("invalid compression code"), + ))); + NonNull::from(Box::leak(result)) } /// Set pixel data according to an and-mask starting at the offset. @@ -265,8 +273,15 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and( offset: usize, bit_vec: *mut SPBitVec, compression: SPCompressionCode, -) -> *mut SPCommand { - unsafe {sp_command_bitmap_linear_internal(offset, bit_vec, compression, BinaryOperation::Xor)} +) -> NonNull { + assert!(!bit_vec.is_null()); + let bit_vec = *Box::from_raw(bit_vec); + let result = Box::new(SPCommand(servicepoint::Command::BitmapLinearAnd( + offset, + bit_vec.into(), + compression.try_into().expect("invalid compression code"), + ))); + NonNull::from(Box::leak(result)) } /// Set pixel data according to an or-mask starting at the offset. @@ -299,8 +314,15 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or( offset: usize, bit_vec: *mut SPBitVec, compression: SPCompressionCode, -) -> *mut SPCommand { - unsafe {sp_command_bitmap_linear_internal(offset, bit_vec, compression, BinaryOperation::Or)} +) -> NonNull { + assert!(!bit_vec.is_null()); + let bit_vec = *Box::from_raw(bit_vec); + let result = Box::new(SPCommand(servicepoint::Command::BitmapLinearOr( + offset, + bit_vec.into(), + compression.try_into().expect("invalid compression code"), + ))); + NonNull::from(Box::leak(result)) } /// Set pixel data according to a xor-mask starting at the offset. @@ -333,30 +355,15 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_xor( offset: usize, bit_vec: *mut SPBitVec, compression: SPCompressionCode, -) -> *mut SPCommand { - unsafe {sp_command_bitmap_linear_internal(offset, bit_vec, compression, BinaryOperation::Xor)} -} - -#[inline] -unsafe fn sp_command_bitmap_linear_internal( - offset: usize, - bit_vec: *mut SPBitVec, - compression: SPCompressionCode, - operation: BinaryOperation -) -> *mut SPCommand { +) -> NonNull { assert!(!bit_vec.is_null()); - let bit_vec = unsafe { *Box::from_raw(bit_vec) }; - let compression = match compression.try_into() { - Ok(compression) => compression, - Err(_) => return std::ptr::null_mut(), - }; - let command = SPCommand(servicepoint::BitVecCommand { + let bit_vec = *Box::from_raw(bit_vec); + let result = Box::new(SPCommand(servicepoint::Command::BitmapLinearXor( offset, - operation, - bitvec: bit_vec.into(), - compression, - }.into()); - Box::leak(Box::new(command)) + bit_vec.into(), + compression.try_into().expect("invalid compression code"), + ))); + NonNull::from(Box::leak(result)) } /// Show codepage 437 encoded text on the screen. @@ -384,12 +391,11 @@ pub unsafe extern "C" fn sp_command_cp437_data( grid: *mut SPCp437Grid, ) -> NonNull { assert!(!grid.is_null()); - let grid = *unsafe{Box::from_raw(grid)}; - let result = Box::new(SPCommand(servicepoint::Cp437GridCommand { - origin: servicepoint::Origin::new(x, - y), - grid: grid.0, - }.into())); + let grid = *Box::from_raw(grid); + let result = Box::new(SPCommand(servicepoint::Command::Cp437Data( + servicepoint::Origin::new(x, y), + grid.0, + ))); NonNull::from(Box::leak(result)) } @@ -418,12 +424,11 @@ pub unsafe extern "C" fn sp_command_utf8_data( grid: *mut SPCharGrid, ) -> NonNull { assert!(!grid.is_null()); - let grid = unsafe { *Box::from_raw(grid) }; - let result = Box::new(SPCommand(servicepoint::CharGridCommand { - origin: servicepoint::Origin::new(x, - y), - grid: grid.0, - }.into())); + let grid = *Box::from_raw(grid); + let result = Box::new(SPCommand(servicepoint::Command::Utf8Data( + servicepoint::Origin::new(x, y), + grid.0, + ))); NonNull::from(Box::leak(result)) } @@ -452,20 +457,18 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win( x: usize, y: usize, bitmap: *mut SPBitmap, - compression: SPCompressionCode, -) -> *mut SPCommand { + compression_code: SPCompressionCode, +) -> NonNull { assert!(!bitmap.is_null()); - let bitmap = unsafe { *Box::from_raw(bitmap) }.0; - let compression = match compression.try_into() { - Ok(compression) => compression, - Err(_) => return std::ptr::null_mut(), - }; - let command = SPCommand(servicepoint::BitmapCommand { - origin: servicepoint::Origin::new(x, y), - bitmap, - compression, - }.into()); - Box::leak(Box::new(command)) + let byte_grid = (*Box::from_raw(bitmap)).0; + let result = Box::new(SPCommand(servicepoint::Command::BitmapLinearWin( + servicepoint::Origin::new(x, y), + byte_grid, + compression_code + .try_into() + .expect("invalid compression code"), + ))); + NonNull::from(Box::leak(result)) } /// Deallocates a [SPCommand]. @@ -491,5 +494,5 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win( #[no_mangle] pub unsafe extern "C" fn sp_command_free(command: *mut SPCommand) { assert!(!command.is_null()); - _ = unsafe { Box::from_raw(command) }; + _ = Box::from_raw(command); } diff --git a/src/connection.rs b/src/connection.rs index 6bbdbe2..8b31243 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -3,7 +3,8 @@ //! prefix `sp_connection_` use std::ffi::{c_char, CStr}; -use servicepoint::Connection; +use std::ptr::{null_mut, NonNull}; + use crate::{SPCommand, SPPacket}; /// A connection to the display. @@ -15,7 +16,7 @@ use crate::{SPCommand, SPPacket}; /// if (connection != NULL) /// sp_connection_send_command(connection, sp_command_clear()); /// ``` -pub struct SPConnection(pub(crate) servicepoint::UdpConnection); +pub struct SPConnection(pub(crate) servicepoint::Connection); /// Creates a new instance of [SPConnection]. /// @@ -36,30 +37,30 @@ pub unsafe extern "C" fn sp_connection_open( host: *const c_char, ) -> *mut SPConnection { assert!(!host.is_null()); - let host = unsafe {CStr::from_ptr(host)}.to_str().expect("Bad encoding"); - let connection = match servicepoint::UdpConnection::open(host) { - Err(_) => return std::ptr::null_mut(), + let host = CStr::from_ptr(host).to_str().expect("Bad encoding"); + let connection = match servicepoint::Connection::open(host) { + Err(_) => return null_mut(), Ok(value) => value, }; Box::into_raw(Box::new(SPConnection(connection))) } -// /// Creates a new instance of [SPUdpConnection] for testing that does not actually send anything. -// /// -// /// returns: a new instance. Will never return NULL. -// /// -// /// # Safety -// /// -// /// The caller has to make sure that: -// /// -// /// - the returned instance is freed in some way, either by using a consuming function or -// /// by explicitly calling `sp_connection_free`. -// #[no_mangle] -// pub unsafe extern "C" fn sp_connection_fake() -> NonNull { -// let result = Box::new(SPUdpConnection(servicepoint::Connection::Fake)); -// NonNull::from(Box::leak(result)) -// } +/// Creates a new instance of [SPConnection] for testing that does not actually send anything. +/// +/// returns: a new instance. Will never return NULL. +/// +/// # Safety +/// +/// The caller has to make sure that: +/// +/// - the returned instance is freed in some way, either by using a consuming function or +/// by explicitly calling `sp_connection_free`. +#[no_mangle] +pub unsafe extern "C" fn sp_connection_fake() -> NonNull { + let result = Box::new(SPConnection(servicepoint::Connection::Fake)); + NonNull::from(Box::leak(result)) +} /// Sends a [SPPacket] to the display using the [SPConnection]. /// @@ -86,8 +87,8 @@ pub unsafe extern "C" fn sp_connection_send_packet( ) -> bool { assert!(!connection.is_null()); assert!(!packet.is_null()); - let packet = unsafe {Box::from_raw(packet)}; - unsafe {(*connection).0.send((*packet).0)}.is_ok() + let packet = Box::from_raw(packet); + (*connection).0.send((*packet).0).is_ok() } /// Sends a [SPCommand] to the display using the [SPConnection]. @@ -115,8 +116,8 @@ pub unsafe extern "C" fn sp_connection_send_command( ) -> bool { assert!(!connection.is_null()); assert!(!command.is_null()); - let command = (*unsafe {Box::from_raw(command)}).0; - unsafe{(*connection).0.send(command)}.is_ok() + let command = (*Box::from_raw(command)).0; + (*connection).0.send(command).is_ok() } /// Closes and deallocates a [SPConnection]. @@ -134,5 +135,5 @@ pub unsafe extern "C" fn sp_connection_send_command( #[no_mangle] pub unsafe extern "C" fn sp_connection_free(connection: *mut SPConnection) { assert!(!connection.is_null()); - _ = unsafe {Box::from_raw(connection)}; + _ = Box::from_raw(connection); } diff --git a/src/constants.rs b/src/constants.rs index ed4e52d..1a268f4 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -43,6 +43,6 @@ impl TryFrom for CompressionCode { type Error = (); fn try_from(value: SPCompressionCode) -> Result { - CompressionCode::try_from(value as u16).map_err(|_|()) + CompressionCode::try_from(value as u16) } } diff --git a/src/cp437_grid.rs b/src/cp437_grid.rs index 712034c..9b366c8 100644 --- a/src/cp437_grid.rs +++ b/src/cp437_grid.rs @@ -69,15 +69,13 @@ pub unsafe extern "C" fn sp_cp437_grid_load( height: usize, data: *const u8, data_length: usize, -) -> *mut SPCp437Grid { +) -> NonNull { assert!(data.is_null()); - let data = unsafe{std::slice::from_raw_parts(data, data_length)}; - let grid = servicepoint::Cp437Grid::load( width, height, data, ); - if let Some(grid ) = grid { - Box::leak(Box::new(SPCp437Grid(grid))) - } else { - std::ptr::null_mut() - } + let data = std::slice::from_raw_parts(data, data_length); + let result = Box::new(SPCp437Grid(servicepoint::Cp437Grid::load( + width, height, data, + ))); + NonNull::from(Box::leak(result)) } /// Clones a [SPCp437Grid]. @@ -101,7 +99,7 @@ pub unsafe extern "C" fn sp_cp437_grid_clone( cp437_grid: *const SPCp437Grid, ) -> NonNull { assert!(!cp437_grid.is_null()); - let result = Box::new(unsafe {(*cp437_grid).clone()}); + let result = Box::new((*cp437_grid).clone()); NonNull::from(Box::leak(result)) } @@ -123,7 +121,7 @@ pub unsafe extern "C" fn sp_cp437_grid_clone( #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut SPCp437Grid) { assert!(!cp437_grid.is_null()); - _ = unsafe {Box::from_raw(cp437_grid)}; + _ = Box::from_raw(cp437_grid); } /// Gets the current value at the specified position. @@ -151,7 +149,7 @@ pub unsafe extern "C" fn sp_cp437_grid_get( y: usize, ) -> u8 { assert!(!cp437_grid.is_null()); - unsafe{(*cp437_grid).0.get(x, y)} + (*cp437_grid).0.get(x, y) } /// Sets the value of the specified position in the [SPCp437Grid]. @@ -185,7 +183,7 @@ pub unsafe extern "C" fn sp_cp437_grid_set( value: u8, ) { assert!(!cp437_grid.is_null()); - unsafe {(*cp437_grid).0.set(x, y, value)}; + (*cp437_grid).0.set(x, y, value); } /// Sets the value of all cells in the [SPCp437Grid]. @@ -211,7 +209,7 @@ pub unsafe extern "C" fn sp_cp437_grid_fill( value: u8, ) { assert!(!cp437_grid.is_null()); - unsafe {(*cp437_grid).0.fill(value)}; + (*cp437_grid).0.fill(value); } /// Gets the width of the [SPCp437Grid] instance. @@ -234,7 +232,7 @@ pub unsafe extern "C" fn sp_cp437_grid_width( cp437_grid: *const SPCp437Grid, ) -> usize { assert!(!cp437_grid.is_null()); - unsafe {(*cp437_grid).0.width()} + (*cp437_grid).0.width() } /// Gets the height of the [SPCp437Grid] instance. @@ -257,7 +255,7 @@ pub unsafe extern "C" fn sp_cp437_grid_height( cp437_grid: *const SPCp437Grid, ) -> usize { assert!(!cp437_grid.is_null()); - unsafe {(*cp437_grid).0.height()} + (*cp437_grid).0.height() } /// Gets an unsafe reference to the data of the [SPCp437Grid] instance. @@ -279,7 +277,7 @@ pub unsafe extern "C" fn sp_cp437_grid_height( pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref( cp437_grid: *mut SPCp437Grid, ) -> SPByteSlice { - let data = unsafe {(*cp437_grid).0.data_ref_mut()}; + let data = (*cp437_grid).0.data_ref_mut(); SPByteSlice { start: NonNull::new(data.as_mut_ptr_range().start).unwrap(), length: data.len(), diff --git a/src/packet.rs b/src/packet.rs index 12afb34..9293a8a 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -2,7 +2,7 @@ //! //! prefix `sp_packet_` -use std::ptr::{NonNull}; +use std::ptr::{null_mut, NonNull}; use crate::SPCommand; @@ -29,12 +29,11 @@ pub struct SPPacket(pub(crate) servicepoint::Packet); #[no_mangle] pub unsafe extern "C" fn sp_packet_from_command( command: *mut SPCommand, -) -> *mut SPPacket { +) -> NonNull { assert!(!command.is_null()); - let command = unsafe {*Box::from_raw(command)}; - if let Ok(packet) = command.0.try_into() { - Box::leak(Box::new(SPPacket(packet))) - } else { std::ptr::null_mut() } + let command = *Box::from_raw(command); + let result = Box::new(SPPacket(command.0.into())); + NonNull::from(Box::leak(result)) } /// Tries to load a [SPPacket] from the passed array with the specified length. @@ -59,9 +58,9 @@ pub unsafe extern "C" fn sp_packet_try_load( length: usize, ) -> *mut SPPacket { assert!(!data.is_null()); - let data = unsafe {std::slice::from_raw_parts(data, length)}; + let data = std::slice::from_raw_parts(data, length); match servicepoint::Packet::try_from(data) { - Err(_) => std::ptr::null_mut(), + Err(_) => null_mut(), Ok(packet) => Box::into_raw(Box::new(SPPacket(packet))), } } @@ -105,7 +104,7 @@ pub unsafe extern "C" fn sp_packet_from_parts( let payload = if payload.is_null() { vec![] } else { - let payload = unsafe {std::slice::from_raw_parts(payload, payload_len) }; + let payload = std::slice::from_raw_parts(payload, payload_len); Vec::from(payload) }; @@ -144,7 +143,7 @@ pub unsafe extern "C" fn sp_packet_clone( packet: *const SPPacket, ) -> NonNull { assert!(!packet.is_null()); - let result = Box::new(SPPacket(unsafe {(*packet).0.clone()})); + let result = Box::new(SPPacket((*packet).0.clone())); NonNull::from(Box::leak(result)) } @@ -163,5 +162,5 @@ pub unsafe extern "C" fn sp_packet_clone( #[no_mangle] pub unsafe extern "C" fn sp_packet_free(packet: *mut SPPacket) { assert!(!packet.is_null()); - _ = unsafe {Box::from_raw(packet)} + _ = Box::from_raw(packet) }