Files
rustytorch/crates/core/rtx-bindings/src/lib.rs
T
osobhandClaude Sonnet 5 4aaa36a57a style: cargo fmt --workspace (whitespace/wrapping only, no semantic change)
Whole-workspace rustfmt pass picked up while iterating on Mamba GPU
backward work. Verified formatting-only via diff sampling; no logic
changed.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-08-10 07:09:36 -07:00

101 lines
2.7 KiB
Rust

// Crate-level lint overrides (workspace lints enabled in Cargo.toml)
#![allow(unsafe_code)]
//! RustyTorch++ Language Bindings and SDK
//!
//! This crate provides language bindings for RustyTorch++, enabling usage from:
//! - Python (via PyO3)
//! - C/C++ (via C API)
//! - Java/Node.js (via C API + JNI/N-API)
//! - WebAssembly (via wasm-bindgen)
//!
//! The design emphasizes:
//! - Zero-copy operations where possible
//! - Type safety across language boundaries
//! - Async operation support
//! - Comprehensive error handling
//! - Production-ready performance
#![allow(unexpected_cfgs)]
use thiserror::Error;
#[cfg(feature = "python")]
pub mod python;
#[cfg(feature = "c-api")]
pub mod c_api;
#[cfg(feature = "onnx")]
pub mod onnx;
#[cfg(feature = "dlpack")]
pub mod dlpack;
/// Error types for language bindings
#[derive(Error, Debug)]
pub enum BindingError {
#[error("Tensor shape mismatch: expected {expected:?}, got {actual:?}")]
ShapeError {
expected: Vec<usize>,
actual: Vec<usize>,
},
#[error("Device error: {message}")]
DeviceError { message: String },
#[error("Out of memory: {message}")]
OutOfMemoryError { message: String },
#[error("Type conversion error: {message}")]
ConversionError { message: String },
#[error("Runtime error: {message}")]
RuntimeError { message: String },
#[error("Tensor error: {0}")]
TensorError(#[from] rtx_tensor::TensorError),
#[error("Autograd error: {message}")]
AutogradError { message: String },
// #[error("Inference error: {0}")]
// InferenceError(#[from] rtx_inference::error::InferenceError),
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
#[error("Serialization error: {0}")]
#[cfg(feature = "onnx")]
SerializationError(#[from] serde_json::Error),
}
pub type Result<T> = std::result::Result<T, BindingError>;
// Re-export core types for convenience
pub use rtx_autograd::AutogradContext;
pub use rtx_tensor::{DType, Device, Shape, Tensor};
// Python bindings temporarily disabled due to version conflicts
// #[cfg(feature = "python")]
// use pyo3::prelude::*;
// #[cfg(feature = "python")]
// use pyo3::exceptions::PyRuntimeError;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_conversion() {
// Test that our error types convert properly
let tensor_err = rtx_tensor::TensorError::Shape {
message: "Shape mismatch: expected [2, 3], got [3, 2]".to_string(),
};
let binding_err = BindingError::from(tensor_err);
match binding_err {
BindingError::TensorError(_) => (),
_ => panic!("Expected TensorError conversion"),
}
}
}