mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 months ago
8 changed files with 89 additions and 33 deletions
@ -0,0 +1,15 @@ |
|||
#include "rust_ffi/include/common_code.h" |
|||
|
|||
#include <common_code/common_code.h> |
|||
|
|||
using klotski::ffi::RsCommonCode; |
|||
|
|||
rust::String RsCommonCode::to_string() const noexcept { |
|||
return codec::CommonCode::unsafe_create(code).to_string(); |
|||
} |
|||
|
|||
// TODO: it seems that cxx.rs not support `std::optional`
|
|||
RsCommonCode klotski::ffi::from_string(rust::Str s) { |
|||
std::string_view sv {s.data(), s.length()}; |
|||
return {codec::CommonCode::from_string(sv)->unwrap()}; // TODO: value check
|
|||
} |
@ -1,8 +1,33 @@ |
|||
use cxx_build::CFG; |
|||
|
|||
// NOTE: add `CC=clang-20 CXX=clang++-20 CXXFLAGS="-stdlib=libc++"` for cargo command
|
|||
// we should keep cxx crate using clang for `cxx.cc`
|
|||
|
|||
// NOTE: also, `RUSTFLAGS="-C linker=clang-20"` should be add to cargo env for using lld
|
|||
|
|||
fn main() { |
|||
CFG.include_prefix = "rust_ffi"; |
|||
|
|||
// std::env::set_var("CC", "clang-20");
|
|||
// std::env::set_var("CXX", "clang++-20");
|
|||
|
|||
cxx_build::bridge("src/common_code.rs") |
|||
.file("src/demo.cc") |
|||
.file("adapter/common_code.cc") |
|||
.flag("-std=c++23") |
|||
.flag("-fno-rtti") |
|||
.flag("-fno-exceptions") |
|||
// .flag("-stdlib=libc++")
|
|||
.include("../../core") |
|||
// .cpp_set_stdlib("c++")
|
|||
// .cpp_link_stdlib("c++")
|
|||
.compile("klotski"); |
|||
|
|||
println!("cargo:rerun-if-changed=src/demo.cc"); |
|||
println!("cargo:rustc-link-search=native=../../../cmake-build-release/src/core"); |
|||
println!("cargo:rustc-link-lib=static=klotski_core"); |
|||
|
|||
println!("cargo:rustc-link-arg=-fuse-ld=lld-20"); |
|||
println!("cargo:rustc-link-arg=-stdlib=libc++"); |
|||
|
|||
println!("cargo:rerun-if-changed=adapter/common_code.cc"); |
|||
println!("cargo:rerun-if-changed=src/common_code.rs"); |
|||
} |
|||
|
@ -0,0 +1,9 @@ |
|||
#pragma once |
|||
|
|||
#include "rust_ffi/src/common_code.rs.h" |
|||
|
|||
namespace klotski::ffi { |
|||
|
|||
RsCommonCode from_string(rust::Str s); |
|||
|
|||
} // namespace klotski::ffi
|
@ -1,18 +1,37 @@ |
|||
#[cxx::bridge] |
|||
pub mod ffi { |
|||
#[cxx::bridge(namespace = "klotski::ffi")] |
|||
mod ffi { |
|||
#[derive(Debug)] |
|||
pub struct CommonCode { |
|||
struct RsCommonCode { |
|||
code: u64 |
|||
} |
|||
|
|||
extern "Rust" {} |
|||
|
|||
unsafe extern "C++" { |
|||
include!("klotski/src/klotski.h"); |
|||
include!("rust_ffi/include/common_code.h"); |
|||
|
|||
fn to_string(self: &RsCommonCode) -> String; |
|||
|
|||
fn from_string(s: &str) -> RsCommonCode; |
|||
} |
|||
} |
|||
|
|||
pub use ffi::RsCommonCode as CommonCode; |
|||
|
|||
// fn unwrap(self: &CommonCode) -> u64;
|
|||
impl CommonCode { |
|||
pub fn unsafe_create(code: u64) -> CommonCode { |
|||
CommonCode { code } |
|||
} |
|||
|
|||
pub fn unwrap(self: &CommonCode) -> u64 { |
|||
self.code |
|||
} |
|||
|
|||
pub fn from_string(s: &str) -> CommonCode { |
|||
ffi::from_string(s) |
|||
} |
|||
} |
|||
|
|||
// fn from_string(s: &str) -> CommonCode;
|
|||
fn from_string() -> CommonCode; |
|||
impl PartialEq for CommonCode { |
|||
fn eq(&self, other: &Self) -> bool { |
|||
self.code == other.code |
|||
} |
|||
} |
|||
|
@ -1,8 +0,0 @@ |
|||
#include "klotski/src/klotski.h" |
|||
#include "klotski/src/common_code.rs.h" |
|||
|
|||
#include <iostream> |
|||
|
|||
CommonCode from_string() { |
|||
return CommonCode {}; |
|||
} |
@ -1,5 +0,0 @@ |
|||
#pragma once |
|||
|
|||
#include "klotski/src/common_code.rs.h" |
|||
|
|||
CommonCode from_string(); |
@ -1,12 +1,11 @@ |
|||
mod common_code; |
|||
|
|||
use common_code::ffi::CommonCode; |
|||
use common_code::CommonCode; |
|||
|
|||
fn main() { |
|||
// let code = CommonCode { code: 123 };
|
|||
// let val = code.unwrap();
|
|||
// println!("val = {}", val);
|
|||
let code = CommonCode::from_string("1A9BF0C"); |
|||
println!("code: {:?}", code.unwrap()); |
|||
println!("str: {}", code.to_string()); |
|||
|
|||
let code = common_code::ffi::from_string(); |
|||
println!("code = {:?}", code); |
|||
println!("{}", code == CommonCode::unsafe_create(0x1A9BF0C00)); |
|||
} |
|||
|
Loading…
Reference in new issue