feat: add ..
This commit is contained in:
5
live-reload-rust/greet-rs-2/Cargo.lock
generated
Normal file
5
live-reload-rust/greet-rs-2/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "greet-rs-2"
|
||||
version = "0.1.0"
|
||||
9
live-reload-rust/greet-rs-2/Cargo.toml
Normal file
9
live-reload-rust/greet-rs-2/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "greet-rs-2"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
36
live-reload-rust/greet-rs-2/src/main.rs
Normal file
36
live-reload-rust/greet-rs-2/src/main.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use std::{ffi::c_void, ffi::CString, os::raw::c_char, os::raw::c_int};
|
||||
|
||||
#[link(name = "dl")]
|
||||
extern "C" {
|
||||
fn dlopen(path: *const c_char, flags: c_int) -> *const c_void;
|
||||
fn dlsym(handle: *const c_void, name: *const c_char) -> *const c_void;
|
||||
fn dlclose(handle: *const c_void);
|
||||
}
|
||||
|
||||
// had to look that one up in `dlfcn.h`
|
||||
// in C, it's a #define. in Rust, it's a proper constant
|
||||
pub const RTLD_LAZY: c_int = 0x00001;
|
||||
|
||||
fn main() {
|
||||
let lib_name = CString::new("../libgreet.so").unwrap();
|
||||
let lib = unsafe { dlopen(lib_name.as_ptr(), RTLD_LAZY) };
|
||||
if lib.is_null() {
|
||||
panic!("could not open library");
|
||||
}
|
||||
|
||||
let greet_name = CString::new("greet").unwrap();
|
||||
let greet = unsafe { dlsym(lib, greet_name.as_ptr()) };
|
||||
|
||||
type Greet = unsafe extern "C" fn(name: *const c_char);
|
||||
use std::mem::transmute;
|
||||
let greet: Greet = unsafe { transmute(greet) };
|
||||
|
||||
let name = CString::new("fresh coffee").unwrap();
|
||||
unsafe {
|
||||
greet(name.as_ptr());
|
||||
}
|
||||
|
||||
unsafe {
|
||||
dlclose(lib);
|
||||
}
|
||||
}
|
||||
81
live-reload-rust/greet-rs-3/Cargo.lock
generated
Normal file
81
live-reload-rust/greet-rs-3/Cargo.lock
generated
Normal file
@@ -0,0 +1,81 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||
|
||||
[[package]]
|
||||
name = "cstr"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d59a053faf8d8062404e09ef74417a6ac0d337b6729e26ee5f0b4a839f8ff61d"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "greet-rs-3"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"cstr",
|
||||
"libloading",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libloading"
|
||||
version = "0.6.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2443d8f0478b16759158b2f66d525991a05491138bc05814ef52a250148ef4f9"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "51ef7cd2518ead700af67bf9d1a658d90b6037d77110fd9c0445429d0ba1c6c9"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
11
live-reload-rust/greet-rs-3/Cargo.toml
Normal file
11
live-reload-rust/greet-rs-3/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "greet-rs-3"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
cstr = "*"
|
||||
libloading = "*"
|
||||
14
live-reload-rust/greet-rs-3/src/main.rs
Normal file
14
live-reload-rust/greet-rs-3/src/main.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use cstr::cstr;
|
||||
use std::{error::Error, os::raw::c_char};
|
||||
|
||||
use libloading::{Library, Symbol};
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let lib = Library::new("../libgreet.so")?;
|
||||
unsafe {
|
||||
let greet: Symbol<unsafe extern "C" fn(name: *const c_char)> = lib.get(b"greet")?;
|
||||
greet(cstr!("rust macros").as_ptr());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
6
live-reload-rust/libgreet-rs/Cargo.lock
generated
Normal file
6
live-reload-rust/libgreet-rs/Cargo.lock
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "greet"
|
||||
version = "0.1.0"
|
||||
|
||||
12
live-reload-rust/libgreet-rs/Cargo.toml
Normal file
12
live-reload-rust/libgreet-rs/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "greet"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[lib]
|
||||
crate-type = ["dylib"]
|
||||
|
||||
[dependencies]
|
||||
10
live-reload-rust/libgreet-rs/src/lib.rs
Normal file
10
live-reload-rust/libgreet-rs/src/lib.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use std::{ffi::CStr, os::raw::c_char};
|
||||
|
||||
/// # Safety
|
||||
/// Pointer must be valid, and point to a null-terminated
|
||||
/// string. What happens otherwise is UB.
|
||||
pub unsafe extern "C" fn greet(name: *const c_char) {
|
||||
let cstr = CStr::from_ptr(name);
|
||||
println!("Hello, {}!", cstr.to_str().unwrap());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user