This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from MGTheTrain/feature/implement-rust-binding-…
…for-core-module [Feature] - Implement rust binding for core module
- Loading branch information
Showing
14 changed files
with
352 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,6 @@ secrets.cfg | |
*.so | ||
__pycache__ | ||
obj | ||
bin | ||
bin | ||
Cargo.lock | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// 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. | ||
|
||
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// 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. | ||
|
||
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; | ||
} | ||
} | ||
|
||
// missing step: separate lib from app | ||
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); | ||
} |
Oops, something went wrong.