Shizcow / hotpatch

Changing function definitions at runtime in Rust
Apache License 2.0
129 stars 6 forks source link

State of this project

Currently waiting for inherent traits. There has been some action here recently, and the RFC looks like it's going to be merged soon. Given that I'm busy with school anyway, I'll be holding off on continuing development until inherent traits are in nightly.

hotpatch

crates.io docs.rs

This crate is primarily used to load new function definitions from shared object files in an exceedingly easy way.

Key features:

Nightly Requirement

This crate is nightly only. A list of features it uses are as follows:

Most of the above features are critical to function. As such, this crate will remain nightly only until more of the above are finished.

Short Example

The following shows how dead-simple this crate is to use:

// main.rs
use hotpatch::*;

#[patchable]
fn foo() { }

fn main() -> Result<(), Box<dyn std::error::Error>> {
  foo(); // does nothing
  foo.hotpatch_lib("libsomething.so")?;
  foo(); // does something totally different!
  foo.hotpatch_fn(|| println!("Dynamic!"))?;
  foo(); // even more modification!
  Ok(())
}

Warning

Don't hotpatch the function you're currently in, or any of its parents.

Because hotpatch doesn't allow multiple function definitions to be in affect at the same time, this will cause a deadlock. try variants exist which will return an error if a deadlock would occur.

It is possible to do this with the force functions, however they are unsafe, as in a multithreaded enironment this could cause multiple function definitions to be in effect at once.

Docs

For more information, see the docs.

TODO

This crate is still has a long way to go before being "finished". Below are some items left to do. Submit an issue or PR to this section for feature requests!