Function magnus::gc::register_address

source ·
pub fn register_address<T>(valref: &T)
where T: Mark,
Expand description

Inform Ruby’s garbage collector that valref points to a live Ruby object.

Prevents Ruby moving or collecting valref. This should be used on static items to prevent them being collected instead of relying on Ruby constants/globals to allways refrence the value.

See also BoxValue.

§Examples

use magnus::{gc, RString};

let s = RString::new("example");

// s won't be collected even though it's on the heap
let boxed = Box::new(s);
gc::register_address(&*boxed);

// ...

// allow s to be collected
gc::unregister_address(&*boxed);
drop(boxed);