Skip to content

Instantly share code, notes, and snippets.

@keplersj
Created July 25, 2015 21:00
Show Gist options
  • Save keplersj/3bd3aadd71db206e828f to your computer and use it in GitHub Desktop.
Save keplersj/3bd3aadd71db206e828f to your computer and use it in GitHub Desktop.
Gist demonstrating the ability to run Crystal code from Rust.
#[link(name = "logger")]
extern {
fn CrystalLog(text: *const u8);
}
fn log(text: &'static str) {
unsafe{ CrystalLog(text.as_bytes().as_ptr()) };
}
fn main() {
log("Hello Crystal, from Rust!");
log("Logging from Crystal, using Rust, totally works twice, by the way.");
}
require "colorize"
fun log = CrystalLog(text: UInt8*): Void
# We need to initialize the GC
GC.init
# We need to invoke Crystal's "main" function, the one that initializes
# all constants and runs the top-level code (none in this case, but without
# constants like STDOUT and others the last line will crash).
# We pass 0 and null to argc and argv.
LibCrystalMain.__crystal_main(0, Pointer(Pointer(UInt8)).null)
#puts "#{"Pointer Recieved:".colorize.bold} #{text}"
#puts "#{"Value of Pointer:".colorize.bold} #{String.new(text)}"
puts String.new(text)
end
crystal logger.cr --link-flags "-dynamiclib" -o lib/x86_64-apple-darwin/liblogger.dylib
rustc logged.rs
./logged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment