fusion-engineering / inline-python

Inline Python code directly in your Rust code
https://docs.rs/inline-python
BSD 2-Clause "Simplified" License
1.15k stars 37 forks source link

Can I define Context as global #25

Closed hyousefGopher closed 4 years ago

hyousefGopher commented 4 years ago

It happened that I need to use the context in multiple Rust function, so I thought to define it as global one like this:

static c: Context = Context::new();

But I got the below error:

error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
  --> src\main.rs:20:21
   |
20 | static c: Context = Context::new();
   |                     ^^^^^^^^^^^^^^
de-vri-es commented 4 years ago

No, this isn't possible. Note that a Context holds the GIL, so with a static context it would never be released.

de-vri-es commented 4 years ago

Actually, that isn't true, it doesn't hold the GIL. You do need to acquire the GIL to create a context though, so this still isn't possible in a const fn.