Skip to content

Commit

Permalink
Merge branch 'release/ffi-0.23.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
turboladen committed May 30, 2024
2 parents b5c87d0 + 0c716bd commit 9ee11cb
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 32 deletions.
23 changes: 9 additions & 14 deletions .github/workflows/rust-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ jobs:
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: cargo test all
uses: actions-rs/cargo@v1
with:
command: test
args: --workspace --all-features
run: cargo test --workspace --all-features

check-ios:
name: Test (x86_64-apple-ios)
runs-on: [self-hosted, macOS, xcode13]
if: ${{ false }} # Disabling till PCC-221
env:
DYLD_ROOT_PATH: "/Applications/Xcode.app/Contents/Developer/Applications/Simulator.app"
DYLD_ROOT_PATH: >
"/Applications/Xcode.app/Contents/Developer/Applications/Simulator.app"
steps:
- uses: actions/checkout@v4
- name: Setup SSH
Expand All @@ -59,14 +57,11 @@ jobs:
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/toolchain@v1
- uses: dtolnay/rust-toolchain@stable
with:
profile: minimal
toolchain: stable
target: x86_64-apple-ios
override: true
targets: "x86_64-apple-ios"
- name: cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: --package wise_units-ffi --all-features --target x86_64-apple-ios
run: >
cargo test --package wise_units-ffi
--all-features
--target x86_64-apple-ios
2 changes: 1 addition & 1 deletion .github/workflows/security-audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ jobs:
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Security audit
uses: actions-rs/audit-check@v1
uses: rustsec/audit-check@v1.4.1
with:
token: ${{ secrets.GITHUB_TOKEN }}
4 changes: 2 additions & 2 deletions crates/ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wise_units-ffi"
version = "0.22.1"
version = "0.23.0"
description = "FFI wrapper for wise_units"
repository = "https://github.com/agrian-inc/wise_units"
license = "MIT"
Expand All @@ -10,7 +10,7 @@ publish = ["agrian-registry"]

[dependencies]
ffi_common.workspace = true
wise_units = { version = "0.22", registry = "agrian-registry" }
wise_units = { version = "0.23", registry = "agrian-registry" }

[build-dependencies]
cbindgen = "0.26.0"
Expand Down
7 changes: 4 additions & 3 deletions crates/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
// C bindings don't include the module name, so ffi functions will need it.
#![allow(clippy::module_name_repetitions)]

use ffi_common::core;
use std::{convert::TryInto, os::raw::c_char, ptr};
pub mod measurement;
pub mod unit;

use std::{convert::TryInto, os::raw::c_char, ptr};

use ffi_common::core;
pub use wise_units::{Measurement, Unit};

#[allow(clippy::needless_pass_by_value)]
fn set_error_and_return<T>(message: String) -> *const T {
core::error::set_last_err_msg(&message);
std::ptr::null()
ptr::null()
}

#[allow(clippy::needless_pass_by_value)]
Expand Down
13 changes: 7 additions & 6 deletions crates/ffi/src/measurement.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ffi_common::core::error;
use std::{ffi::CStr, os::raw::c_char, ptr};

use ffi_common::core::error;
use wise_units::{reduce::ToReduced, Convertible, Measurement, UcumUnit, Unit};

/// Create a new `Measurement`. Note that you must call
Expand Down Expand Up @@ -303,7 +304,7 @@ pub unsafe extern "C" fn measurement_div_scalar(
mod tests {
use super::*;
use approx::{assert_relative_eq, assert_ulps_eq};
use ffi_common::core;

use std::ffi::CString;

#[test]
Expand Down Expand Up @@ -400,7 +401,7 @@ mod tests {

#[test]
fn can_reduce() {
let expression = CString::new("[acr_us]/m2/har").expect("CString::new failed");
let expression = CString::new("har/m2/[acr_us]").expect("CString::new failed");
let value = 1.0;
let expected_expression = "[acr_us]";
let expected_value = 10_000.0;
Expand Down Expand Up @@ -515,7 +516,7 @@ mod tests {
let m = measurement_new(value, expression1.as_ptr());
let converted = measurement_convert_to(m, expression2.as_ptr());
assert_eq!(converted, ptr::null());
let error = CStr::from_ptr(core::error::get_last_err_msg());
let error = CStr::from_ptr(error::get_last_err_msg());
let error_str = error.to_str().expect("Failed to get str from CStr");
assert_eq!(error_str, expected_error);
}
Expand All @@ -532,11 +533,11 @@ mod tests {
// result is null, error is not null
let conversion_result_1 = measurement_convert_to(m, expression2.as_ptr());
assert_eq!(conversion_result_1, ptr::null());
assert_ne!(core::error::get_last_err_msg(), ptr::null());
assert_ne!(error::get_last_err_msg(), ptr::null());
// result is not null, error is null
let conversion_result_2 = measurement_convert_to(m, expression3.as_ptr());
assert_ne!(conversion_result_2, ptr::null());
assert_eq!(core::error::get_last_err_msg(), ptr::null());
assert_eq!(error::get_last_err_msg(), ptr::null());
}
}
}
14 changes: 8 additions & 6 deletions crates/ffi/src/unit.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::move_string_to_buffer;
use ffi_common::core::error;
use std::{
ffi::{CStr, CString},
os::raw::c_char,
ptr,
str::FromStr,
};

use ffi_common::core::error;
use wise_units::{
is_compatible_with::IsCompatibleWith, parser::Composable, reduce::ToReduced, UcumUnit, Unit,
is_compatible_with::IsCompatibleWith, reduce::ToReduced, Composable, UcumUnit, Unit,
};

use crate::move_string_to_buffer;

/// Create a new `Unit`. Note that you must call `unit_destroy(data: unit)` with
/// this instance when you are done with it so that the the unit can be properly
/// destroyed and its memory freed.
Expand Down Expand Up @@ -265,7 +267,7 @@ pub unsafe extern "C" fn unit_composition_buf(
mod tests {
use super::*;
use approx::{assert_relative_eq, assert_ulps_eq};
use ffi_common::core;

use std::ffi::CString;

#[test]
Expand All @@ -287,7 +289,7 @@ mod tests {
unsafe {
let u = unit_new(expression.as_ptr());
assert_eq!(u, ptr::null());
let error = CStr::from_ptr(core::error::get_last_err_msg())
let error = CStr::from_ptr(error::get_last_err_msg())
.to_str()
.expect("Failed to get str from CStr.");
assert_eq!(error, expected_error);
Expand Down Expand Up @@ -404,7 +406,7 @@ mod tests {
unsafe {
let unit = unit_new(expression.as_ptr());
let reduced = unit_reduced(unit);
assert_eq!((*reduced).expression().as_str(), "/m2.cm.km");
assert_eq!((*reduced).expression().as_str(), "/km3.cm");
}
}

Expand Down

0 comments on commit 9ee11cb

Please sign in to comment.