For example, I have the target locked code:
#[cfg(all(target_family="wasm", target_os="emscripten"))] {
unsafe {
let webgl_context_result: i32 = create_webgl_context();
debug!("WebGL Context Created With Value {:?}...", emscripten::read_emscripten_result(webgl_context_result));
}
}
The function which turns the int to an enum value is:
#[allow(dead_code)]
pub fn read_emscripten_result(value: i32) -> String {
match value.try_into() {
Ok(EmscriptenResult::Success) => String::from("Success"),
Ok(EmscriptenResult::Deferred) => String::from("Deferred"),
Ok(EmscriptenResult::NotSupported) => String::from("NotSupported"),
Ok(EmscriptenResult::FailedNotDeferred) => String::from("FailedNotDeferred"),
Ok(EmscriptenResult::InvalidTarget) => String::from("InvalidTarget"),
Ok(EmscriptenResult::UnknownTarget) => String::from("UnknownTarget"),
Ok(EmscriptenResult::InvalidParam) => String::from("InvalidParam"),
Ok(EmscriptenResult::Failed) => String::from("Failed"),
Ok(EmscriptenResult::NoData) => String::from("NoData"),
Ok(EmscriptenResult::TimedOut) => String::from("TimedOut"),
Err(_) => String::from("Unknown"),
}
}
I currently have to put #[allow(dead_code)]
in order for Rust to not warn about the code when building for non-emscripten targets. The code itself isn't dead, it just doesn't get called in all circumstances and it would be nice if I could set some kind of flag (e.g. in cargo.toml or at the top of main.rs and lib.rs) which tells Rust not to treat those specific pieces of code as dead unless they are actually dead in all circumstances.
Edit: I do know that in this particular case, I could lock this behind a target too, but I've had cases where I had to do this for every function in a file as it caused problems with breaking imports if I put a file wide lock.