mirror of https://github.com/dnomd343/ClearDNS
Dnomd343
2 years ago
6 changed files with 60 additions and 0 deletions
@ -0,0 +1,18 @@ |
|||
#include <stdio.h> |
|||
#include "to_json.h" |
|||
|
|||
int main() { |
|||
printf("start demo\n"); |
|||
|
|||
char raw_string[] = "hello"; |
|||
|
|||
const char *json_content = to_json_rust(raw_string); |
|||
|
|||
printf("return content -> `%s`\n", json_content); |
|||
|
|||
free_rust_string(json_content); |
|||
|
|||
printf("rust string free success\n"); |
|||
|
|||
return 0; |
|||
} |
@ -0,0 +1,7 @@ |
|||
# This file is automatically @generated by Cargo. |
|||
# It is not intended for manual editing. |
|||
version = 3 |
|||
|
|||
[[package]] |
|||
name = "to-json" |
|||
version = "0.1.0" |
@ -0,0 +1,11 @@ |
|||
[package] |
|||
name = "to-json" |
|||
version = "0.1.0" |
|||
edition = "2021" |
|||
|
|||
[lib] |
|||
crate-type = ["cdylib"] |
|||
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
|||
|
|||
[dependencies] |
@ -0,0 +1,15 @@ |
|||
use std::ffi::{c_char, CStr, CString}; |
|||
|
|||
#[no_mangle] |
|||
pub unsafe extern "C" fn to_json_rust(content: *const c_char) -> *const c_char { |
|||
let content: &str = CStr::from_ptr(content).to_str().unwrap(); |
|||
|
|||
println!("Raw content -> {}", content); |
|||
|
|||
CString::new(content).unwrap().into_raw() |
|||
} |
|||
|
|||
#[no_mangle] |
|||
pub unsafe extern "C" fn free_rust_string(string: *const c_char) { |
|||
let _ = CString::from_raw(string as *mut _); |
|||
} |
@ -0,0 +1,8 @@ |
|||
#include <stdarg.h> |
|||
#include <stdbool.h> |
|||
#include <stdint.h> |
|||
#include <stdlib.h> |
|||
|
|||
const char *to_json_rust(const char *content); |
|||
|
|||
void free_rust_string(const char *string); |
Loading…
Reference in new issue