You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to make 3rd party rust types available to the VM #5459
I'm building a little data processing application, in which I'd like be able to add data transformations that are written in python but will be executed by rust under the hood. Eventually I want provide a polars dataframe, and modify it via python.
I'm already hitting a road block getting the dataframe in and out of the VM. I tried to build on top of the call_between_rust_and_python example, but it only works with types that are already supported by rustpython or on simple types that are composed of supported types.
I'd be very happy if you'd nugde me into the right direction, given if it's actually possible what I'm trying to achieve.
Still RustStruct in the call_between_rust_and_python is the best option. You have to wrap a rust type to make a python type.
The inner rust type can be literally any type, though they requires different wrapping strategy.
Please share the details how you are blocked by building on top of call_between_rust_and_python. Then I could understand the situation better.
I can't implement the TryFromBorrowedObject trait for MyLazyFrame as LazyFrame doesn't implement TryFromBorrowedObject, and I can't implement the trait myself as it's from an external crate.
See the comment in the snippet.
use polars::prelude::*;#[pyattr]#[pyclass(module = "rust_py_module", name = "LazyFrame")]#[derive(PyPayload)]pubstructMyLazyFrame{publazy_frame:LazyFrame,}#[pyclass]implMyLazyFrame{#[pygetset]fnselect(self,columns:Vec<String>) -> self{let cols = columns.iter().map(|c| col(c)).collect();MyLazyFrame{lazy_frame:self.lazy_frame.select(cols),}}}impl<'a>TryFromBorrowedObject<'a>forMyLazyFrame{fntry_from_borrowed_object(vm:&VirtualMachine,obj:&'aPyObject) -> PyResult<Self>{// this doesn't work as Trait `TryFromBorrowedObject` is not implemented for `LazyFrame` let lazy_frame = obj.get_attr("lazy_frame", vm)?.try_into_value::<LazyFrame>(vm)?;Ok(MyLazyFrame{ lazy_frame })}}
LazyFrame is not a Python type and only MyLazyFrame is. You can neither do obj.get_attr("lazy_frame", vm) nor try_into_value::<LazyFrame>(vm).
I just realized the example is confusing because python code and rust code shares same field name.
This is probably the flow you are looking for.
// MyLazyFrame is already a Python type. You don't need any conversion.impl<'a>TryFromObject<'a>forLazyFrame{// the `obj` here must be a `MyLazyFrame` object.fntry_from_object(vm:&VirtualMachine,obj:PyObjectRef) -> PyResult<Self>{let frame:PyRef<MyLazyFrame> = obj.try_to_value::<MyLazyFrame>(vm)?;Ok(frame.lazy_frame.clone())}}
If you don't need to retrieve a LazyFrame object directly, you don't need to implement it. downcast, try_to_value, try_to_ref will work for MyLazyFrame without additional work.
Activity
youknowone commentedon Dec 20, 2024
Still RustStruct in the call_between_rust_and_python is the best option. You have to wrap a rust type to make a python type.
The inner rust type can be literally any type, though they requires different wrapping strategy.
Please share the details how you are blocked by building on top of
call_between_rust_and_python. Then I could understand the situation better.directindex commentedon Dec 20, 2024
Thanks for getting back.
I can't implement the
TryFromBorrowedObjecttrait forMyLazyFrameasLazyFramedoesn't implementTryFromBorrowedObject, and I can't implement the trait myself as it's from an external crate.See the comment in the snippet.
youknowone commentedon Jan 9, 2025
LazyFrame is not a Python type and only MyLazyFrame is. You can neither do
obj.get_attr("lazy_frame", vm)nortry_into_value::<LazyFrame>(vm).I just realized the example is confusing because python code and rust code shares same field name.
This is probably the flow you are looking for.
If you don't need to retrieve a LazyFrame object directly, you don't need to implement it.
downcast,try_to_value,try_to_refwill work forMyLazyFramewithout additional work.