Skip to main content

Ungil

Trait Ungil 

Source
pub unsafe trait Ungil { }
Expand description

Types that are safe to access while the GIL is not held.

§Safety

The type must not carry borrowed Python references or, if it does, not allow access to them if the GIL is not held.

See the module-level documentation for more information.

§Examples

This tracking is currently imprecise as it relies on the Send auto trait on stable Rust. For example, an Rc smart pointer should be usable without the GIL, but we currently prevent that:

use alloc::rc::Rc;

Python::attach(|py| {
    let rc = Rc::new(42);

    py.detach(|| {
        println!("{:?}", rc);
    });
});

This also implies that the interplay between attach and detach is unsound, for example one can circumvent this protection using the send_wrapper crate:

use send_wrapper::SendWrapper;

Python::attach(|py| {
    let string = PyString::new(py, "foo");

    let wrapped = SendWrapper::new(string);

    py.detach(|| {
        let sneaky: &Bound<'_, PyString> = &*wrapped;

        println!("{:?}", sneaky);
    });
});

Fixing this loophole on stable Rust has significant ergonomic issues, but it is fixed when using nightly Rust and the nightly feature, c.f. #2141.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T: Send> Ungil for T

⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here