Browse Source

feat: rust ffi for klotski codec

legacy
Dnomd343 2 years ago
parent
commit
a7913cb0df
  1. 21
      src/cli/src/main.rs
  2. 12
      src/klotski_core/ffi/all_cases.cc
  3. 64
      src/klotski_core/ffi/codec.cc
  4. 24
      src/klotski_core/ffi/metadata.cc
  5. 113
      src/klotski_core/klotski.h
  6. 234
      src/rust_ffi/src/codec/ffi.rs
  7. 3
      src/rust_ffi/src/codec/mod.rs
  8. 2
      src/rust_ffi/src/core.rs
  9. 3
      src/rust_ffi/src/lib.rs

21
src/cli/src/main.rs

@ -1,5 +1,24 @@
extern crate klotski_ffi; extern crate klotski_ffi;
fn main() { fn main() {
println!("{:#?}", klotski_ffi::load_metadata()); // println!("{:#?}", klotski_ffi::load_metadata());
// println!("{}", klotski_ffi::is_short_code_available());
// println!("{}", klotski_ffi::is_short_code_available_fast());
//
// klotski_ffi::short_code_enable();
//
// println!("{}", klotski_ffi::is_short_code_available());
// println!("{}", klotski_ffi::is_short_code_available_fast());
//
// klotski_ffi::short_code_enable_fast();
//
// println!("{}", klotski_ffi::is_short_code_available());
// println!("{}", klotski_ffi::is_short_code_available_fast());
// let raw_code = klotski_ffi::common_code_to_raw_code(0x1A9BF0C00).unwrap();
// println!("raw code = {}", raw_code);
println!("short code: {}", klotski_ffi::short_code_to_string(12345).unwrap());
} }

12
src/klotski_core/ffi/all_cases.cc

@ -8,23 +8,23 @@ using klotski::BasicRanges;
const uint32_t ALL_CASES_SIZE = klotski::ALL_CASES_SIZE_SUM; const uint32_t ALL_CASES_SIZE = klotski::ALL_CASES_SIZE_SUM;
const uint32_t BASIC_RANGES_SIZE = klotski::BASIC_RANGES_SIZE; const uint32_t BASIC_RANGES_SIZE = klotski::BASIC_RANGES_SIZE;
void all_cases_build() noexcept { void all_cases_build() {
AllCases::build(); AllCases::build();
} }
void basic_ranges_build() noexcept { void basic_ranges_build() {
BasicRanges::build(); BasicRanges::build();
} }
bool is_all_cases_available() noexcept { bool is_all_cases_available() {
return AllCases::status() == AllCases::AVAILABLE; return AllCases::status() == AllCases::AVAILABLE;
} }
bool is_basic_ranges_available() noexcept { bool is_basic_ranges_available() {
return BasicRanges::status() == BasicRanges::AVAILABLE; return BasicRanges::status() == BasicRanges::AVAILABLE;
} }
void export_all_cases(uint64_t *buffer) noexcept { void export_all_cases(uint64_t *buffer) {
for (uint64_t head = 0; head < 16; ++head) { for (uint64_t head = 0; head < 16; ++head) {
for (const auto &range : AllCases::fetch()[head]) { for (const auto &range : AllCases::fetch()[head]) {
*(buffer++) = head << 32 | range; *(buffer++) = head << 32 | range;
@ -32,7 +32,7 @@ void export_all_cases(uint64_t *buffer) noexcept {
} }
} }
void export_basic_ranges(uint32_t *buffer) noexcept { void export_basic_ranges(uint32_t *buffer) {
auto basic_ranges_ptr = &*BasicRanges::fetch().begin(); auto basic_ranges_ptr = &*BasicRanges::fetch().begin();
memcpy(buffer, basic_ranges_ptr, BASIC_RANGES_SIZE * 4); // 32-bits -> 4-bytes memcpy(buffer, basic_ranges_ptr, BASIC_RANGES_SIZE * 4); // 32-bits -> 4-bytes
} }

64
src/klotski_core/ffi/codec.cc

@ -12,15 +12,15 @@ using klotski::BasicRanges;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void short_code_enable() noexcept { void short_code_enable() {
ShortCode::speed_up(ShortCode::NORMAL); ShortCode::speed_up(ShortCode::NORMAL);
} }
void short_code_enable_fast() noexcept { void short_code_enable_fast() {
ShortCode::speed_up(ShortCode::FAST); ShortCode::speed_up(ShortCode::FAST);
} }
bool is_short_code_available() noexcept { bool is_short_code_available() {
if (BasicRanges::status() != BasicRanges::AVAILABLE) { if (BasicRanges::status() != BasicRanges::AVAILABLE) {
return false; return false;
} }
@ -28,7 +28,7 @@ bool is_short_code_available() noexcept {
return true; return true;
} }
bool is_short_code_available_fast() noexcept { bool is_short_code_available_fast() {
if (AllCases::status() != AllCases::AVAILABLE) { if (AllCases::status() != AllCases::AVAILABLE) {
return false; return false;
} }
@ -38,21 +38,21 @@ bool is_short_code_available_fast() noexcept {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
bool raw_code_check(uint64_t raw_code) noexcept { bool raw_code_check(uint64_t raw_code) {
return RawCode::check(raw_code); return RawCode::check(raw_code);
} }
bool short_code_check(uint32_t short_code) noexcept { bool short_code_check(uint32_t short_code) {
return ShortCode::check(short_code); return ShortCode::check(short_code);
} }
bool common_code_check(uint64_t common_code) noexcept { bool common_code_check(uint64_t common_code) {
return CommonCode::check(common_code); return CommonCode::check(common_code);
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code) noexcept { bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code) {
if (!RawCode::check(raw_code)) { if (!RawCode::check(raw_code)) {
return false; return false;
} }
@ -62,7 +62,7 @@ bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code) noexcept {
return true; return true;
} }
bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code) noexcept { bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code) {
if (!ShortCode::check(short_code)) { if (!ShortCode::check(short_code)) {
return false; return false;
} }
@ -72,7 +72,7 @@ bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code) noexcept {
return true; return true;
} }
bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code) noexcept { bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code) {
if (!RawCode::check(raw_code)) { if (!RawCode::check(raw_code)) {
return false; return false;
} }
@ -82,7 +82,7 @@ bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code) noexcept
return true; return true;
} }
bool common_code_to_raw_code(uint64_t common_code, uint64_t *raw_code) noexcept { bool common_code_to_raw_code(uint64_t common_code, uint64_t *raw_code) {
if (!CommonCode::check(common_code)) { if (!CommonCode::check(common_code)) {
return false; return false;
} }
@ -92,7 +92,7 @@ bool common_code_to_raw_code(uint64_t common_code, uint64_t *raw_code) noexcept
return true; return true;
} }
bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code) noexcept { bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code) {
if (!ShortCode::check(short_code)) { if (!ShortCode::check(short_code)) {
return false; return false;
} }
@ -102,7 +102,7 @@ bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code) noexc
return true; return true;
} }
bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code) noexcept { bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code) {
if (!CommonCode::check(common_code)) { if (!CommonCode::check(common_code)) {
return false; return false;
} }
@ -114,35 +114,35 @@ bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code) noexc
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
uint32_t raw_code_to_short_code_unsafe(uint64_t raw_code) noexcept { uint32_t raw_code_to_short_code_unsafe(uint64_t raw_code) {
return RawCode::unsafe_create(raw_code) return RawCode::unsafe_create(raw_code)
.to_common_code().to_short_code().unwrap(); .to_common_code().to_short_code().unwrap();
} }
uint64_t short_code_to_raw_code_unsafe(uint32_t short_code) noexcept { uint64_t short_code_to_raw_code_unsafe(uint32_t short_code) {
return ShortCode::unsafe_create(short_code) return ShortCode::unsafe_create(short_code)
.to_common_code().to_raw_code().unwrap(); .to_common_code().to_raw_code().unwrap();
} }
uint64_t raw_code_to_common_code_unsafe(uint64_t raw_code) noexcept { uint64_t raw_code_to_common_code_unsafe(uint64_t raw_code) {
return RawCode::unsafe_create(raw_code).to_common_code().unwrap(); return RawCode::unsafe_create(raw_code).to_common_code().unwrap();
} }
uint64_t common_code_to_raw_code_unsafe(uint64_t common_code) noexcept { uint64_t common_code_to_raw_code_unsafe(uint64_t common_code) {
return CommonCode::unsafe_create(common_code).to_raw_code().unwrap(); return CommonCode::unsafe_create(common_code).to_raw_code().unwrap();
} }
uint64_t short_code_to_common_code_unsafe(uint32_t short_code) noexcept { uint64_t short_code_to_common_code_unsafe(uint32_t short_code) {
return ShortCode::unsafe_create(short_code).to_common_code().unwrap(); return ShortCode::unsafe_create(short_code).to_common_code().unwrap();
} }
uint32_t common_code_to_short_code_unsafe(uint64_t common_code) noexcept { uint32_t common_code_to_short_code_unsafe(uint64_t common_code) {
return CommonCode::unsafe_create(common_code).to_short_code().unwrap(); return CommonCode::unsafe_create(common_code).to_short_code().unwrap();
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
bool is_vertical_mirror(uint64_t raw_code, bool *result) noexcept { bool is_vertical_mirror(uint64_t raw_code, bool *result) {
if (!RawCode::check(raw_code)) { if (!RawCode::check(raw_code)) {
return false; return false;
} }
@ -150,7 +150,7 @@ bool is_vertical_mirror(uint64_t raw_code, bool *result) noexcept {
return true; return true;
} }
bool is_horizontal_mirror(uint64_t raw_code, bool *result) noexcept { bool is_horizontal_mirror(uint64_t raw_code, bool *result) {
if (!RawCode::check(raw_code)) { if (!RawCode::check(raw_code)) {
return false; return false;
} }
@ -158,7 +158,7 @@ bool is_horizontal_mirror(uint64_t raw_code, bool *result) noexcept {
return true; return true;
} }
bool to_vertical_mirror(uint64_t raw_code, uint64_t *result) noexcept { bool to_vertical_mirror(uint64_t raw_code, uint64_t *result) {
if (!RawCode::check(raw_code)) { if (!RawCode::check(raw_code)) {
return false; return false;
} }
@ -166,7 +166,7 @@ bool to_vertical_mirror(uint64_t raw_code, uint64_t *result) noexcept {
return true; return true;
} }
bool to_horizontal_mirror(uint64_t raw_code, uint64_t *result) noexcept { bool to_horizontal_mirror(uint64_t raw_code, uint64_t *result) {
if (!RawCode::check(raw_code)) { if (!RawCode::check(raw_code)) {
return false; return false;
} }
@ -176,19 +176,19 @@ bool to_horizontal_mirror(uint64_t raw_code, uint64_t *result) noexcept {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
bool is_vertical_mirror_unsafe(uint64_t raw_code) noexcept { bool is_vertical_mirror_unsafe(uint64_t raw_code) {
return RawCode::unsafe_create(raw_code).is_vertical_mirror(); return RawCode::unsafe_create(raw_code).is_vertical_mirror();
} }
bool is_horizontal_mirror_unsafe(uint64_t raw_code) noexcept { bool is_horizontal_mirror_unsafe(uint64_t raw_code) {
return RawCode::unsafe_create(raw_code).is_horizontal_mirror(); return RawCode::unsafe_create(raw_code).is_horizontal_mirror();
} }
uint64_t to_vertical_mirror_unsafe(uint64_t raw_code) noexcept { uint64_t to_vertical_mirror_unsafe(uint64_t raw_code) {
return RawCode::unsafe_create(raw_code).to_vertical_mirror().unwrap(); return RawCode::unsafe_create(raw_code).to_vertical_mirror().unwrap();
} }
uint64_t to_horizontal_mirror_unsafe(uint64_t raw_code) noexcept { uint64_t to_horizontal_mirror_unsafe(uint64_t raw_code) {
return RawCode::unsafe_create(raw_code).to_horizontal_mirror().unwrap(); return RawCode::unsafe_create(raw_code).to_horizontal_mirror().unwrap();
} }
@ -196,7 +196,7 @@ uint64_t to_horizontal_mirror_unsafe(uint64_t raw_code) noexcept {
const uint32_t SHORT_CODE_STR_SIZE = 6; const uint32_t SHORT_CODE_STR_SIZE = 6;
bool short_code_to_string(uint32_t short_code, char short_code_str[]) noexcept { bool short_code_to_string(uint32_t short_code, char short_code_str[]) {
if (!ShortCode::check(short_code)) { if (!ShortCode::check(short_code)) {
return false; return false;
} }
@ -205,7 +205,7 @@ bool short_code_to_string(uint32_t short_code, char short_code_str[]) noexcept {
return true; return true;
} }
bool short_code_from_string(const char short_code_str[], uint32_t *short_code) noexcept { bool short_code_from_string(const char short_code_str[], uint32_t *short_code) {
try { try {
*short_code = ShortCode::from_string(short_code_str).unwrap(); *short_code = ShortCode::from_string(short_code_str).unwrap();
} catch (...) { } catch (...) {
@ -218,7 +218,7 @@ bool short_code_from_string(const char short_code_str[], uint32_t *short_code) n
const uint32_t COMMON_CODE_STR_SIZE = 10; const uint32_t COMMON_CODE_STR_SIZE = 10;
bool common_code_to_string(uint64_t common_code, char common_code_str[]) noexcept { bool common_code_to_string(uint64_t common_code, char common_code_str[]) {
if (!CommonCode::check(common_code)) { if (!CommonCode::check(common_code)) {
return false; return false;
} }
@ -227,7 +227,7 @@ bool common_code_to_string(uint64_t common_code, char common_code_str[]) noexcep
return true; return true;
} }
bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]) noexcept { bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]) {
if (!CommonCode::check(common_code)) { if (!CommonCode::check(common_code)) {
return false; return false;
} }
@ -236,7 +236,7 @@ bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[])
return true; return true;
} }
bool common_code_from_string(const char common_code_str[], uint64_t *common_code) noexcept { bool common_code_from_string(const char common_code_str[], uint64_t *common_code) {
try { try {
*common_code = CommonCode::from_string(common_code_str).unwrap(); *common_code = CommonCode::from_string(common_code_str).unwrap();
} catch (...) { } catch (...) {

24
src/klotski_core/ffi/metadata.cc

@ -1,50 +1,50 @@
#include "klotski.h" #include "klotski.h"
#include "metadata.h" #include "metadata.h"
uint32_t get_version_major() noexcept { uint32_t get_version_major() {
return VERSION_MAJOR; return VERSION_MAJOR;
} }
uint32_t get_version_minor() noexcept { uint32_t get_version_minor() {
return VERSION_MINOR; return VERSION_MINOR;
} }
uint32_t get_version_patch() noexcept { uint32_t get_version_patch() {
return VERSION_PATCH; return VERSION_PATCH;
} }
const char* get_author() noexcept { const char* get_author() {
return AUTHOR; return AUTHOR;
} }
const char* get_git_tag() noexcept { const char* get_git_tag() {
return GIT_TAG_ID; return GIT_TAG_ID;
} }
const char* get_version() noexcept { const char* get_version() {
return VERSION_STR; return VERSION_STR;
} }
const char* get_commit_id() noexcept { const char* get_commit_id() {
return GIT_COMMIT_ID; return GIT_COMMIT_ID;
} }
const char* get_build_time() noexcept { const char* get_build_time() {
return BUILD_TIME; return BUILD_TIME;
} }
const char* get_git_branch() noexcept { const char* get_git_branch() {
return GIT_BRANCH; return GIT_BRANCH;
} }
const char* get_project_url() noexcept { const char* get_project_url() {
return GIT_PROJECT; return GIT_PROJECT;
} }
const char* get_system_info() noexcept { const char* get_system_info() {
return SYSTEM; return SYSTEM;
} }
const char* get_compiler_info() noexcept { const char* get_compiler_info() {
return COMPILER; return COMPILER;
} }

113
src/klotski_core/klotski.h

@ -2,7 +2,6 @@
#ifdef __cplusplus #ifdef __cplusplus
#include <cstdint> #include <cstdint>
#define NOEXCEPT noexcept
#else #else
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
@ -25,19 +24,19 @@ extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
EXTERN_FUNC uint32_t get_version_major() NOEXCEPT; extern uint32_t get_version_major();
EXTERN_FUNC uint32_t get_version_minor() NOEXCEPT; EXTERN_FUNC uint32_t get_version_minor();
EXTERN_FUNC uint32_t get_version_patch() NOEXCEPT; EXTERN_FUNC uint32_t get_version_patch();
EXTERN_FUNC const char* get_author() NOEXCEPT; EXTERN_FUNC const char* get_author();
EXTERN_FUNC const char* get_git_tag() NOEXCEPT; EXTERN_FUNC const char* get_git_tag();
EXTERN_FUNC const char* get_version() NOEXCEPT; EXTERN_FUNC const char* get_version();
EXTERN_FUNC const char* get_commit_id() NOEXCEPT; EXTERN_FUNC const char* get_commit_id();
EXTERN_FUNC const char* get_build_time() NOEXCEPT; EXTERN_FUNC const char* get_build_time();
EXTERN_FUNC const char* get_git_branch() NOEXCEPT; EXTERN_FUNC const char* get_git_branch();
EXTERN_FUNC const char* get_project_url() NOEXCEPT; EXTERN_FUNC const char* get_project_url();
EXTERN_FUNC const char* get_system_info() NOEXCEPT; EXTERN_FUNC const char* get_system_info();
EXTERN_FUNC const char* get_compiler_info() NOEXCEPT; EXTERN_FUNC const char* get_compiler_info();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
@ -47,16 +46,16 @@ extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
EXTERN_FUNC void all_cases_build() NOEXCEPT; EXTERN_FUNC void all_cases_build();
EXTERN_FUNC void basic_ranges_build() NOEXCEPT; EXTERN_FUNC void basic_ranges_build();
EXTERN_FUNC bool is_all_cases_available() NOEXCEPT; EXTERN_FUNC bool is_all_cases_available();
EXTERN_FUNC bool is_basic_ranges_available() NOEXCEPT; EXTERN_FUNC bool is_basic_ranges_available();
extern const uint32_t ALL_CASES_SIZE; extern const uint32_t ALL_CASES_SIZE;
EXTERN_FUNC void export_all_cases(uint64_t *buffer) NOEXCEPT; EXTERN_FUNC void export_all_cases(uint64_t *buffer);
extern const uint32_t BASIC_RANGES_SIZE; extern const uint32_t BASIC_RANGES_SIZE;
EXTERN_FUNC void export_basic_ranges(uint32_t *buffer) NOEXCEPT; EXTERN_FUNC void export_basic_ranges(uint32_t *buffer);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
@ -66,47 +65,47 @@ extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
EXTERN_FUNC void short_code_enable() NOEXCEPT; EXTERN_FUNC void short_code_enable();
EXTERN_FUNC void short_code_enable_fast() NOEXCEPT; EXTERN_FUNC void short_code_enable_fast();
EXTERN_FUNC bool is_short_code_available() NOEXCEPT; EXTERN_FUNC bool is_short_code_available();
EXTERN_FUNC bool is_short_code_available_fast() NOEXCEPT; EXTERN_FUNC bool is_short_code_available_fast();
EXTERN_FUNC bool raw_code_check(uint64_t raw_code) NOEXCEPT; EXTERN_FUNC bool raw_code_check(uint64_t raw_code);
EXTERN_FUNC bool short_code_check(uint32_t short_code) NOEXCEPT; EXTERN_FUNC bool short_code_check(uint32_t short_code);
EXTERN_FUNC bool common_code_check(uint64_t common_code) NOEXCEPT; EXTERN_FUNC bool common_code_check(uint64_t common_code);
EXTERN_FUNC bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code) NOEXCEPT; EXTERN_FUNC bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code);
EXTERN_FUNC bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code) NOEXCEPT; EXTERN_FUNC bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code);
EXTERN_FUNC bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code) NOEXCEPT; EXTERN_FUNC bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code);
EXTERN_FUNC bool common_code_to_raw_code(uint64_t common_code, uint64_t *raw_code) NOEXCEPT; EXTERN_FUNC bool common_code_to_raw_code(uint64_t common_code, uint64_t *raw_code);
EXTERN_FUNC bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code) NOEXCEPT; EXTERN_FUNC bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code);
EXTERN_FUNC bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code) NOEXCEPT; EXTERN_FUNC bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code);
EXTERN_FUNC uint32_t raw_code_to_short_code_unsafe(uint64_t raw_code) NOEXCEPT; EXTERN_FUNC uint32_t raw_code_to_short_code_unsafe(uint64_t raw_code);
EXTERN_FUNC uint64_t short_code_to_raw_code_unsafe(uint32_t short_code) NOEXCEPT; EXTERN_FUNC uint64_t short_code_to_raw_code_unsafe(uint32_t short_code);
EXTERN_FUNC uint64_t raw_code_to_common_code_unsafe(uint64_t raw_code) NOEXCEPT; EXTERN_FUNC uint64_t raw_code_to_common_code_unsafe(uint64_t raw_code);
EXTERN_FUNC uint64_t common_code_to_raw_code_unsafe(uint64_t common_code) NOEXCEPT; EXTERN_FUNC uint64_t common_code_to_raw_code_unsafe(uint64_t common_code);
EXTERN_FUNC uint64_t short_code_to_common_code_unsafe(uint32_t short_code) NOEXCEPT; EXTERN_FUNC uint64_t short_code_to_common_code_unsafe(uint32_t short_code);
EXTERN_FUNC uint32_t common_code_to_short_code_unsafe(uint64_t common_code) NOEXCEPT; EXTERN_FUNC uint32_t common_code_to_short_code_unsafe(uint64_t common_code);
EXTERN_FUNC bool is_vertical_mirror(uint64_t raw_code, bool *result) NOEXCEPT; EXTERN_FUNC bool is_vertical_mirror(uint64_t raw_code, bool *result);
EXTERN_FUNC bool is_horizontal_mirror(uint64_t raw_code, bool *result) NOEXCEPT; EXTERN_FUNC bool is_horizontal_mirror(uint64_t raw_code, bool *result);
EXTERN_FUNC bool to_vertical_mirror(uint64_t raw_code, uint64_t *result) NOEXCEPT; EXTERN_FUNC bool to_vertical_mirror(uint64_t raw_code, uint64_t *result);
EXTERN_FUNC bool to_horizontal_mirror(uint64_t raw_code, uint64_t *result) NOEXCEPT; EXTERN_FUNC bool to_horizontal_mirror(uint64_t raw_code, uint64_t *result);
EXTERN_FUNC bool is_vertical_mirror_unsafe(uint64_t raw_code) NOEXCEPT; EXTERN_FUNC bool is_vertical_mirror_unsafe(uint64_t raw_code);
EXTERN_FUNC bool is_horizontal_mirror_unsafe(uint64_t raw_code) NOEXCEPT; EXTERN_FUNC bool is_horizontal_mirror_unsafe(uint64_t raw_code);
EXTERN_FUNC uint64_t to_vertical_mirror_unsafe(uint64_t raw_code) NOEXCEPT; EXTERN_FUNC uint64_t to_vertical_mirror_unsafe(uint64_t raw_code);
EXTERN_FUNC uint64_t to_horizontal_mirror_unsafe(uint64_t raw_code) NOEXCEPT; EXTERN_FUNC uint64_t to_horizontal_mirror_unsafe(uint64_t raw_code);
extern const uint32_t SHORT_CODE_STR_SIZE; extern const uint32_t SHORT_CODE_STR_SIZE;
EXTERN_FUNC bool short_code_to_string(uint32_t short_code, char short_code_str[]) NOEXCEPT; EXTERN_FUNC bool short_code_to_string(uint32_t short_code, char short_code_str[]);
EXTERN_FUNC bool short_code_from_string(const char short_code_str[], uint32_t *short_code) NOEXCEPT; EXTERN_FUNC bool short_code_from_string(const char short_code_str[], uint32_t *short_code);
extern const uint32_t COMMON_CODE_STR_SIZE; extern const uint32_t COMMON_CODE_STR_SIZE;
EXTERN_FUNC bool common_code_to_string(uint64_t common_code, char common_code_str[]) NOEXCEPT; EXTERN_FUNC bool common_code_to_string(uint64_t common_code, char common_code_str[]);
EXTERN_FUNC bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]) NOEXCEPT; EXTERN_FUNC bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]);
EXTERN_FUNC bool common_code_from_string(const char common_code_str[], uint64_t *common_code) NOEXCEPT; EXTERN_FUNC bool common_code_from_string(const char common_code_str[], uint64_t *common_code);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

234
src/rust_ffi/src/codec/ffi.rs

@ -0,0 +1,234 @@
use std::f32::consts::E;
use std::ffi::{c_char, CString};
use super::Core;
fn short_code_enable() {
unsafe {
Core::short_code_enable()
}
}
fn short_code_enable_fast() {
unsafe {
Core::short_code_enable_fast()
}
}
fn is_short_code_available() -> bool {
unsafe {
Core::is_short_code_available()
}
}
fn is_short_code_available_fast() -> bool {
unsafe {
Core::is_short_code_available_fast()
}
}
fn raw_code_check(raw_code: u64) -> bool {
unsafe {
Core::raw_code_check(raw_code)
}
}
fn short_code_check(short_code: u32) -> bool {
unsafe {
Core::short_code_check(short_code)
}
}
fn common_code_check(common_code: u64) -> bool {
unsafe {
Core::common_code_check(common_code)
}
}
fn raw_code_to_short_code(raw_code: u64) -> Result<u32, &'static str> {
let mut short_code: u32 = 0;
unsafe {
match Core::raw_code_to_short_code(raw_code, &mut short_code) {
true => Ok(short_code),
_ => Err("invalid raw code"),
}
}
}
fn short_code_to_raw_code(short_code: u32) -> Result<u64, &'static str> {
let mut raw_code: u64 = 0;
unsafe {
match Core::short_code_to_raw_code(short_code, &mut raw_code) {
true => Ok(raw_code),
_ => Err("invalid short code"),
}
}
}
fn raw_code_to_common_code(raw_code: u64) -> Result<u64, &'static str> {
let mut common_code: u64 = 0;
unsafe {
match Core::raw_code_to_common_code(raw_code, &mut common_code) {
true => Ok(common_code),
_ => Err("invalid raw code"),
}
}
}
fn common_code_to_raw_code(common_code: u64) -> Result<u64, &'static str> {
let mut raw_code: u64 = 0;
unsafe {
match Core::common_code_to_raw_code(common_code, &mut raw_code) {
true => Ok(raw_code),
_ => Err("invalid common code"),
}
}
}
fn short_code_to_common_code(short_code: u32) -> Result<u64, &'static str> {
let mut common_code: u64 = 0;
unsafe {
match Core::short_code_to_common_code(short_code, &mut common_code) {
true => Ok(common_code),
_ => Err("invalid short code"),
}
}
}
fn common_code_to_short_code(common_code: u64) -> Result<u32, &'static str> {
let mut short_code: u32 = 0;
unsafe {
match Core::common_code_to_short_code(common_code, &mut short_code) {
true => Ok(short_code),
_ => Err("invalid common code"),
}
}
}
fn raw_code_to_short_code_unsafe(raw_code: u64) -> u32 {
unsafe {
Core::raw_code_to_short_code_unsafe(raw_code)
}
}
fn short_code_to_raw_code_unsafe(short_code: u32) -> u64 {
unsafe {
Core::short_code_to_raw_code_unsafe(short_code)
}
}
fn raw_code_to_common_code_unsafe(raw_code: u64) -> u64 {
unsafe {
Core::raw_code_to_common_code_unsafe(raw_code)
}
}
fn common_code_to_raw_code_unsafe(common_code: u64) -> u64 {
unsafe {
Core::common_code_to_raw_code_unsafe(common_code)
}
}
fn short_code_to_common_code_unsafe(short_code: u32) -> u64 {
unsafe {
Core::short_code_to_common_code_unsafe(short_code)
}
}
fn common_code_to_short_code_unsafe(common_code: u64) -> u32 {
unsafe {
Core::common_code_to_short_code_unsafe(common_code)
}
}
fn is_vertical_mirror(raw_code: u64) -> Result<bool, &'static str> {
let mut result: bool = false;
unsafe {
match Core::is_vertical_mirror(raw_code, &mut result) {
true => Ok(result),
_ => Err("invalid raw code"),
}
}
}
fn is_horizontal_mirror(raw_code: u64) -> Result<bool, &'static str> {
let mut result: bool = false;
unsafe {
match Core::is_horizontal_mirror(raw_code, &mut result) {
true => Ok(result),
_ => Err("invalid raw code"),
}
}
}
fn to_vertical_mirror(raw_code: u64) -> Result<u64, &'static str> {
let mut result: u64 = 0;
unsafe {
match Core::to_vertical_mirror(raw_code, &mut result) {
true => Ok(result),
_ => Err("invalid raw code"),
}
}
}
fn to_horizontal_mirror(raw_code: u64) -> Result<u64, &'static str> {
let mut result: u64 = 0;
unsafe {
match Core::to_horizontal_mirror(raw_code, &mut result) {
true => Ok(result),
_ => Err("invalid raw code"),
}
}
}
fn is_vertical_mirror_unsafe(raw_code: u64) -> bool {
unsafe {
Core::is_vertical_mirror_unsafe(raw_code)
}
}
fn is_horizontal_mirror_unsafe(raw_code: u64) -> bool {
unsafe {
Core::is_horizontal_mirror_unsafe(raw_code)
}
}
fn to_vertical_mirror_unsafe(raw_code: u64) -> u64 {
unsafe {
Core::to_vertical_mirror_unsafe(raw_code)
}
}
fn to_horizontal_mirror_unsafe(raw_code: u64) -> u64 {
unsafe {
Core::to_horizontal_mirror_unsafe(raw_code)
}
}
// extern const uint32_t SHORT_CODE_STR_SIZE;
// EXTERN_FUNC bool short_code_to_string(uint32_t short_code, char short_code_str[]) NOEXCEPT;
// EXTERN_FUNC bool short_code_from_string(const char short_code_str[], uint32_t *short_code) NOEXCEPT;
pub fn short_code_to_string(short_code: u32) -> Result<String, &'static str> {
unsafe {
let mut buffer: Vec<c_char> = vec![0; Core::SHORT_CODE_STR_SIZE as usize];
match Core::short_code_to_string(short_code, buffer.as_mut_ptr()) {
true => {
let mut result = String::new();
for c in &buffer[..buffer.len() - 1] {
result.push(*c as u8 as char);
}
Ok(result)
},
_ => Err("invalid short code"),
}
}
}
// TODO: add unsafe version
// extern const uint32_t COMMON_CODE_STR_SIZE;
// EXTERN_FUNC bool common_code_to_string(uint64_t common_code, char common_code_str[]) NOEXCEPT;
// EXTERN_FUNC bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]) NOEXCEPT;
// EXTERN_FUNC bool common_code_from_string(const char common_code_str[], uint64_t *common_code) NOEXCEPT;
// TODO: add unsafe version

3
src/rust_ffi/src/codec/mod.rs

@ -0,0 +1,3 @@
pub mod ffi;
use super::core::Core;

2
src/rust_ffi/src/core.rs

@ -1,4 +1,4 @@
/// Klotski c-style API convert by bindgen. /// Klotski c-style ABI convert by bindgen.
/// Core module expose these interfaces for abstraction. /// Core module expose these interfaces for abstraction.
pub mod Core { pub mod Core {

3
src/rust_ffi/src/lib.rs

@ -1,4 +1,7 @@
mod core; mod core;
mod codec;
mod metadata; mod metadata;
pub use metadata::load_metadata; pub use metadata::load_metadata;
pub use codec::ffi::*;

Loading…
Cancel
Save