Expand description
C-compatible FFI bindings for qtty physical quantities and unit conversions.
qtty-ffi provides a stable C ABI for qtty, enabling interoperability with C/C++ code
and other languages with C FFI support. It also provides helper types and macros for
downstream Rust crates that need to expose their own FFI APIs using qtty quantities.
§Features
- ABI-stable types:
#[repr(C)]and#[repr(u32)]types safe for FFI - Unit registry: Mapping between FFI unit IDs and conversion factors
- C API:
extern "C"functions for quantity construction and conversion - Rust helpers: Macros and trait implementations for downstream integration
§Quick Start (C/C++)
Include the generated header and link against the library:
#include "qtty_ffi.h"
// Create a quantity
QttyQuantity meters;
qtty_quantity_make(1000.0, UnitId_Meter, &meters);
// Convert to kilometers
QttyQuantity kilometers;
int32_t status = qtty_quantity_convert(meters, UnitId_Kilometer, &kilometers);
if (status == QTTY_OK) {
// kilometers.value == 1.0
}§Quick Start (Rust)
Use the helper traits and macros for seamless conversion:
use qtty::length::{Meters, Kilometers};
use qtty_ffi::{QttyQuantity, UnitId};
// Convert Rust type to FFI
let meters = Meters::new(1000.0);
let ffi_qty: QttyQuantity = meters.into();
// Convert FFI back to Rust type (with automatic unit conversion)
let km: Kilometers = ffi_qty.try_into().unwrap();
assert!((km.value() - 1.0).abs() < 1e-12);§ABI Stability
The following are part of the ABI contract and will never change:
UnitIddiscriminant values (existing variants)DimensionIddiscriminant values (existing variants)QttyQuantitymemory layout- Status code values (
QTTY_OK,QTTY_ERR_UNKNOWN_UNIT, etc.) - Function signatures of exported
extern "C"functions
New variants may be added to enums (with new discriminant values), and new functions may be added, but existing items will remain stable.
§Supported Units (v1)
§Length
UnitId::Meter- SI base unitUnitId::Kilometer- 1000 meters
§Time
UnitId::Second- SI base unitUnitId::Minute- 60 secondsUnitId::Hour- 3600 secondsUnitId::Day- 86400 seconds
§Angle
UnitId::Radian- SI unitUnitId::Degree- π/180 radians
§Error Handling
All FFI functions return status codes:
QTTY_OK(0): SuccessQTTY_ERR_UNKNOWN_UNIT(-1): Invalid unit IDQTTY_ERR_INCOMPATIBLE_DIM(-2): Dimension mismatchQTTY_ERR_NULL_OUT(-3): Null output pointerQTTY_ERR_INVALID_VALUE(-4): Invalid value (reserved)
§Thread Safety
All functions are thread-safe. The library contains no global mutable state.
Re-exports§
pub use helpers::days_into_ffi;pub use helpers::degrees_into_ffi;pub use helpers::hours_into_ffi;pub use helpers::kilometers_into_ffi;pub use helpers::meters_into_ffi;pub use helpers::minutes_into_ffi;pub use helpers::radians_into_ffi;pub use helpers::seconds_into_ffi;pub use helpers::try_into_days;pub use helpers::try_into_degrees;pub use helpers::try_into_hours;pub use helpers::try_into_kilometers;pub use helpers::try_into_meters;pub use helpers::try_into_minutes;pub use helpers::try_into_radians;pub use helpers::try_into_seconds;
Modules§
- helpers
- Helper functions and trait implementations for downstream crate integration.
- macros
- Macros for implementing FFI conversions for qtty unit types.
- registry
- Unit registry and conversion logic for FFI.
Macros§
- impl_
unit_ ffi - Implements
From<$qty_type>forQttyQuantityandTryFrom<QttyQuantity>for$qty_type.
Structs§
- Qtty
Derived Quantity - A derived quantity representing a compound unit (numerator/denominator).
- Qtty
Quantity - A POD quantity carrier type suitable for FFI.
Enums§
- Dimension
Id - Dimension identifier for FFI.
- UnitId
- Unit identifier for FFI.
Constants§
- QTTY_
ERR_ INCOMPATIBLE_ DIM - Error: conversion requested between incompatible dimensions.
- QTTY_
ERR_ INVALID_ VALUE - Error: the provided value is invalid (reserved for future use).
- QTTY_
ERR_ NULL_ OUT - Error: a required output pointer was null.
- QTTY_
ERR_ UNKNOWN_ UNIT - Error: the provided unit ID is not recognized/valid.
- QTTY_OK
- Success status code.
Functions§
- qtty_
ffi_ version - Returns the FFI ABI version.
- qtty_
quantity_ ⚠convert - Converts a quantity to a different unit.
- qtty_
quantity_ ⚠convert_ value - Converts a value from one unit to another.
- qtty_
quantity_ ⚠make - Creates a new quantity with the given value and unit.
- qtty_
unit_ ⚠dimension - Gets the dimension of a unit.
- qtty_
unit_ is_ valid - Checks if a unit ID is valid (recognized by the registry).
- qtty_
unit_ name - Gets the name of a unit as a NUL-terminated C string.
- qtty_
units_ ⚠compatible - Checks if two units are compatible (same dimension).