How come the 'transparent' option is not available when using the pyclass macro? #4956
-
Mostly my question revolves around the good old "am I missing something?", I am not very experienced. Thisis an enum with a few unit variants, all of them unit enums. I started out with the following, but ran into the issues that (correctly) the type of those variants is
Now, this works fine. But I do not know what I am missing between the switch from pyclass to simply using the IntoPyObject trait.
Now, how, forgive me if I do not know about the differences in the macros - is the lower one basically doing everything similarly? I take it the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The two macros are quite different. In this case the former creates a class hierarchy with a base class class Cuisine:
Italian: Italian = ...
...
class Italian(Cuisine):
... Each variant class then contains the fields of the variant. The impl<'py> IntoPyObject<'py> for Cuisine {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
match self {
Cuisine::Italian(arg) => {
Ok(arg.into_pyobject(py)?.into_any()) // you will get what ever `PastaType` turns into
}
...
}
}
} |
Beta Was this translation helpful? Give feedback.
The two macros are quite different.
#[pyclass]
does a lot more than simply derivingFromPyObject
andIntoPyObject
.In this case the former creates a class hierarchy with a base class
Cuisine
and "child" classes for each variant inheriting from it.Each variant class then contains the fields of the variant.
The
#[derive(IntoPyObject)]
will not define any class at all. In your example it will simply match on the enum and forward to theIntoPyObject
impl of the inner type, something like the following.