ddnet/src/engine/shared/rust_version.rs
heinrich5991 dcd76fd3e1 Add support for Rust code in DDNet
The glue is done using the [cxx crate](https://cxx.rs/) on the Rust
side.

As a proof-of-concept, only a small console command (`rust_version`)
printing the currently used Rust version was added.

You can generate and open the Rust documentation using
`DDNET_TEST_NO_LINK=1 cargo doc --open`.

You can run the Rust tests using `cmake --build <build dir> --target
run_rust_tests`, they're automatically included in the `run_tests`
target as well.

Rust tests don't work on Windows in debug mode on Windows because Rust
cannot currently link with the debug version of the C stdlib on Windows:
https://github.com/rust-lang/rust/issues/39016.

---

The stuff in `src/rust-bridge` is generated using
```
cxxbridge src/engine/shared/rust_version.rs --output src/rust-bridge/engine/shared/rust_version.cpp --output src/rust-bridge/engine/shared/rust_version.h
cxxbridge src/engine/console.rs --output src/rust-bridge/cpp/console.cpp --output src/rust-bridge/cpp/console.h
```
2022-10-19 23:46:06 +02:00

60 lines
1.7 KiB
Rust

use super::CFGFLAG_CLIENT;
use super::CFGFLAG_SERVER;
use ddnet_base::s;
use ddnet_base::UserPtr;
use ddnet_engine::gs_ConsoleDefaultColor;
use ddnet_engine::IConsole;
use ddnet_engine::IConsole_FCommandCallback;
use ddnet_engine::IConsole_IResult;
use ddnet_engine::IConsole_OUTPUT_LEVEL_STANDARD;
use std::pin::Pin;
#[cxx::bridge]
mod ffi {
extern "C++" {
include!("base/rust.h");
include!("engine/console.h");
type IConsole = ddnet_engine::IConsole;
}
extern "Rust" {
fn RustVersionPrint(console: &IConsole);
fn RustVersionRegister(console: Pin<&mut IConsole>);
}
}
/// Print the Rust version used for compiling this crate.
///
/// Uses [`IConsole::Print`] for printing.
#[allow(non_snake_case)]
pub fn RustVersionPrint(console: &IConsole) {
console.Print(
IConsole_OUTPUT_LEVEL_STANDARD,
s!("rust_version"),
s!(include_str!(concat!(env!("OUT_DIR"), "/rustc-version"))),
gs_ConsoleDefaultColor,
);
}
#[allow(non_snake_case)]
extern "C" fn PrintRustVersionCallback(_: &IConsole_IResult, user: UserPtr) {
RustVersionPrint(unsafe { user.cast() })
}
/// Register the `rust_version` command to the given console instance.
///
/// This command calls the [`RustVersionPrint`] function to print the Rust
/// version used for compiling this crate.
#[allow(non_snake_case)]
pub fn RustVersionRegister(console: Pin<&mut IConsole>) {
let user = console.as_ref().get_ref().into();
console.Register(
s!("rust_version"),
s!(""),
CFGFLAG_CLIENT | CFGFLAG_SERVER,
IConsole_FCommandCallback(PrintRustVersionCallback),
user,
s!("Prints the Rust version used to compile DDNet"),
);
}