How to turn str | None
into a Rust Enum
#4143
dedebenui
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In python it's very common to give
str
to functions to toggle some features (like themode
inopen
). In Rust, those should probably be enums, but pyo3 cannot deriveFromPyObject
for enums with empty variants yet (#417). Here is how I do it:My main approach is to extract the string from the python object, and implement
TryFrom
for my enum for any type that can be dereferenced asstr
.To avoid repeating some code, I would have liked to write this last implementation block as
but I cannot write blanket implementations for traits that I didn't define.
Beta Was this translation helpful? Give feedback.
All reactions