rustpython_vm/convert/
transmute_from.rs

1use crate::{
2    object::{AsObject, PyObject, PyPayload, PyRef, PyResult},
3    vm::VirtualMachine,
4};
5
6/// Marks a type that has the exact same layout as PyObjectRef, e.g. a type that is
7/// `repr(transparent)` over PyObjectRef.
8///
9/// # Safety
10/// Can only be implemented for types that are `repr(transparent)` over a PyObjectRef `obj`,
11/// and logically valid so long as `check(vm, obj)` returns `Ok(())`
12pub unsafe trait TransmuteFromObject: Sized {
13    fn check(vm: &VirtualMachine, obj: &PyObject) -> PyResult<()>;
14}
15
16unsafe impl<T: PyPayload> TransmuteFromObject for PyRef<T> {
17    fn check(vm: &VirtualMachine, obj: &PyObject) -> PyResult<()> {
18        let class = T::class(&vm.ctx);
19        if obj.fast_isinstance(class) {
20            if obj.payload_is::<T>() {
21                Ok(())
22            } else {
23                Err(vm.new_downcast_runtime_error(class, obj))
24            }
25        } else {
26            Err(vm.new_downcast_type_error(class, obj))
27        }
28    }
29}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.