From 9dbf2d4688d9ad1406a6c1bb02d65aea82ae923a Mon Sep 17 00:00:00 2001 From: Joshua Piper <32164094+JoshPiper@users.noreply.github.com> Date: Fri, 10 Dec 2021 13:14:40 +0000 Subject: [PATCH] docs: Add printing to console example. --- example/printing-to-console/Cargo.toml | 11 +++++++ example/printing-to-console/README.md | 4 +++ .../printing-to-console/rust-toolchain.toml | 2 ++ example/printing-to-console/src/lib.rs | 29 +++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 example/printing-to-console/Cargo.toml create mode 100644 example/printing-to-console/README.md create mode 100644 example/printing-to-console/rust-toolchain.toml create mode 100644 example/printing-to-console/src/lib.rs diff --git a/example/printing-to-console/Cargo.toml b/example/printing-to-console/Cargo.toml new file mode 100644 index 0000000..11b6afd --- /dev/null +++ b/example/printing-to-console/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "printing-to-console" +version = "0.1.0" +edition = "2021" +publish = false + +[lib] +crate-type = ["cdylib"] + +[dependencies] +gmod = {version = "10.2.1", features = ["gmcl"], default-features = false} \ No newline at end of file diff --git a/example/printing-to-console/README.md b/example/printing-to-console/README.md new file mode 100644 index 0000000..2d23812 --- /dev/null +++ b/example/printing-to-console/README.md @@ -0,0 +1,4 @@ +# Printing to Console Example + +This is an example of using a module to print to console. +Compiling should follow the instructions in [my-first-binary-module](../my-first-binary-module/README.md). diff --git a/example/printing-to-console/rust-toolchain.toml b/example/printing-to-console/rust-toolchain.toml new file mode 100644 index 0000000..271800c --- /dev/null +++ b/example/printing-to-console/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly" \ No newline at end of file diff --git a/example/printing-to-console/src/lib.rs b/example/printing-to-console/src/lib.rs new file mode 100644 index 0000000..8e6137a --- /dev/null +++ b/example/printing-to-console/src/lib.rs @@ -0,0 +1,29 @@ +#![feature(c_unwind)] + +use gmod::gmcl::override_stdout; +use gmod::lua::State; + +#[macro_use] extern crate gmod; + +#[gmod13_open] +fn gmod13_open(lua: State) -> i32 { + // Here, if this module is running on the client. + if lua.is_client() { + // We overwrite println! so it prints to the console. + override_stdout() + } + + if lua.is_server() { + println!("Hello Server, this is a binary module!") + } else { + println!("Hello Client, this is a binary module!") + } + + 0 +} + +#[gmod13_close] +fn gmod13_close(lua: State) -> i32 { + println!("Goodbye from binary module!"); + 0 +}