kyren / gc-arena

Incremental garbage collection from safe Rust
Creative Commons Zero v1.0 Universal
436 stars 36 forks source link

How do I store a reference to a static lifetimed reference inside a GC'ed value? #99

Closed glyh closed 1 month ago

glyh commented 1 month ago

My use case is basically storing a constant &str inside a GC'ed tagged enum(which is also recursive).

I've read through the example but I didn't find something like this. I would appreciate it if you can provide some examples, thank you!

glyh commented 1 month ago

For now attempting this directly with rust lifetime params I got a "lifetime should be at least as long as 'static" error.

kyren commented 1 month ago

Generally you can't mix other non-'static lifetimes than 'gc into gc-arena, but it sorta depends. Am I understanding things correctly that you want to keep around a &'a &'static str or a &'a mut &'static str, and not just a &'static str?

glyh commented 1 month ago

I want to keep around a 'static inside a 'gc, I guess RefCell is the way to go?

I need this just for &static str, though. I guess for this case I can also use a &String?

kyren commented 1 month ago

You can keep around a &'static T inside the arena, there is no need for RefCell at all are you maybe missing a #[collect(require_static)] on the field? no never mind, this should work by default, &'static T always implements Collect so you shouldn't even need that.

If you can give me an example that doesn't compile I can probably provide more specific help.

glyh commented 1 month ago

This might be the way to go, I'll take a look at this. Thank you!

kyren commented 1 month ago

Ah, I may have told you incorrectly if you didn't see my edit, I'm sorry!

If you can't figure it out, feel free to follow up. If you can show me what error you're seeing and a code sample I can help.