Module extendr_api::wrapper::externalptr

source ·
Expand description

ExternalPtr is a way to leak Rust allocated data to R, forego deallocation to R and its GC strategy.

An ExternalPtr encompasses three values, an owned pointer to the Rust type, a tag and a prot. Tag is a helpful naming of the type, but it doesn’t offer any solid type-checking capability. And prot is meant to be R values, that are supposed to be kept together with the ExternalPtr.

Neither tag nor prot are attributes, therefore to use ExternalPtr as a class in R, you must decorate it with a class-attribute manually.

Beware: Equality (by way of PartialEq) does not imply equality of value, but equality of pointer. Two objects stored as ExternalPtr may be equal in value, but be two distinct entities, with distinct pointers.

Instead, rely on AsRef to make by value comparison, e.g. to compare for equality of two instances of ExternalPtr<T> by value, a.as_ref() == b.as_ref().


  • Wrapper for creating R objects containing any Rust object.