Skip to content

Commit

Permalink
Fixed merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
tbetcke committed Nov 26, 2024
2 parents 64a2221 + 9664159 commit 151d1e9
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 10 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: 🧪

on:
push:
branches:
- "**"
pull_request:
branches:
- main
merge_group:

jobs:
run-tests-rust:
name: Run Rust tests
runs-on: ubuntu-latest
strategy:
matrix:
rust-version: ["stable"]
feature-flags: ['--features "strict"']
steps:
- name: Set up Rust
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: ${{ matrix.rust-version }}
components: rustfmt
- uses: actions/checkout@v3

- name: Run unit tests
run: cargo test ${{ matrix.feature-flags }}
- name: Run unit tests in release mode
run: cargo test --release ${{ matrix.feature-flags }}

check-dependencies:
name: Check dependencies
runs-on: ubuntu-latest
strategy:
matrix:
rust-version: ["stable"]
steps:
- name: Set up Rust
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: ${{ matrix.rust-version }}
components: rustfmt
- name: Install cargo-upgrades
run: cargo install cargo-upgrades
- uses: actions/checkout@v3
- name: Check that dependencies are up to date
run:
cargo upgrades
34 changes: 34 additions & 0 deletions .github/workflows/run-weekly-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: 🧪📅

on:
schedule:
- cron: "0 7 * * 1"

jobs:
run-tests-rust:
name: Run Rust tests
runs-on: ubuntu-latest
strategy:
matrix:
rust-version: ["stable", "beta", "nightly"]
steps:
- name: Set up Rust
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: ${{ matrix.rust-version }}
components: rustfmt
- uses: actions/checkout@v3

- name: Build rust library
run: cargo build --features "strict"
- name: Build rust library in release mode
run: cargo build --release --features "strict"


- name: Run unit tests
run: cargo test --features "strict"
- name: Run unit tests in release mode
run: cargo test --release --features "strict"

- name: Build docs
run: cargo doc --features "strict" --no-deps
32 changes: 32 additions & 0 deletions .github/workflows/style-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name:

on:
push:
branches:
- "**"
pull_request:
branches:
- main
merge_group:

jobs:
style-checks:
name: Rust style checks
runs-on: ubuntu-latest
strategy:
matrix:
feature-flags: ['--features "strict"', '']
steps:
- name: Set up Rust
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: stable
components: rustfmt,clippy
- uses: actions/checkout@v3

- name: Style checks
run: |
cargo fmt -- --check
cargo clippy ${{ matrix.feature-flags }} -- -D warnings
cargo clippy --tests ${{ matrix.feature-flags }} -- -D warnings
cargo clippy --examples ${{ matrix.feature-flags }} -- -D warnings
7 changes: 1 addition & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

workspace = { members = ["c-api-tools-macros", "example_project"] }

[features]
strict = []

[package]
name = "c-api-tools"
version = "0.1.0-dev"
version = "0.0.0-dev"
edition = "2021"
authors = [
"Timo Betcke <[email protected]>",
Expand All @@ -25,13 +24,9 @@ name = "c_api_tools"
crate-type = ["lib", "cdylib"]

[dependencies]
itertools = "0.13.*"
num = "0.4"
c-api-tools-macros = { path = "./c-api-tools-macros" }

[dev-dependencies]
paste = "1.*"

[build-dependencies]
cbindgen = "0.27.0"

Expand Down
2 changes: 1 addition & 1 deletion c-api-tools-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ strict = []

[package]
name = "c-api-tools-macros"
version = "0.1.3-dev"
version = "0.0.0-dev"
edition = "2021"
authors = [
"Matthew Scroggs <[email protected]>",
Expand Down
5 changes: 5 additions & 0 deletions c-api-tools-macros/src/attribute_c_wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ pub(crate) fn c_wrapper_impl(args: TokenStream, item: TokenStream) -> TokenStrea
}

impl #ident {
/// Return reference to wrapped pointer.
pub fn inner(&self) -> &Box<dyn std::any::Any> {
&self._ptr
}
/// Return mutable reference to wrapped pointer.
pub fn inner_mut(&mut self) -> &mut Box<dyn std::any::Any> {
&mut self._ptr
}
Expand All @@ -98,6 +100,7 @@ pub(crate) fn c_wrapper_impl(args: TokenStream, item: TokenStream) -> TokenStrea
let name = syn::Ident::new((name.clone() + "_create").as_str(), Span::call_site());

output.extend(quote! {
/// Create a new instance of the wrapper.
#[no_mangle]
pub extern "C" fn #name() -> *mut #ident {
let obj = #ident { _ptr: Box::new(()) };
Expand All @@ -111,6 +114,7 @@ pub(crate) fn c_wrapper_impl(args: TokenStream, item: TokenStream) -> TokenStrea
let name = syn::Ident::new((name.clone() + "_free").as_str(), Span::call_site());

output.extend(quote! {
/// Free the instance of the wrapper.
#[no_mangle]
pub unsafe extern "C" fn #name(ptr: *mut #ident) {
if ptr.is_null() {
Expand All @@ -127,6 +131,7 @@ pub(crate) fn c_wrapper_impl(args: TokenStream, item: TokenStream) -> TokenStrea
let name = syn::Ident::new((name.clone() + "_unwrap").as_str(), Span::call_site());

output.extend(quote! {
/// Unwrap the instance of the wrapper.
unsafe fn #name(ptr: *mut #ident) -> Option<&'static mut Box<dyn std::any::Any>> {
if ptr.is_null() {
return None;
Expand Down
8 changes: 6 additions & 2 deletions c-api-tools-macros/src/attribute_concretise_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,11 @@ struct ConcretiseTypeArgs {

pub(crate) fn concretise_type_impl(args: TokenStream, item: TokenStream) -> TokenStream {
let syn::ItemFn {
vis, sig, block, ..
vis,
sig,
block,
attrs,
..
} = parse_macro_input!(item as syn::ItemFn);

let attr_args = match NestedMeta::parse_meta_list(args.into()) {
Expand Down Expand Up @@ -352,12 +356,12 @@ pub(crate) fn concretise_type_impl(args: TokenStream, item: TokenStream) -> Toke
// We now put everything together.

let output = quote! {
#( #attrs)*
#[no_mangle]
#vis #new_signature {
#vis #sig
#block


#if_let_stream
{
panic!("Unknown type.");
Expand Down
6 changes: 6 additions & 0 deletions c-api-tools-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
//! C API tools macros
#![cfg_attr(feature = "strict", deny(warnings), deny(unused_crate_dependencies))]
#![warn(missing_docs)]

mod attribute_c_wrappers;
mod attribute_concretise_types;

use attribute_c_wrappers::c_wrapper_impl;
use attribute_concretise_types::concretise_type_impl;
use proc_macro::TokenStream;

/// C functions
#[proc_macro_attribute]
pub fn cfuncs(args: TokenStream, item: TokenStream) -> TokenStream {
c_wrapper_impl(args, item)
}

/// Concretise types
#[proc_macro_attribute]
pub fn concretise_types(args: TokenStream, item: TokenStream) -> TokenStream {
concretise_type_impl(args, item)
Expand Down
9 changes: 9 additions & 0 deletions example_project/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
#include <stdint.h>
#include <stdlib.h>

/**
* Wrapper for `MyStruct`.
*/
typedef struct MyWrapper MyWrapper;

/**
* Create a new instance of the wrapper.
*/
struct MyWrapper *my_wrapper_create(void);

/**
* Free the instance of the wrapper.
*/
void my_wrapper_free(struct MyWrapper *ptr);

void test_func(struct MyWrapper *spam);
10 changes: 10 additions & 0 deletions example_project/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
//! Example project
#![cfg_attr(feature = "strict", deny(warnings), deny(unused_crate_dependencies))]
#![warn(missing_docs)]

use std::fmt::Display;

pub use c_api_tools::cfuncs;
pub use c_api_tools::concretise_types;

#[cfuncs(name = "my_wrapper", create, free, unwrap)]
/// Wrapper for `MyStruct`.
pub struct MyWrapper;

/// Some struct
pub struct MyStruct<T: num::Float, V: num::Float> {
/// First field.
pub a: T,
/// Second field.
pub b: V,
}

impl<T: num::Float, V: num::Float> MyStruct<T, V> {
/// Generate a new instance of `MyStruct`.
pub fn new(a: T, b: V) -> Self {
Self { a, b }
}
Expand All @@ -22,6 +31,7 @@ impl<T: num::Float, V: num::Float> MyStruct<T, V> {
gen_type(name = "dtype2", replace_with = ["f32", "f64"]),
field(arg = 0, name = "wrap", wrapper = "MyWrapper", replace_with = ["MyStruct<{{dtype1}}, {{dtype2}}>"]),
)]
/// Test function.
pub fn test_func<T: num::Float + Display, V: num::Float + Display>(spam: &MyStruct<T, V>) {
println!("{} {}", spam.a, spam.b);
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//! C API tools
#![cfg_attr(feature = "strict", deny(warnings), deny(unused_crate_dependencies))]
#![warn(missing_docs)]

mod types;

pub use c_api_tools_macros::cfuncs;
Expand Down
20 changes: 19 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
pub enum DType {
/// Float 32
F32,
/// Float 64
F64,
/// Complex 32
C32,
/// Complex 64
C64,
/// Unsigned int 8
U8,
/// Unsigned int 32
U32,
/// Unsigned int 64
U64,
/// Int 8
I8,
/// Int 32
I32,
/// Int 64
I64,
/// Undefined type
Undefined,
}

impl DType {
/// Return true of type is real.
pub fn is_real(&self) -> bool {
matches!(
self,
Expand All @@ -33,10 +45,12 @@ impl DType {
)
}

/// Return true if type is complex.
pub fn is_complex(&self) -> bool {
matches!(self, DType::C32 | DType::C64)
}

/// Return the associated real type.
pub fn real_type(&self) -> DType {
match self {
DType::C32 => DType::F32,
Expand All @@ -45,6 +59,9 @@ impl DType {
}
}

/// Return the associated complex type.
///
/// If there is no associated complex type `DType::Undefined` is returned.
pub fn complex_type(&self) -> DType {
match self {
DType::F32 => DType::C32,
Expand All @@ -54,8 +71,9 @@ impl DType {
}
}

/// Return runtime numeric type information.
/// Runtime numeric type information.
pub trait DTypeIdentifier {
/// Return runtime numeric type information.
fn dtype() -> DType;
}

Expand Down

0 comments on commit 151d1e9

Please sign in to comment.