feat: add calltoc
This commit is contained in:
3
__ffi/calltoc/.cargo/config.toml
Normal file
3
__ffi/calltoc/.cargo/config.toml
Normal file
@@ -0,0 +1,3 @@
|
||||
[build]
|
||||
rustflags = ["-C", "link-args=-L/Users/hatterjiang/Code/hattergit/simple-rust-tests/__ffi/calltoc -lmyc"]
|
||||
|
||||
16
__ffi/calltoc/Cargo.lock
generated
Normal file
16
__ffi/calltoc/Cargo.lock
generated
Normal file
@@ -0,0 +1,16 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "calltoc"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.139"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
|
||||
9
__ffi/calltoc/Cargo.toml
Normal file
9
__ffi/calltoc/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "calltoc"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2.139"
|
||||
7
__ffi/calltoc/justfile
Normal file
7
__ffi/calltoc/justfile
Normal file
@@ -0,0 +1,7 @@
|
||||
_:
|
||||
@just --list
|
||||
|
||||
compile_c:
|
||||
gcc -c myc.c -o myc.o
|
||||
ar -r libmyc.a myc.o
|
||||
|
||||
30
__ffi/calltoc/myc.c
Normal file
30
__ffi/calltoc/myc.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char* hello() {
|
||||
char* ptr = (char*)malloc(13);
|
||||
if (ptr == NULL) {
|
||||
return ptr;
|
||||
}
|
||||
ptr[0] = 'H';
|
||||
ptr[1] = 'e';
|
||||
ptr[2] = 'l';
|
||||
ptr[3] = 'l';
|
||||
ptr[4] = 'o';
|
||||
ptr[5] = ' ';
|
||||
ptr[6] = 'w';
|
||||
ptr[7] = 'o';
|
||||
ptr[8] = 'r';
|
||||
ptr[9] = 'l';
|
||||
ptr[10] = 'd';
|
||||
ptr[11] = '!';
|
||||
ptr[12] = '\0';
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void goodbye(char* ptr) {
|
||||
if (ptr != NULL) {
|
||||
free(ptr);
|
||||
}
|
||||
}
|
||||
37
__ffi/calltoc/src/main.rs
Normal file
37
__ffi/calltoc/src/main.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use std::ffi::{c_char, CStr};
|
||||
use std::ops::Deref;
|
||||
|
||||
extern "C" {
|
||||
fn hello() -> *const c_char;
|
||||
fn goodbye(s: *const c_char);
|
||||
}
|
||||
|
||||
struct Greeting {
|
||||
message: *const c_char,
|
||||
}
|
||||
|
||||
impl Drop for Greeting {
|
||||
fn drop(&mut self) {
|
||||
unsafe { goodbye(self.message); }
|
||||
}
|
||||
}
|
||||
|
||||
impl Greeting {
|
||||
fn new() -> Greeting {
|
||||
Greeting { message: unsafe { hello() } }
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Greeting {
|
||||
type Target = str;
|
||||
fn deref<'a>(&'a self) -> &'a str {
|
||||
let c_str = unsafe{ CStr::from_ptr(self.message) };
|
||||
c_str.to_str().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let greeting = Greeting::new();
|
||||
println!("{}", greeting.deref());
|
||||
}
|
||||
Reference in New Issue
Block a user