Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
add rust cxx sample
Browse files Browse the repository at this point in the history
  • Loading branch information
MGTheTrain committed Apr 16, 2024
1 parent 1af11c4 commit 8131235
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 0 deletions.
Empty file removed bindings/rust/.gitignore
Empty file.
12 changes: 12 additions & 0 deletions bindings/rust/cxx-sample/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "cxx-sample"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cxx = "1.0"

[build-dependencies]
cxx-build = "1.0"
10 changes: 10 additions & 0 deletions bindings/rust/cxx-sample/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() {
cxx_build::bridge("src/main.rs")
.file("src/math-utils.cc")
.std("c++14")
.compile("cxxbridge-core");

println!("cargo:rerun-if-changed=src/main.rs");
println!("cargo:rerun-if-changed=src/math-utils.cc");
println!("cargo:rerun-if-changed=include/math-utils.h");
}
80 changes: 80 additions & 0 deletions bindings/rust/cxx-sample/include/math-utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// The MIT License
//
// Copyright (c) 2024 MGTheTrain
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include <stdint.h>

#ifdef __cplusplus
#define M_PI 3.141
extern "C" {
#endif

/**
* @brief Adds two integers.
* @param a The first integer.
* @param b The second integer.
* @return The sum of a and b.
*/
int32_t add(int32_t a, int32_t b);

/**
* @brief Subtracts one integer from another.
* @param a The integer to subtract from.
* @param b The integer to subtract.
* @return The result of subtracting b from a.
*/
int32_t subtract(int32_t a, int32_t b);

/**
* @brief Multiplies two integers.
* @param a The first integer.
* @param b The second integer.
* @return The product of a and b.
*/
int32_t multiply(int32_t a, int32_t b);

/**
* @brief Divides one float by another.
* @param a The float to divide.
* @param b The divisor.
* @return The result of dividing a by b.
* @throw Division by zero error if b is zero.
*/
float divide(float a, float b);

/**
* @brief Calculates the area of the circle.
* @param radius The radius of the circle.
* @return The area of the circle.
*/
float getCircleArea(float radius);

/**
* @brief Calculates the circumference of the circle.
* @param radius The radius of the circle
* @return The circumference of the circle.
*/
float getCircleCircumference(float radius);
#ifdef __cplusplus
}
#endif
48 changes: 48 additions & 0 deletions bindings/rust/cxx-sample/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use cxx::ExternType;

#[cxx::bridge]
mod ffi {
unsafe extern "C++" {
include!("cxx-sample/include/math-utils.h");
fn add(a: i32, b: i32) -> i32;
fn subtract(a: i32, b: i32) -> i32;
fn multiply(a: i32, b: i32) -> i32;
fn divide(a: f32, b: f32) -> f32;

fn getCircleArea(radius: f32) -> f32;
fn getCircleCircumference(radius: f32) -> f32;
}
}

fn main() {
let a = 10;
let b = 5;
let result_add: i32;
let result_subtract: i32;
let result_multiply: i32;
let result_divide: f32;

unsafe {
result_add = ffi::add(a, b);
result_subtract = ffi::subtract(a, b);
result_multiply = ffi::multiply(a, b);
result_divide = ffi::divide(a as f32, b as f32);
}

println!("{} + {} = {}", a, b, result_add);
println!("{} - {} = {}", a, b, result_subtract);
println!("{} * {} = {}", a, b, result_multiply);
println!("{} / {} = {}", a, b, result_divide);

let radius = 5.0;
let circle_area: f32;
let circle_circumference: f32;

unsafe {
circle_area = ffi::getCircleArea(radius);
circle_circumference = ffi::getCircleCircumference(radius);
}

println!("Area of circle with radius {}: {}", radius, circle_area);
println!("Circumference of circle with radius {}: {}", radius, circle_circumference);
}
70 changes: 70 additions & 0 deletions bindings/rust/cxx-sample/src/math-utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// The MIT License
//
// Copyright (c) 2024 MGTheTrain
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "../include/math-utils.h"

/**
* @brief Adds two integers.
* @param a The first integer.
* @param b The second integer.
* @return The sum of a and b.
*/
int32_t add(int32_t a, int32_t b) { return a + b; }

/**
* @brief Subtracts one integer from another.
* @param a The integer to subtract from.
* @param b The integer to subtract.
* @return The result of subtracting b from a.
*/
int32_t subtract(int32_t a, int32_t b) { return a - b; }

/**
* @brief Multiplies two integers.
* @param a The first integer.
* @param b The second integer.
* @return The product of a and b.
*/
int32_t multiply(int32_t a, int32_t b) { return a * b; }

/**
* @brief Divides one float by another.
* @param a The float to divide.
* @param b The divisor.
* @return The result of dividing a by b.
* @throw Division by zero error if b is zero.
*/
float divide(float a, float b) { return a / b; }

/**
* @brief Calculates the area of the circle.
* @param radius The radius of the circle.
* @return The area of the circle.
*/
float getCircleArea(float radius) { return M_PI * radius * radius; }

/**
* @brief Calculates the circumference of the circle.
* @param radius The radius of the circle
* @return The circumference of the circle.
*/
float getCircleCircumference(float radius) { return 2 * M_PI * radius; }

0 comments on commit 8131235

Please sign in to comment.