Skip to main content

rewrite_stored_version_for_tests

Function rewrite_stored_version_for_tests 

Source
pub unsafe fn rewrite_stored_version_for_tests(sexp: SEXP, version: &str)
Expand description

Test support: make a restored pointer read as a save by another version of its crate, so a package can test its handling of miniextendr_restored_other_version (#1872) without building two versions.

It rewrites the version in the type ID a restored pointer keeps in prot[0] (crate@version::module::Type) to version. The pointer must have no address, as one restored by readRDS() / unserialize() has: a live pointer is checked by Any::downcast, not by the ID. Call it from a test-only #[miniextendr] function:

ⓘ
/// @param x A pointer restored by `unserialize()`.
#[miniextendr(noexport)]
pub fn rewrite_saved_version(x: SEXP, version: String) {
    // SAFETY: R's main thread; `x` is an external pointer.
    unsafe { miniextendr_api::externalptr::rewrite_stored_version_for_tests(x, &version) };
}
back <- unserialize(serialize(obj, NULL))
rewrite_saved_version(back, "0.0.1")
tryCatch(method(back), miniextendr_restored_other_version = function(e) e$saved_version)
#> [1] "0.0.1"

§Panics

When sexp is not a restored ExternalPtr-built pointer with a versioned type ID.

§Safety

Must be called from R’s main thread with a valid EXTPTRSXP.