-
Notifications
You must be signed in to change notification settings - Fork 805
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
make GILOnceCell::get_or_try_init_type_ref
public
#4542
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Change `GILOnceCell::get_or_try_init_type_ref` to public. |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -218,7 +218,35 @@ impl GILOnceCell<Py<PyType>> { | |||||||||||
/// Get a reference to the contained Python type, initializing it if needed. | ||||||||||||
/// | ||||||||||||
/// This is a shorthand method for `get_or_init` which imports the type from Python on init. | ||||||||||||
pub(crate) fn get_or_try_init_type_ref<'py>( | ||||||||||||
/// | ||||||||||||
/// # Example: Using `GILOnceCell` to avoid the overhead of importing a class multiple times | ||||||||||||
/// | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to just say what the example does, this makes for nicer URLs as well.
Suggested change
(this also explicitly mentions "static", which is important. We don't want people to have local (or worse, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done: 5d95fba |
||||||||||||
/// ``` | ||||||||||||
/// # use pyo3::prelude::*; | ||||||||||||
/// # use pyo3::sync::GILOnceCell; | ||||||||||||
/// # use pyo3::types::{PyDict, PyType}; | ||||||||||||
/// # use pyo3::intern; | ||||||||||||
/// # | ||||||||||||
/// #[pyfunction] | ||||||||||||
/// fn create_ordered_dict<'py>(py: Python<'py>, dict: Bound<'py, PyDict>) -> PyResult<Bound<'py, PyAny>> { | ||||||||||||
/// // Even if this function is called multiple times, | ||||||||||||
/// // the `OrderedDict` class will be imported only once. | ||||||||||||
/// static ORDERED_DICT: GILOnceCell<Py<PyType>> = GILOnceCell::new(); | ||||||||||||
/// ORDERED_DICT | ||||||||||||
/// .get_or_try_init_type_ref(py, "collections", "OrderedDict")? | ||||||||||||
/// .call1((dict,)) | ||||||||||||
/// } | ||||||||||||
/// | ||||||||||||
/// # Python::with_gil(|py| { | ||||||||||||
/// # let dict = PyDict::new(py); | ||||||||||||
/// # dict.set_item(intern!(py, "foo"), 42).unwrap(); | ||||||||||||
/// # let fun = wrap_pyfunction!(create_ordered_dict, py).unwrap(); | ||||||||||||
/// # let ordered_dict = fun.call1((&dict,)).unwrap(); | ||||||||||||
/// # assert!(dict.eq(ordered_dict).unwrap()); | ||||||||||||
/// # }); | ||||||||||||
/// ``` | ||||||||||||
/// | ||||||||||||
pub fn get_or_try_init_type_ref<'py>( | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can use a better name. Maybe just "import"? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, but then maybe it should be implemented for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be fine with that. I think you want There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, done in 79568f6! |
||||||||||||
&self, | ||||||||||||
py: Python<'py>, | ||||||||||||
module_name: &str, | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clarify that "it" refers to the GILOnceCell, not the class in it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! bcb883c