Skip to content

Instantly share code, notes, and snippets.

@whb07
Last active February 2, 2021 13:56
Show Gist options
  • Save whb07/f9484aa55e0141bd5609a34109a1b501 to your computer and use it in GitHub Desktop.
Save whb07/f9484aa55e0141bd5609a34109a1b501 to your computer and use it in GitHub Desktop.
// compile by:
// rustc src/lib.rs --crate-type cdylib
#[repr(C)]
pub struct Point {
pub x: f64,
pub y: f64,
}
#[no_mangle]
pub extern "C" fn add_points(left: Point, right: Point) -> Point {
Point {
x: left.x + right.x,
y: left.y + right.y,
}
}
// Learn more about F# at http://fsharp.org
open System
open System.Runtime.InteropServices
// let SL = StructAttribute()
[<StructLayout(LayoutKind.Sequential)>]
[<Struct>]
type Point = {
x : float ; y : float
}
[<DllImport(@"/tmp/pointer/liblib.dylib")>]
extern Point add_points(Point a, Point b);
[<EntryPoint>]
let main argv =
let first = { x=1.5; y=15.0 };
let second = { x=2.5; y=5.0 };
let third = add_points(first, second);
printfn "Hello World from F#! %A" third;
0 // return an integer exit code
// Learn more about F# at http://fsharp.org
open System.Runtime.InteropServices
[<DllImport(@"/tmp/pointer/fpointer/libsendit.dylib", CharSet = CharSet.Ansi)>]
extern void sendit(string s);
[<EntryPoint>]
let main argv =
sendit("/pizza")
0 // return an integer exit code
//toml
// [dependencies]
// rocket = "0.4.0"
// libc = "0.2.76"
// [lib]
// crate-type = ["cdylib"]
#![feature(proc_macro_hygiene, decl_macro)]
use rocket::{ignite, routes, get};
use libc::c_char;
use std::ffi::CStr;
#[get("/world")]
fn hi() -> &'static str {
"Hello!"
}
#[no_mangle]
pub extern "C" fn sendit(s: *const c_char){
let c_str = unsafe {
assert!(!s.is_null());
CStr::from_ptr(s)
};
let r_str = c_str.to_str().unwrap();
ignite().mount(r_str, routes![hi]).launch();
@whb07
Copy link
Author

whb07 commented Feb 2, 2021

This is a interop play between Rust -> CFFI -> .NET world. While seemingly small, due to incomplete documentation and experimentation, it was a nice and interesting way to target .NET with Rust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment