From 9b79896e1d0ef0f2a359868f1c082f4ded88b636 Mon Sep 17 00:00:00 2001 From: Tarrence van As Date: Thu, 2 Nov 2023 14:46:24 -0400 Subject: [PATCH] Setup ci --- .github/workflows/ci.yml | 57 +++++++++++++++++++++ .github/workflows/release.yml | 95 +++++++++++++++++++++++++++++++++++ Cargo.lock | 28 +++++------ Cargo.toml | 2 +- README.md | 2 +- build.rs | 8 +-- scripts/rust_fmt.sh | 3 ++ src/lib.rs | 26 ++++++---- 8 files changed, 190 insertions(+), 31 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/release.yml create mode 100755 scripts/rust_fmt.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..9ed6ce8 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,57 @@ +name: ci + +on: + push: + branches: + - main + pull_request: + +env: + CARGO_TERM_COLOR: always + RUST_VERSION: 1.70.0 + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ env.RUST_VERSION }} + - uses: Swatinem/rust-cache@v2 + - uses: arduino/setup-protoc@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + - name: Install nextest test runner + uses: taiki-e/install-action@nextest + - run: | + cargo nextest run --all-features + + clippy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ env.RUST_VERSION }} + targets: wasm32-unknown-unknown + components: clippy + - uses: Swatinem/rust-cache@v2 + - uses: arduino/setup-protoc@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + - run: scripts/clippy.sh + + fmt: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: nightly + components: rustfmt + - uses: Swatinem/rust-cache@v2 + - uses: arduino/setup-protoc@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + - run: scripts/rust_fmt.sh --check diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..dd5beac --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,95 @@ +name: release + +on: + push: + tags: + - "*" + +env: + CARGO_TERM_COLOR: always + RUST_VERSION: 1.70.0 + +jobs: + prepare: + name: Prepare release + runs-on: ubuntu-latest + + steps: + - name: Checkout sources + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + build-and-release: + name: ${{ matrix.job.target }} (${{ matrix.job.os }}) + needs: prepare + runs-on: ${{ matrix.job.os }} + strategy: + matrix: + job: + # The OS is used for the runner + # The platform is a generic platform name + # The target is used by Cargo + # The arch is either 386, arm64 or amd64 + # The svm target platform to use for the binary https://github.com/roynalnaruto/svm-rs/blob/84cbe0ac705becabdc13168bae28a45ad2299749/svm-builds/build.rs#L4-L24 + - os: ubuntu-latest-32-cores + platform: linux + target: x86_64-unknown-linux-gnu + arch: amd64 + - os: ubuntu-latest-32-cores + platform: linux + target: aarch64-unknown-linux-gnu + arch: arm64 + svm_target_platform: linux-aarch64 + - os: macos-latest + platform: darwin + target: x86_64-apple-darwin + arch: amd64 + - os: macos-latest + platform: darwin + target: aarch64-apple-darwin + arch: arm64 + - os: windows-latest + platform: win32 + target: x86_64-pc-windows-msvc + arch: amd64 + + steps: + - uses: actions/checkout@v3 + + - uses: dtolnay/rust-toolchain@master + name: Rust Toolchain Setup + with: + targets: ${{ matrix.job.target }} + toolchain: ${{ env.RUST_VERSION }} + + - uses: Swatinem/rust-cache@v1 + with: + cache-on-failure: true + + - name: Package + run: | + if [[ "${{ matrix.job.os }}" == "macos-latest" ]]; then + tar -czvf torii-${{ matrix.job.target }}-${{ matrix.job.arch }}.tar.gz target/release/libtorii_c.dylib + elif [[ "${{ matrix.job.os }}" == "windows-latest" ]]; then + tar -czvf torii-${{ matrix.job.target }}-${{ matrix.job.arch }}.tar.gz target/release/libtorii_c.dll + else + tar -czvf torii-${{ matrix.job.target }}-${{ matrix.job.arch }}.tar.gz target/release/libtorii_c.so + fi + + - name: Create Release + id: create_release + uses: actions/create-release@v1 + with: + tag_name: ${{ github.ref }} + release_name: Release ${{ github.ref }} + draft: false + prerelease: false + + - name: Upload Release Asset + uses: actions/upload-release-asset@v1 + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./torii-${{ matrix.job.target }}-${{ matrix.job.arch }}.tar.gz + asset_name: torii-${{ matrix.job.target }}-${{ matrix.job.arch }}.tar.gz + asset_content_type: application/gzip diff --git a/Cargo.lock b/Cargo.lock index c514571..ba263ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4043,6 +4043,20 @@ dependencies = [ "web-sys", ] +[[package]] +name = "torii-c" +version = "0.3.1" +dependencies = [ + "cbindgen", + "dojo-types", + "parking_lot 0.12.1", + "starknet 0.6.0", + "starknet-crypto", + "tokio", + "torii-client", + "torii-grpc", +] + [[package]] name = "torii-client" version = "0.3.3" @@ -4104,20 +4118,6 @@ dependencies = [ "url", ] -[[package]] -name = "torii-unity" -version = "0.3.1" -dependencies = [ - "cbindgen", - "dojo-types", - "parking_lot 0.12.1", - "starknet 0.6.0", - "starknet-crypto", - "tokio", - "torii-client", - "torii-grpc", -] - [[package]] name = "tower" version = "0.4.13" diff --git a/Cargo.toml b/Cargo.toml index 2587309..7c5527f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] edition = "2021" -name = "torii-unity" +name = "torii-c" version = "0.3.1" [lib] diff --git a/README.md b/README.md index 2756589..8d3d904 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,5 @@ This package provides C bindings for the Torii Client SDK. The approach is to ge ``` cargo build --release -gcc example/main.c -L target/release -l torii_unity -I .. +gcc example/main.c -L target/release -l torii_c -I .. ``` diff --git a/build.rs b/build.rs index 9f5dc16..6922202 100644 --- a/build.rs +++ b/build.rs @@ -1,12 +1,12 @@ -extern crate cbindgen; - use std::env; fn main() { let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); - let mut config: cbindgen::Config = Default::default(); - config.language = cbindgen::Language::C; + let config = cbindgen::Config { + language: cbindgen::Language::C, + ..Default::default() + }; cbindgen::Builder::new() .with_config(config) diff --git a/scripts/rust_fmt.sh b/scripts/rust_fmt.sh new file mode 100755 index 0000000..62a4186 --- /dev/null +++ b/scripts/rust_fmt.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +cargo +nightly fmt --check --all -- "$@" diff --git a/src/lib.rs b/src/lib.rs index 96235e6..bb1e6ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,9 +15,9 @@ pub struct FieldElement { data: [u8; 32], } -impl Into for &FieldElement { - fn into(self) -> starknet::core::types::FieldElement { - starknet::core::types::FieldElement::from_bytes_be(&self.data).unwrap() +impl From<&FieldElement> for starknet::core::types::FieldElement { + fn from(val: &FieldElement) -> Self { + starknet::core::types::FieldElement::from_bytes_be(&val.data).unwrap() } } @@ -29,10 +29,10 @@ pub struct EntityModel { pub keys_len: usize, } -impl Into for &EntityModel { - fn into(self) -> dojo_types::schema::EntityModel { - let model = unsafe { CStr::from_ptr(self.model).to_string_lossy().into_owned() }; - let keys = unsafe { std::slice::from_raw_parts(self.keys, self.keys_len).to_vec() }; +impl From<&EntityModel> for dojo_types::schema::EntityModel { + fn from(val: &EntityModel) -> Self { + let model = unsafe { CStr::from_ptr(val.model).to_string_lossy().into_owned() }; + let keys = unsafe { std::slice::from_raw_parts(val.keys, val.keys_len).to_vec() }; dojo_types::schema::EntityModel { model, @@ -42,7 +42,8 @@ impl Into for &EntityModel { } #[no_mangle] -pub extern "C" fn client_new( +#[allow(clippy::missing_safety_doc)] +pub unsafe extern "C" fn client_new( torii_url: *const c_char, rpc_url: *const c_char, world: &FieldElement, @@ -79,7 +80,8 @@ pub extern "C" fn client_new( } #[no_mangle] -pub extern "C" fn client_add_entities_to_sync( +#[allow(clippy::missing_safety_doc)] +pub unsafe extern "C" fn client_add_entities_to_sync( client: *mut ToriiClient, entities: *const EntityModel, entities_len: usize, @@ -107,7 +109,8 @@ pub extern "C" fn client_add_entities_to_sync( } #[no_mangle] -pub extern "C" fn client_remove_entities_to_sync( +#[allow(clippy::missing_safety_doc)] +pub unsafe extern "C" fn client_remove_entities_to_sync( client: *mut ToriiClient, entities: *const EntityModel, entities_len: usize, @@ -139,7 +142,8 @@ pub extern "C" fn client_remove_entities_to_sync( // back into a Box, which gets dropped at the end of the scope, // deallocating the memory. #[no_mangle] -pub extern "C" fn client_free(client: *mut ToriiClient) { +#[allow(clippy::missing_safety_doc)] +pub unsafe extern "C" fn client_free(client: *mut ToriiClient) { if !client.is_null() { unsafe { let _ = Box::from_raw(client);