1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use crate::{builtins::PyBaseExceptionRef, PyObjectRef, PyResult, VirtualMachine};

/// Implemented by any type that can be returned from a built-in Python function.
///
/// `ToPyObject` has a blanket implementation for any built-in object payload,
/// and should be implemented by many primitive Rust types, allowing a built-in
/// function to simply return a `bool` or a `usize` for example.
pub trait ToPyObject {
    fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef;
}

pub trait ToPyResult {
    fn to_pyresult(self, vm: &VirtualMachine) -> PyResult;
}

pub trait ToPyException {
    fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef;
}

pub trait IntoPyException {
    fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef;
}

impl<T> IntoPyException for &'_ T
where
    T: ToPyException,
{
    fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
        self.to_pyexception(vm)
    }
}