Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: C bindings #404

Draft
wants to merge 57 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
6920939
C binding experiments
nicoburns Mar 21, 2023
b93c73b
Simplify conversion functions
nicoburns Mar 21, 2023
c04de15
Reorder padding/margin examples
nicoburns Mar 21, 2023
9d0112a
Add getter example
nicoburns Mar 21, 2023
0ad8c37
Return error code rather than panicking on null style pointer
nicoburns Mar 21, 2023
ab9289b
cargo fmt
nicoburns Mar 21, 2023
ae72e49
Documentation get_style and with_style_mut macros
nicoburns Mar 21, 2023
31bfebb
Split FFI into multiple files
nicoburns Mar 21, 2023
ff7fe6c
Rename api module to style
nicoburns Mar 21, 2023
ca2c223
Make style getters/setters unsafe
nicoburns Mar 21, 2023
54fc2dd
Add grid placement APIs
nicoburns Mar 21, 2023
cc73f28
Prefix style methods with TaffyStyle
nicoburns Mar 21, 2023
27f4f8b
Move c bindings to an external crate
nicoburns Sep 22, 2023
52429af
Eliminate boxes and raw pointers from C api
nicoburns Sep 22, 2023
9c4c58c
cargo fmt
nicoburns Sep 22, 2023
c0bd4f6
Make function UpperCamelCase with underscore between struct name and …
nicoburns Sep 22, 2023
76bd418
Use repr(C) instead of repr(u8)
nicoburns Sep 22, 2023
d80f333
Replace trbl function with function with dynamic edge parameter
nicoburns Sep 22, 2023
7656106
Implement getters/setters for all non-grid numeric style properties
nicoburns Sep 22, 2023
33677e7
Format grid style
nicoburns Sep 22, 2023
3a0668d
Update style size tests to include alignment
nicoburns Sep 22, 2023
be73591
Add Overflow to prelude
nicoburns Sep 22, 2023
7863849
Remove padding trbl function
nicoburns Sep 22, 2023
eff3a05
Implement Display, Position, Overflow
nicoburns Sep 22, 2023
b72c6bf
Start generating bindings
nicoburns Sep 22, 2023
ac6785a
Add style getters/setters for enum styles
nicoburns Sep 22, 2023
806ab20
Add abstraction for f32 getters/setters
nicoburns Sep 22, 2023
69aa336
Use individual unit and value for style value setters
nicoburns Sep 22, 2023
53d9c04
Use TaffyStyleConstRef and TaffyStyleMutRef types
nicoburns Sep 22, 2023
87355e2
fmt + clippy
nicoburns Sep 23, 2023
f6afd49
fmt prelude
nicoburns Sep 23, 2023
0060613
Use generic result struct
nicoburns Sep 23, 2023
611ab31
Prefix enum variant names + make screaming snake case
nicoburns Sep 23, 2023
c21640e
Core: Add try_style_mut function to Taffy struct
nicoburns Sep 23, 2023
253bb70
Refactor error handling + add initial tree manipulation functions
nicoburns Sep 23, 2023
24c89bf
Get "basic" example running via C bindings
nicoburns Sep 23, 2023
922be16
Fix clippy lint
nicoburns Sep 23, 2023
11693b6
Allow AvailableSpace to be specified in TaffyTree_ComputeLayout
nicoburns Sep 23, 2023
2b203d2
Exclude example binaries from git
nicoburns Sep 23, 2023
568a05b
Rename StyleValueUnit to TaffyUnit
nicoburns Sep 23, 2023
6cebb47
Ensure all types are prefixed by Taffy
nicoburns Sep 23, 2023
b96832f
Add style getter/setters for grid row
nicoburns Sep 23, 2023
9595562
Add TaffyTree_GetLayout method
nicoburns Sep 23, 2023
addbcb4
Remove bindgen from compile_basic.sh
nicoburns Sep 23, 2023
9853a18
README WIP
nicoburns Sep 23, 2023
6642172
README WIP
nicoburns Sep 23, 2023
345a21b
Fix build
nicoburns Sep 23, 2023
292fc3e
Fix markdown lints
nicoburns Oct 22, 2023
99ac7b3
Fix bindings after rebase
nicoburns Oct 22, 2023
ae1317e
Add ability to set measure function
nicoburns Oct 22, 2023
ae831dd
Add script to regenerate header file
nicoburns Oct 22, 2023
5c72ec1
Add CI task to build C bindings example
nicoburns Oct 22, 2023
3ac2afb
Remove result types from style getters
nicoburns Oct 30, 2023
a0820e6
Move c bindings to bindings/c directory
nicoburns Oct 30, 2023
b8879fd
Fix build after rebase
nicoburns Feb 12, 2024
49e1f33
Use nightly toolchain for c bindings (to enable cbindgen to work)
nicoburns Feb 12, 2024
b437b3b
Fix c bindings ci
nicoburns Feb 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,19 @@ jobs:
name: Build benchmarks (w/yoga)
env:
RUSTFLAGS: "-C opt-level=0"

build-c-bindings:
name: Build C Bindings
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: |
cd bindings/c
cargo build
name: Build ctaffy library
- run: |
cd bindings/c/examples
./compile_basic.sh
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../../../target/debug ./basic
name: Build C Bindings
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ path = "benches/dummy_benchmark.rs"
harness = false

[workspace]
members = ["scripts/gentest", "scripts/format-fixtures", "scripts/import-yoga-tests", "benches"]
members = ["scripts/gentest", "scripts/format-fixtures", "scripts/import-yoga-tests", "benches", "bindings/c"]
1 change: 1 addition & 0 deletions bindings/c/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
examples/basic
18 changes: 18 additions & 0 deletions bindings/c/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "ctaffy"
version = "0.0.1"
authors = [
"Nico Burns <nico@nicoburns.com>",
]
edition = "2021"
include = ["src/**/*", "Cargo.toml"]
description = "C bindings to Taffy (A flexible UI layout library)"
repository = "https://github.com/DioxusLabs/taffy"
license = "MIT"

[lib]
name = "ctaffy"
crate-type = ["staticlib", "cdylib", "rlib"]

[dependencies]
taffy = { path = "../.." }
97 changes: 97 additions & 0 deletions bindings/c/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# C API for Taffy (ctaffy)

Taffy is a flexible, high-performance, cross-platform UI layout library written in Rust.

This directory contains C bindings for Taffy. The API is a pure C API (no C++ features are used), and is designed to be easy to build other language bindings on top of. In addition to the documentation below, you may to read through the header file (`include/taffy.h`).

## Examples

There are readable examples in the examples directory.

Assuming you have Rust and Cargo installed (and a C compiler), then this should work to run the basic example:

```bash
git clone https://github.com/DioxusLabs/taffy.git
cd taffy/ctaffy/examples
./compile-basic.sh
./basic
```

## Naming Conventions

- Everything in the Taffy C API is prefixed with `Taffy`, except enum variant names which are prefixed with `TAFFY_`
- Structs and Enums are named in UpperCamelCase (e.g. `TaffyTree`, `TaffyStyle`)
- Functions begin with the name of the struct they apply to, followed by an underscore, followed by the name of the function in UpperCamelCase (e.g. `TaffyTree_New`, `TaffyStyle_SetFlexBasis`)
- Enum variants are SCREAMING_SNAKE_CASE

## Error Handling

Error handling is managed by the use of return codes and "result" structs. All functions in the API return either an `enum TaffyReturnCode` or one of the `struct Taffy*Result` structs (or `void` in the case of infallible operations that don't return anything).

### Return codes

Error handling is managed by the use of an enum `TaffyReturnCode`:

```c
typedef enum TaffyReturnCode {
TAFFY_RETURN_CODE_OK,
TAFFY_RETURN_CODE_NULL_STYLE_POINTER,
TAFFY_RETURN_CODE_NULL_TREE_POINTER,
// ... (see header file for full definition)
} TaffyReturnCode;
```

`TAFFY_RETURN_CODE_OK` indicates that the operation succeeded. All other variant indicate

### Result structs

"Result structs" are used for functions that need to return another value in addition to a `TaffyReturnCode` indicating success/failure (such as style getters which need to return the relevant style value). As C doesn't support generic structs, there are several "Result structs": one for each type of value. But each struct follows the same structure as the following example (varying only in the name of the struct and the type of the `value` field):

<table>
<tr><th>TaffyIntResult</th><th>TaffyDimensionResult</th></tr>
<tr>
<td>

```c
typedef struct TaffyIntResult {
enum TaffyReturnCode return_code;
int32_t value;
} TaffyIntResult;
```

</td>
<td>

```c
typedef struct TaffyDimensionResult {
enum TaffyReturnCode return_code;
struct TaffyDimension value;
} TaffyDimensionResult;
```

</td>
</tr>
</table>

Functions that return Result structs will either return a `TAFFY_RETURN_CODE_OK` along with a meaningful value, or a error variant of `TaffyReturnCode` along with

## API Usage

### Tree Creation and Manipulation

The `TaffyTree` struct is the entrypoint to the Taffy C API and represents an entire tree of UI nodes. Taffy uses arena allocation, so the `TaffyTree` owns the entire tree of nodes at all times.

You only ever get access to a `TaffyNodeId` handle which can be used to manipulate the structure of the tree, or pointers to `TaffyStyle` object (which can be used to set styles on a Node, but are considered borrowed pointers and thus must only be held temporarily).

#### Lifecycle

- `TaffyTree_New` allocates a new `TaffyTree` and returns an owned pointer to it.
- `TaffyTree_Free` can be used to free the tree once you are done with it.

All other functions in the API which accept a pointer to a `TaffyTree` have borrowing semantics: they access the tree during the duration of the function (and if the pointer is not a `const` pointer, may modify the tree), but will not store the pointer or take ownership of the tree.

#### Node creation and manipulation

- `TaffyTree_NewNode` creates a new Node within the tree and returns a `TaffyNodeId` handle to it. The node will initially have the default styles

### Setting styles on node
22 changes: 22 additions & 0 deletions bindings/c/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
language = "c"
cpp_compat = true
documentation_style = "c99"
line_length = 120

[enum]
rename_variants = "QualifiedScreamingSnakeCase"

[export]
include = ["TaffyNodeId"]

[export.rename]
TaffyResult_f32 = "TaffyFloatResult"
TaffyResult_i32 = "TaffyIntResult"
TaffyResult_TaffyNodeId = "TaffyNodeIdResult"
TaffyResult_TaffyDimension = "TaffyDimensionResult"
TaffyResult_TaffyGridPlacement = "TaffyGridPlacementResult"
TaffyResult_TaffyStyleMutRef = "TaffyStyleMutRefResult"

[parse.expand]
crates = ["ctaffy"]

41 changes: 41 additions & 0 deletions bindings/c/examples/basic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <stdio.h>

#include "taffy.h"

int main() {
// Create tree
TaffyTree *tree = TaffyTree_New();

// Create child (+set styles)
TaffyNodeId child = TaffyTree_NewNode(tree).value;
TaffyStyle *child_style = TaffyTree_GetStyleMut(tree, child).value;
TaffyStyle_SetWidth(child_style, 0.5, TAFFY_UNIT_PERCENT);
TaffyStyle_SetHeight(child_style, 0, TAFFY_UNIT_AUTO);

// Create parent (+set styles)
TaffyNodeId parent = TaffyTree_NewNode(tree).value;
TaffyStyle *parent_style = TaffyTree_GetStyleMut(tree, parent).value;
TaffyStyle_SetWidth(parent_style, 100, TAFFY_UNIT_LENGTH);
TaffyStyle_SetHeight(parent_style, 100, TAFFY_UNIT_LENGTH);
TaffyStyle_SetJustifyContent(parent_style, TAFFY_ALIGN_CONTENT_CENTER);

// Setup parent-child relationship
TaffyTree_AppendChild(tree, parent, child);

// Compute layout (100x100 viewport)
printf("\nCompute layout with 100x100 viewport:\n");
TaffyTree_ComputeLayout(tree, parent, 100, 100);
TaffyTree_PrintTree(tree, parent);

// Compute layout (infinite viewport)
printf("\nCompute layout with infinite viewport:\n");
TaffyTree_ComputeLayout(tree, parent, INFINITY, INFINITY);
TaffyTree_PrintTree(tree, parent);

// Free tree
TaffyTree_Free(tree);
return 0;
}
6 changes: 6 additions & 0 deletions bindings/c/examples/compile_basic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -ex

cargo build --manifest-path ../Cargo.toml
gcc -O3 -DDEBUG -o basic basic.c -std=c99 -Wall -I../include -L../../../target/debug -lctaffy
3 changes: 3 additions & 0 deletions bindings/c/generate-bindings.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

cbindgen --crate ctaffy --output include/taffy.h
Loading