-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6f41384
Showing
8 changed files
with
223 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Xmake cache | ||
.xmake/ | ||
build/ | ||
back/target/ | ||
**/Cargo.lock | ||
.cache/ | ||
compile_commands.json | ||
|
||
# MacOS Cache | ||
.DS_Store | ||
|
||
|
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,3 @@ | ||
[build] | ||
rustflags = ["-Clinker-plugin-lto", "-Clinker=clang" ,"-Clink-arg=-flto","-Clink-arg=-fuse-ld=mold"] | ||
|
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,13 @@ | ||
[package] | ||
name = "back" | ||
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" | ||
[lib] | ||
crate-type = ["staticlib"] |
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,4 @@ | ||
fn main() { | ||
let _ = cxx_build::bridge("src/lib.rs"); | ||
println!("cargo:rerun-if-changed=src/lib.rs"); | ||
} |
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,100 @@ | ||
use std::fmt; | ||
|
||
#[cxx::bridge] | ||
mod ffi { | ||
#[namespace = "shared"] | ||
struct Color { | ||
r: u8, | ||
g: u8, | ||
b: u8, | ||
} | ||
|
||
#[namespace = "shared"] | ||
struct SharedThing { | ||
points: Box<Points>, | ||
persons: UniquePtr<Person>, | ||
pixels: Vec<Color>, | ||
} | ||
|
||
unsafe extern "C++" { | ||
include!("cpp_part.h"); | ||
type Person; | ||
|
||
fn get_name(person: &Person) -> &CxxString; | ||
fn make_person() -> UniquePtr<Person>; | ||
fn is_black(self: &Color) -> bool; | ||
} | ||
|
||
#[namespace = "rust_part"] | ||
extern "Rust" { | ||
type Points; | ||
fn print_shared_thing(points: &SharedThing); | ||
fn make_shared_thing() -> SharedThing; | ||
fn rust_echo(val: i32) -> i32; | ||
} | ||
|
||
#[namespace = "shared"] | ||
extern "Rust" { | ||
fn is_white(self: &Color) -> bool; | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Points { | ||
x: Vec<u8>, | ||
y: Vec<u8>, | ||
} | ||
|
||
impl ffi::Color { | ||
pub fn white() -> Self { | ||
Self { | ||
r: 255, | ||
g: 255, | ||
b: 255, | ||
} | ||
} | ||
|
||
pub fn black() -> Self { | ||
Self { r: 0, g: 0, b: 0 } | ||
} | ||
|
||
pub fn is_white(&self) -> bool { | ||
self.r == 255 && self.g == 255 && self.b == 255 | ||
} | ||
} | ||
|
||
impl fmt::Debug for ffi::Color { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("Color") | ||
.field("r", &self.r) | ||
.field("g", &self.g) | ||
.field("b", &self.b) | ||
.finish() | ||
} | ||
} | ||
|
||
fn print_shared_thing(thing: &ffi::SharedThing) { | ||
println!("{:#?}", thing.points); | ||
println!( | ||
"Pixel 0 is white: {}, pixel is black: {}", | ||
thing.pixels[0].is_white(), | ||
thing.pixels[1].is_black() | ||
); | ||
println!("{:#?}", ffi::get_name(thing.persons.as_ref().unwrap())); | ||
} | ||
|
||
fn make_shared_thing() -> ffi::SharedThing { | ||
ffi::SharedThing { | ||
points: Box::new(Points { | ||
x: vec![1, 2, 3], | ||
y: vec![4, 5, 6], | ||
}), | ||
persons: ffi::make_person(), | ||
pixels: vec![ffi::Color::white(), ffi::Color::black()], | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
fn rust_echo(val: i32) -> i32 { | ||
val | ||
} |
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,16 @@ | ||
#pragma once | ||
#include <iostream> | ||
#include <memory> | ||
#include <string> | ||
|
||
struct Person { | ||
std::string name; | ||
|
||
Person() { this->name = "cpp expert!"; } | ||
|
||
void print_name() { std::cout << this->name << std::endl; } | ||
}; | ||
|
||
const std::string &get_name(const Person &person); | ||
|
||
std::unique_ptr<Person> make_person(); |
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,58 @@ | ||
#include "lib.rs.h" | ||
#include <chrono> | ||
#include <iostream> | ||
|
||
const std::string &get_name(const Person &person) { return person.name; } | ||
|
||
std::unique_ptr<Person> make_person() { return std::make_unique<Person>(); } | ||
|
||
bool shared::Color::is_black() const noexcept { | ||
return this->r == 0 && this->g == 0 && this->b == 0; | ||
} | ||
|
||
int cpp_echo(int val) { return val; } | ||
|
||
int test_fun() { | ||
int sum = 0; | ||
for (int i = 0; i < 1000000; i += 1) { | ||
sum += rust_part::rust_echo(i); | ||
} | ||
return sum; | ||
} | ||
|
||
int test_inline() { | ||
int sum = 0; | ||
for (int i = 0; i < 1000000; i += 1) { | ||
sum += cpp_echo(i); | ||
} | ||
return sum; | ||
} | ||
|
||
void test_lto() { | ||
auto t1 = std::chrono::high_resolution_clock::now(); | ||
auto sum = test_fun(); | ||
auto t2 = std::chrono::high_resolution_clock::now(); | ||
|
||
auto duration = | ||
std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count(); | ||
|
||
std::cout << "Calling rust function" | ||
<< ", time elapsed: " << duration << " ns." << std::endl; | ||
|
||
t1 = std::chrono::high_resolution_clock::now(); | ||
sum = test_inline(); | ||
t2 = std::chrono::high_resolution_clock::now(); | ||
duration = | ||
std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count(); | ||
|
||
std::cout << "Calling c++ function" | ||
<< ", time elapsed: " << duration << " ns." << std::endl; | ||
} | ||
|
||
int main() { | ||
auto thing = rust_part::make_shared_thing(); | ||
rust_part::print_shared_thing(thing); | ||
|
||
test_lto(); | ||
return 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,17 @@ | ||
add_rules("mode.debug", "mode.release") | ||
|
||
target("cm") | ||
set_toolchains("clang-13") | ||
set_languages("cxx17") | ||
set_kind("binary") | ||
add_files("src/*.cpp") | ||
add_includedirs("back/target/cxxbridge/back/src/") | ||
add_includedirs("include/") | ||
add_files("back/target/cxxbridge/back/src/lib.rs.cc") | ||
add_links("back") | ||
add_links("stdc++") | ||
-- add_links("pthread") | ||
-- add_links("dl") | ||
add_linkdirs("back/target/release/") | ||
add_ldflags("-fuse-ld=mold") | ||
add_ldflags("-flto") |