Crates.io | hotswap |
lib.rs | hotswap |
version | 0.6.0 |
source | src |
created_at | 2016-06-19 23:43:09.671306 |
updated_at | 2018-06-27 05:17:57.953232 |
description | Easy code swapping in running executables |
homepage | |
repository | https://github.com/draivin/rust-hotswap |
max_upload_size | |
id | 5430 |
size | 26,214 |
A library for hotswapping running code with minimal effort, requires a nightly rust build.
Beware that the library is a prototype for now, and it may crash frequently.
hotswap
and hotswap-runtime
dependencies to your Cargo.toml
.dylib
build with the same project name and path to your Cargo.toml
.#![feature(plugin, const_fn)]
feature gates.#![plugin(hotswap)]
.#[hotswap]
modifier.#![hotswap_header]
attribute to the top of your program.unsafe { hotswap_start!() }
to the entry point of your program, before
you call any hotswapped functions.# Cargo.toml
[package]
name = "hotswapdemo"
version = "0.1.0"
[lib]
# This must be the same as the package name (with hyphens replaced with
# underscores). Anything else will cause an error at runtime.
name = "hotswapdemo"
crate-type = ["dylib"]
path = "src/main.rs"
[dependencies]
hotswap = "*"
hotswap-runtime = "*"
// main.rs
#![feature(plugin, const_fn)]
#![plugin(hotswap)]
#![hotswap_header]
use std::thread::sleep;
use std::time::Duration;
#[hotswap]
fn test(test: i32) -> () {
println!("Foo: {}", test);
}
fn main() {
unsafe { hotswap_start!() }
let mut i = 1;
loop {
test(i);
i += 1;
sleep(Duration::from_millis(2000));
}
}
That's it!
From there you can run the binary
> cargo run
Running `target/debug/hotswapdemo`
Foo: 1
Foo: 2
Foo: 3
Then, once it is running, you can edit the printing code, e.g.
println!("Bar: {} :)", test);
Once you recompile the code on another terminal (or on the same one using background), you'll see the changes!
> cargo build --lib
Compiling hotswapdemo v0.1.0 [...]
> fg
Foo: 7
Foo: 8
Bar: 9 :)
Bar: 10 :)
The running code will update without restarting the binary or losing state!