Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macros: simpler expansion for intern! #2413

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions src/once_cell.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! A write-once cell mediated by the Python GIL.
use crate::Python;
use crate::{types::PyString, Py, Python};
use std::cell::UnsafeCell;

/// A write-once cell similar to [`once_cell::OnceCell`](https://docs.rs/once_cell/1.4.0/once_cell/).
Expand Down Expand Up @@ -151,24 +151,28 @@ impl<T> GILOnceCell<T> {
#[macro_export]
macro_rules! intern {
($py: expr, $text: expr) => {{
fn isolate_from_dyn_env(py: $crate::Python<'_>) -> &$crate::types::PyString {
static INTERNED: $crate::once_cell::GILOnceCell<$crate::Py<$crate::types::PyString>> =
$crate::once_cell::GILOnceCell::new();

INTERNED
.get_or_init(py, || {
$crate::conversion::IntoPy::into_py(
$crate::types::PyString::intern(py, $text),
py,
)
})
.as_ref(py)
}

isolate_from_dyn_env($py)
static INTERNED: $crate::once_cell::Interned = $crate::once_cell::Interned::new($text);
INTERNED.get($py)
}};
}

/// Implementation detail for `intern!` macro.
#[doc(hidden)]
pub struct Interned(&'static str, GILOnceCell<Py<PyString>>);

impl Interned {
pub const fn new(value: &'static str) -> Self {
Interned(value, GILOnceCell::new())
}

#[inline]
pub fn get<'py>(&'py self, py: Python<'py>) -> &'py PyString {
self.1
.get_or_init(py, || PyString::intern(py, self.0).into())
.as_ref(py)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions tests/test_compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ fn _test_compile_errors() {

#[rustversion::since(1.60)]
fn tests_rust_1_60(t: &trybuild::TestCases) {
t.compile_fail("tests/ui/invalid_intern_arg.rs");
t.compile_fail("tests/ui/invalid_immutable_pyclass_borrow.rs");
t.compile_fail("tests/ui/invalid_pymethod_receiver.rs");
t.compile_fail("tests/ui/missing_intopy.rs");
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/invalid_intern_arg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use pyo3::Python;

fn main() {
let foo = if true { "foo" } else { "bar" };
Python::with_gil(|py| py.import(pyo3::intern!(py, foo)).unwrap());
}
8 changes: 8 additions & 0 deletions tests/ui/invalid_intern_arg.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error[E0435]: attempt to use a non-constant value in a constant
--> tests/ui/invalid_intern_arg.rs:5:55
|
5 | Python::with_gil(|py| py.import(pyo3::intern!(py, foo)).unwrap());
| ------------------^^^-
| | |
| | non-constant value
| help: consider using `let` instead of `static`: `let INTERNED`