Skip to content

Commit

Permalink
Add some notes about struct with lifetimes (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
yutannihilation authored Jul 29, 2024
1 parent 08ed0f8 commit 11979d8
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions book/src/struct.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,26 @@ Of course, this is an expert-only usage and is rarely needed. Again, you should
almost always use `&T` or `&mut T` instead of `T`. If you are really sure it
doesn't work well, you can use `T`.

## Lifetime

`#[savvy]` macro doesn't support a struct with lifetimes. This is because
crossing the boundary of FFI means losing the track of the lifetimes.

For example, the struct below contains a reference to a variable of `usize`.
However, once an instance of `Foo` is passed to R's side, Rust cannot know
whether the variable is still alive when `Foo` is passed back to Rust's side.

```rust
struct Foo<'a>(&'a usize)
```

Then, what should we do to deal with such structs? I'm yet to find the best
practices, but you might be able to

* use `'static` lifetime (i.e. `struct Foo(&'static usize)`) probably by
referencing a global variable
* instead of passing the struct itself to R, store the struct in a global
`OnceCell<HashMap>` and pass the key

## External pointer?

Expand Down

0 comments on commit 11979d8

Please sign in to comment.