I think it should be easily accomplished after https://github.com/PyO3/pyo3/pull/3611 is resolved Usage example: https://github.com/PyO3/pyo3/pull/3611/files#diff-84422521ed1ffead90ff986cc098b4b6dca6f898087c2824650bf8865d0584dcR10-R13