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

Diagram for code layouts in safer ffi #107

Open
wants to merge 11 commits into
base: ditto-node-js
Choose a base branch
from
Open
17 changes: 8 additions & 9 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- macos-latest
- windows-latest
rust:
- 1.52.0
- 1.57.0
- stable
# - nightly
steps:
Expand Down Expand Up @@ -51,18 +51,18 @@ jobs:
needs: check
strategy:
matrix:
feature--proc_macros: ['', '--features proc_macros']
feature--std: ['', '--features std']
feature--alloc: ['', '--features alloc']
feature--log: ['', '--features log']
feature--async-fn: ['', '--features async-fn']
# feature--log: ['', '--features log']
feature--out-refs: ['', '--features out-refs']
feature--node-js: ['', '--features node-js']
steps:
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.52.0
toolchain: 1.57.0
override: true

- name: Clone repo
Expand All @@ -73,10 +73,9 @@ jobs:
command: check
args: |
--no-default-features
${{ matrix.feature--proc_macros }}
${{ matrix.feature--std }}
${{ matrix.feature--alloc }}
${{ matrix.feature--log }}
${{ matrix.feature--async-fn }}
${{ matrix.feature--out-refs }}
${{ matrix.feature--node-js }}

Expand All @@ -93,7 +92,7 @@ jobs:
- macos-latest
# - windows-latest
rust:
# - 1.52.0
# - 1.57.0
- stable
- nightly
steps:
Expand All @@ -116,7 +115,7 @@ jobs:
RUST_BACKTRACE: full
with:
command: test
args: --no-default-features --features derives,std
args: --no-default-features --features std

- name: FFI test (C & C#?)
run: make -C ffi_tests
Expand Down Expand Up @@ -158,7 +157,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: doc
args: --all-features
args: --features docs

- name: Build guide and documentation
run: |
Expand Down
66 changes: 41 additions & 25 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ path = "src/_lib.rs"

[package]
name = "safer-ffi"
# Keep in sync with `[dependencies.proc_macro]` and `src/proc_macro/Cargo.toml`
version = "0.0.5"
version = "0.1.0" # Keep in sync
authors = ["Daniel Henry-Mantilla <[email protected]>"]
edition = "2018"
edition = "2021"

description = "Write safer FFI code in Rust without polluting it with unsafe code"
keywords = ["ffi", "no_std", "framework", "safety", "bindings"]
Expand All @@ -18,36 +17,52 @@ readme = "README.md"
[features]
default = [
"std",
# FIXME: REMOVE!
"dyn-traits", "csharp-headers",
]

# Document under the following features: all but for debug or experimental.
docs = [
"csharp-headers",
"default",
"nightly",
"uninit",
]

nightly = []

alloc = []
std = [ "alloc" ]
std = [
"alloc",
]

# Deprecated
proc_macros = []

async-fn = [
"safer_ffi-proc_macros/async-fn",
]

derives = ["proc_macros"] # alias
proc_macros = [
"safer_ffi-proc_macros/proc_macros",
"require_unsafe_in_body",
"proc-macro-hack",
]
debug_proc_macros = [
"proc_macros",
"safer_ffi-proc_macros/verbose-expansions",
]

out-refs = ["uninit"]
dyn-traits = [
"safer_ffi-proc_macros/dyn-traits",
]

out-refs = [
"uninit",
]

headers = [
"inventory",
"safer_ffi-proc_macros/headers",
"std",
]
csharp-headers = ["headers"]
csharp-headers = [
"headers",
]

node-js = [
"async-fn",
Expand All @@ -57,23 +72,26 @@ node-js = [
# "napi-derive",
"safer_ffi-proc_macros/node-js",
# For convenience
"proc_macros", "std",
"std",
]

[dev-dependencies]
macro_rules_attribute = "0.0.1"
macro_rules_attribute = "0.1.0"

[target.'cfg(not(target = "wasm32-unknown-unknown"))'.dependencies]
libc = { version = "0.2.66", default-features = false }

[dependencies]
paste = { package = "mini_paste", version = "0.1.*" }
log = { version = "0.4.8", optional = true }
require_unsafe_in_body = { version = "0.2.1", optional = true }
inventory = { version = "0.1.6", optional = true }
proc-macro-hack = { version = "0.5.15", optional = true }
unwind_safe = "0.0.1"
scopeguard = "1.1.0"
log.version = "0.4.8"
log.optional = true
inventory.version = "0.1.6"
inventory.optional = true
scopeguard.version = "1.1.0"
unwind_safe.version = "0.1.0"
fstrings = "0.2.3"
with_builtin_macros.version = "0.0.3"
macro_rules_attribute = "0.1.0"

[dependencies.napi]
package = "napi-dispatcher"
Expand All @@ -88,9 +106,8 @@ path = "napi-dispatcher"
# branch = "ditto/closure-into-jsfunction"

[dependencies.safer_ffi-proc_macros]
# package = "safer_ffi-proc_macros"
path = "src/proc_macro"
version = "0.0.5"
version = "0.1.0-rc1" # Keep in sync

[dependencies.uninit]
optional = true
Expand All @@ -111,5 +128,4 @@ members = [
]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docs"]
features = ["docs"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ https://github.com/getditto/safer_ffi)

## Prerequisites

Minimum Supported Rust Version: `1.52.0`
Minimum Supported Rust Version: `1.57.0`

## Quickstart

Expand Down
Binary file added docs/layouts.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion ffi_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ edition = "2018"

[dependencies.safer-ffi]
path = ".."
features = ["async-fn", "proc_macros"]
features = ["async-fn"]

[features]
generate-headers = ["safer-ffi/csharp-headers"]
Expand Down
3 changes: 3 additions & 0 deletions ffi_tests/generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public unsafe struct AnUnusedStruct_t {
public Wow_t are_you_still_there;
}

/// <summary>
/// Hello, <c>World</c>!
/// </summary>
public enum Triforce_t : byte {
Din = 3,
Farore = 1,
Expand Down
12 changes: 8 additions & 4 deletions ffi_tests/generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@ typedef struct {

} AnUnusedStruct_t;

/** \brief
* Hello, `World`!
*/
/** \remark Has the same ABI as `uint8_t` **/
#ifdef DOXYGEN
typedef enum Triforce
#else
typedef uint8_t Triforce_t; enum
#endif
{
/** . */
/** <No documentation available> */
TRIFORCE_DIN = 3,
/** . */
/** <No documentation available> */
TRIFORCE_FARORE = 1,
/** . */
/** <No documentation available> */
TRIFORCE_NARYU,
}
#ifdef DOXYGEN
Expand All @@ -73,14 +76,15 @@ typedef enum SomeReprCEnum {
void check_SomeReprCEnum (
SomeReprCEnum_t _baz);

/** <No documentation available> */
/** \remark Has the same ABI as `uint8_t` **/
#ifdef DOXYGEN
typedef enum Bar
#else
typedef uint8_t Bar_t; enum
#endif
{
/** . */
/** <No documentation available> */
BAR_A,
}
#ifdef DOXYGEN
Expand Down
22 changes: 17 additions & 5 deletions ffi_tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg_attr(rustfmt, rustfmt::skip)]

use ::safer_ffi::prelude::*;

/// Concatenate the two input strings into a new one.
Expand Down Expand Up @@ -91,7 +92,7 @@ mod foo {

mod bar {
use super::*;
#[derive_ReprC]
#[derive_ReprC2]
#[repr(u8)]
pub
enum Bar { A }
Expand Down Expand Up @@ -127,15 +128,26 @@ pub enum Wow {
Jenkins,
}

/// Hello, `World`!
#[ffi_export]
#[derive_ReprC]
#[derive_ReprC2]
#[repr(u8)]
pub enum Triforce {
Din = 3,
Farore = 1,
Farore = Triforce::Din as u8 - 2,
Naryu,
}

/// Hello, `World`!
// #[ffi_export]
#[derive_ReprC2]
pub struct Next {
/// I test some `gen`-eration.
gen: bar::Bar,
/// with function pointers and everything!
cb: extern "C" fn(u8) -> i8,
}

#[ffi_export]
#[derive_ReprC]
#[repr(C)]
Expand All @@ -148,8 +160,8 @@ pub struct AnUnusedStruct {
fn generate_headers ()
-> ::std::io::Result<()>
{Ok({
use ::safer_ffi::headers::Language::*;
for &(language, ext) in &[(C, "h"), (CSharp, "cs")] {
use ::safer_ffi::headers::Language::{self, *};
for &(language, ext) in &[(C, "h"), (Language::CSharp, "cs")][..1] {
let builder =
::safer_ffi::headers::builder()
.with_language(language)
Expand Down
2 changes: 1 addition & 1 deletion ffi_tests/tests/csharp/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>netcoreapp6.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
1 change: 0 additions & 1 deletion js_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ futures = "0.3.15"
path = ".."
features = [
# "debug_proc_macros",
"proc_macros",
"out-refs",
]

Expand Down
2 changes: 1 addition & 1 deletion js_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn concat (s1: char_p::Ref<'_>, s2: char_p::Ref<'_>)
}

#[ffi_export(node_js)]
fn concat_bytes (
fn concat_byte_slices (
xs1: Option<c_slice::Ref<'_, u8>>,
xs2: Option<c_slice::Ref<'_, u8>>,
) -> Option<c_slice::Box<u8>>
Expand Down
2 changes: 1 addition & 1 deletion js_tests/tests/tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export async function run_tests({ ffi, performance, assert, is_web }) {
);

assert.deepEqual(
Uint8Array.from(ffi.boxCBytesIntoBuffer(ffi.concat_bytes(
Uint8Array.from(ffi.boxCBytesIntoBuffer(ffi.concat_byte_slices(
Uint8Array.from('Hello, ', c => c.charCodeAt(0)),
Uint8Array.from('World!', c => c.charCodeAt(0)),
))),
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.52.0
1.60.0
Loading