Skip to content

Commit cb2e70e

Browse files
committed
Project structure update
1 parent 2baf376 commit cb2e70e

File tree

10 files changed

+69
-122
lines changed

10 files changed

+69
-122
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
src/cxxbridge_code/target
2-
build
3-
1+
examples/src/cxxbridge_code/target
2+
examples/build

README.MD

Lines changed: 28 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,39 @@
1-
# Example of using [CXX](https://cxx.rs/) and [Corrosion](https://github.com/corrosion-rs/corrosion) in CMake project to call Rust functions from C++
1+
# CMake file for using [CXX](https://cxx.rs/) and [Corrosion](https://github.com/corrosion-rs/corrosion) in CMake project to call Rust functions from C++
22

3-
This is a fork of the [rusty_cmake](https://github.com/trondhe/rusty_cmake) repository to use CXX and Corrosion in a CMake project to use CXX and Corrosion in a CMake project with the following modifications:
3+
This is a fork of the [rusty_cmake](https://github.com/trondhe/rusty_cmake) repository to use CXX and Corrosion in a CMake project with the following modifications:
44

55
### `cxx_corrosion.cmake` file:
6-
### `cxx_corrosion.cmake` file:
7-
- Windows-related code is removed (Linux and MacOS are supported) (Linux and MacOS are supported).
8-
- Windows-related code is removed (Linux and MacOS are supported) (Linux and MacOS are supported).
9-
- Simplify by using a simple one CMakeLists file for the project.
10-
11-
### Code examples to call Rust functions from C++:
12-
- [Example of using a different primitive types](https://github.com/geekbrother/cxx-corrosion-cmake/blob/main/src/main.cpp) for arguments and returns.
13-
- Example of using [Rust Result type](https://cxx.rs/binding/result.html#returning-result-from-rust-to-c) in return and [Anyhow](https://docs.rs/anyhow/latest/anyhow/).
14-
- [Example of using panics](https://github.com/geekbrother/cxx-corrosion-cmake/blob/2a981b2ec34ee4d4ffe261b1be07691f74c31a04/src/main.cpp#L15) in Rust + C++.
15-
### Code examples to call Rust functions from C++:
16-
- [Example of using a different primitive types](https://github.com/geekbrother/cxx-corrosion-cmake/blob/main/src/main.cpp) for arguments and returns.
17-
- Example of using [Rust Result type](https://cxx.rs/binding/result.html#returning-result-from-rust-to-c) in return and [Anyhow](https://docs.rs/anyhow/latest/anyhow/).
18-
- [Example of using panics](https://github.com/geekbrother/cxx-corrosion-cmake/blob/2a981b2ec34ee4d4ffe261b1be07691f74c31a04/src/main.cpp#L15) in Rust + C++.
6+
- Function to get STEM is removed in a favor of one-line solution;
7+
- Simplify by using a simple one CMakeLists file for the project;
8+
- Windows-related code is removed (Linux and MacOS are supported);
9+
- Minor code changes to simplify and use a modern C++.
1910

2011
# Why?
2112

22-
This repo can be used as a sandbox playground for testing simple ideas of the Rust library integration in the current C++ CMake project and as examples of the usage.
23-
24-
# Usage
25-
26-
## Dependencies
27-
- Linux or MacOS
28-
- CMake
29-
- Clang
30-
## CMake
31-
32-
Run CMake and build commands in a project's root:
33-
34-
`cmake -B build . && make -C build -j4`.
13+
This `cmake/corrosion_cxx.cmake` can be imported in any CMake project to simplify Rust code usage in C++.
3514

36-
Then you can run the example app by calling:
15+
Current solution is a battle-tested and `cmake/corrosion_cxx.cmake` file is used in a [Comm](https://comm.app) application development.
3716

38-
`build/cxx_cmake`.
17+
# Examples
3918

40-
## Nix
19+
Example code is located in `examples` folder.
4120

42-
If you are using [Nix](https://nixos.org/download.html) as a development environment you can use a nix shell with all dependencies:
43-
44-
```
45-
nix-shell -p cmake -p clang -p rustup -p libiconv -p git --pure
46-
```
21+
This repo example can be used as a sandbox playground for testing simple ideas of the Rust library integration in the current C++ CMake project and as examples of the usage.
4722

48-
And then run build commands from CMake above.
49-
50-
## Docker
51-
52-
There is a Docker file to build and run the example app in Docker container.
53-
To build it run the build command from the project's root directory:
54-
55-
```
56-
docker build . -t cxx-corrosion-cmake
57-
```
58-
59-
Then you can run the example app by calling:
23+
### Code examples to call Rust functions from C++:
24+
- [Example of using a different primitive types](https://github.com/geekbrother/cxx-corrosion-cmake/blob/main/src/main.cpp) for arguments and returns;
25+
- Example of using [Rust Result type](https://cxx.rs/binding/result.html#returning-result-from-rust-to-c) and [Anyhow](https://docs.rs/anyhow/latest/anyhow/) in return to C++;
26+
- [Example of using panics](https://github.com/geekbrother/cxx-corrosion-cmake/blob/2a981b2ec34ee4d4ffe261b1be07691f74c31a04/src/main.cpp#L15) in Rust when calling from C++.
6027

61-
```
62-
docker run cxx-corrosion-cmake
63-
```
28+
## Dependencies
29+
- Linux or MacOS
30+
- CMake
31+
- Clang
6432
- [Corrosion](https://github.com/corrosion-rs/corrosion#installation)
65-
# Todo
33+
- Rustup
6634
## CMake
67-
- [ ] Make it vcpkg package
68-
- [ ] Make it nix package
69-
Run CMake and build commands in a project's root:
35+
36+
Run CMake and build commands in the `examples` folder:
7037

7138
`cmake -B build . && make -C build -j4`.
7239

@@ -82,12 +49,12 @@ If you are using [Nix](https://nixos.org/download.html) as a development environ
8249
nix-shell -p cmake -p clang -p rustup -p libiconv -p git --pure
8350
```
8451

85-
And then run build commands from CMake above.
52+
And then run build commands from CMake section above.
8653

8754
## Docker
8855

8956
There is a Docker file to build and run the example app in Docker container.
90-
To build it run the build command from the project's root directory:
57+
To build it run the build command from the `examples` folder:
9158

9259
```
9360
docker build . -t cxx-corrosion-cmake
@@ -101,5 +68,7 @@ docker run cxx-corrosion-cmake
10168

10269
# Todo
10370

104-
- [ ] Make it vcpkg package
105-
- [ ] Make it nix package
71+
Create a packages in package managers to help tracking updates in the project instead of manually track the changes.
72+
73+
- Make it nix package
74+
- Make it vcpkg package

CMakeLists.txt renamed to examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ project(cxx_cmake CXX)
77
set(CMAKE_CXX_STANDARD 17)
88

99
find_package(Corrosion REQUIRED)
10-
include(cmake/corrosion_cxx.cmake)
10+
include(../cmake/corrosion_cxx.cmake)
1111

1212
add_executable(${PROJECT_NAME})
1313

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/src/main.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "cxxbridge_code/src/lib.rs.h"
2+
#include <iostream>
3+
#include <string>
4+
#include "rust/cxx.h"
5+
6+
int main()
7+
{
8+
// Primitive types:
9+
std::cout << "[1] A value given via generateb cxxbridge for lib_cxxbridge_bool: "
10+
<< lib_cxxbridge_bool(true) << std::endl;
11+
std::cout << "[2] A value given via generated cxxbridge for lib_cxxbridge_integer: "
12+
<< lib_cxxbridge_integer(86) << std::endl;
13+
const rust::String testString = "Max";
14+
std::cout << "[3] A value given via generated cxxbridge for lib_cxxbridge_string: "
15+
<< lib_cxxbridge_string(testString) << std::endl;
16+
17+
// Return Rust `Result` with Ok:
18+
std::cout << "[4] A Rust `Result Ok` value given via generated cxxbridge for lib_cxxbridge_return_result_ok:" << std::endl
19+
<< lib_cxxbridge_return_result_ok() << std::endl;
20+
21+
// Return Rust `Result` with Error:
22+
try
23+
{
24+
std::cout << "[5] A Rust `Result Error` value given via generated cxxbridge for lib_cxxbridge_return_result_error:" << std::endl
25+
<< lib_cxxbridge_return_result_error() << std::endl;
26+
}
27+
catch (rust::Error e)
28+
{
29+
std::cerr << "Got an error from Rust function `Result`:" << std::endl;
30+
std::cerr << e.what() << std::endl;
31+
}
32+
33+
// Panic in a function call:
34+
std::cout << "[6] Testing a case of panic by running lib_cxxbridge_panicked_function:" << std::endl;
35+
std::cout << lib_cxxbridge_panicked_function();
36+
37+
return 0;
38+
}

src/main.cpp

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)