Accessing the current `Robj` in the `impl`
- Dominant language
- Rust
- Stars
- 528
- Forks
- 58
- PR merge metrics
- No merged PRs in 30d
Description
Hi there, thank you for working on this project! One nice addition would be to be able to take as input the `RObj` instead of `self`.
In term of bindings, I am new to R and beginner in Rust, but I have used [PyBind11](https://pybind11.readthedocs.io/en/stable/index.html) a lot.
There we do
```cpp
struct Foo {
int bar() { return 33; }
};
py::class_("Foo")
// Function pointer
.def("bar", &Foo::bar)
// Or arbitrary lambda with a Foo
.def("bar", [](Foo const& self){ return self.bar(); })
// Or arbitrary lambda with a py::object / py::handle
.def("bar", [](py::object self) {
// Use arbitrary Python from C++
py::print(self)
// Use Foo in C++ (may throw exception)
Foo const& self_foo = py::cast(self);
return self_foo.bar();
})
```
From Python, any of these would be called via `Foo().bar()`.
I believe we can take parameters as `Robj` or `ExternalPrt` in Extendr, but that does not work for the object instance.
```rust
#[extendr]
struct Foo {
}
#[extendr]
impl Foo {
// Works normally
fn foo(&self) -> i32 { 33 }
// Error: argument "robj" is missing, with no default
fn foo(Robj robj) -> i32 { 33 }
}
```
I would like to be able to have something like this working. Either through function parameters, or through inner methods if that is easier.
My main motivation is to be able to keep a copy of `Robj` for returning objects that capture a reference to `Foo`, so that it is preserved from garbage collection.
For a fair comparison, PyBind11 has both `.def` and `.def_static` so it always know in the former that the first argument is always the object.
Here it would perhaps need some extra hint, like a `SelfRobj` type, or an function attribute `#[method]` (or param attribute `#[self]`) to force something to be a method even without a `self` param.
Contributor guide
Assessment
This issue has not been assessed yet.