Skip to content

Commit

Permalink
Make FFI optional (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
urschrei committed Nov 1, 2023
1 parent 6aebc12 commit 727df0d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
with:
use-cross: ${{ matrix.use-cross }}
command: build
args: --release --target=${{ matrix.target }} --features headers
args: --release --target=${{ matrix.target }} --features headers,ffi
- name: Gather Assets
run: |
src=$(pwd)
Expand Down
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "polylabel"
version = "2.5.0"
version = "3.0.0"
authors = ["Stephan Hügel <urschrei@gmail.com>"]
description = "A Rust implementation of the Polylabel algorithm for finding optimum polygon label positions."
readme = "README.md"
Expand All @@ -10,7 +10,7 @@ license = "MIT"
edition = "2021"

[dependencies]
libc = "0.2.149"
libc = {version = "0.2.149", default-features=false, optional = true}
geo = "0.26.0"
num-traits = "0.2.8"
thiserror = "1.0.4"
Expand All @@ -23,6 +23,7 @@ criterion = "0.4.0"

[features]
headers = ["cbindgen"]
ffi = ["libc"]

[lib]
name = "polylabel"
Expand All @@ -39,3 +40,6 @@ lto = true
name = "benchmark"
harness = false

[package.metadata.docs.rs]
# Features to pass to Cargo (default: [])
features = ["ffi"]
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ A command-line tool is available: `cargo install polylabel_cmd`. This enables th
https://docs.rs/polylabel

## FFI
### Enable the Cargo `ffi` and `headers` features to enable this functionality
Call `polylabel_ffi` with the following three mandatory arguments:
- [`Array`](https://docs.rs/polylabel/1.0.3/polylabel/struct.Array.html) (a struct with two fields):
- `data`: a void pointer to an array of two-element `c_double` arrays, each of which represents a point on the exterior Polygon shell)
Expand All @@ -49,7 +50,7 @@ The function returns a [struct](https://docs.rs/polylabel/1.0.3/polylabel/struct
- `x_pos`
- `y_pos`

A Python example is available in [`ffi.py`](ffi.py)
Headers are provided in the `include` directory. A Python example is available in [`ffi.py`](ffi.py)

An auto-generated header file is available at [`include/header.h`](include/header.h)

Expand Down
8 changes: 4 additions & 4 deletions include/header.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Generated with cbindgen:0.22.0 */
/* Generated with cbindgen:0.24.5 */

/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */

Expand Down Expand Up @@ -40,11 +40,11 @@ typedef struct WrapperArray {
*
* Accepts three arguments:
*
* - an exterior ring [`Array`](struct.Array.html)
* - zero or more interior rings [`WrapperArray`](struct.WrapperArray.html)
* - an exterior ring representing a Polygon shell or closed LineString
* - zero or more interior rings representing Polygon holes
* - a tolerance `c_double`.
* If an error occurs while attempting to calculate the label position, the resulting point coordinates
* will be NaN, NaN.
* will be `NaN, NaN`.
*/
struct Position polylabel_ffi(struct Array outer,
struct WrapperArray inners,
Expand Down
6 changes: 3 additions & 3 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ fn reconstitute2(arr: WrapperArray) -> Vec<Vec<[f64; 2]>> {
///
/// Accepts three arguments:
///
/// - an exterior ring [`Array`](struct.Array.html)
/// - zero or more interior rings [`WrapperArray`](struct.WrapperArray.html)
/// - an exterior ring representing a Polygon shell or closed LineString
/// - zero or more interior rings representing Polygon holes
/// - a tolerance `c_double`.
/// If an error occurs while attempting to calculate the label position, the resulting point coordinates
/// will be NaN, NaN.
/// will be `NaN, NaN`.
#[no_mangle]
pub extern "C" fn polylabel_ffi(
outer: Array,
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
)]
//! This crate provides a Rust implementation of the [Polylabel](https://github.com/mapbox/polylabel) algorithm
//! for finding the optimum position of a polygon label.
//!
//! ffi bindings are provided: enable the `ffi` and `headers` features when building the crate.
use geo::{prelude::*, Coord, Rect};
use geo::{GeoFloat, Point, Polygon};
use num_traits::FromPrimitive;
Expand All @@ -16,8 +18,10 @@ pub mod errors;
use errors::PolylabelError;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "ffi")]
mod ffi;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "ffi")]
pub use crate::ffi::{polylabel_ffi, Array, Position, WrapperArray};

/// Represention of a Quadtree node's cells. A node contains four Qcells.
Expand Down

0 comments on commit 727df0d

Please sign in to comment.