Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/plans/apply.rs
8443 views
1
use std::fmt::{Debug, Formatter};
2
3
use polars_core::prelude::*;
4
5
pub trait DataFrameUdf: Send + Sync {
6
fn call_udf(&self, df: DataFrame) -> PolarsResult<DataFrame>;
7
fn display_str(&self) -> PlSmallStr {
8
PlSmallStr::from_static("dyn DataFrameUdf")
9
}
10
}
11
12
impl<F> DataFrameUdf for F
13
where
14
F: Fn(DataFrame) -> PolarsResult<DataFrame> + Send + Sync,
15
{
16
fn call_udf(&self, df: DataFrame) -> PolarsResult<DataFrame> {
17
self(df)
18
}
19
}
20
21
pub trait DataFrameUdfMut: Send + Sync {
22
fn call_udf(&mut self, df: DataFrame) -> PolarsResult<DataFrame>;
23
}
24
25
impl<F> DataFrameUdfMut for F
26
where
27
F: FnMut(DataFrame) -> PolarsResult<DataFrame> + Send + Sync,
28
{
29
fn call_udf(&mut self, df: DataFrame) -> PolarsResult<DataFrame> {
30
self(df)
31
}
32
}
33
34
impl Debug for dyn DataFrameUdf {
35
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
36
self.display_str().fmt(f)
37
}
38
}
39
impl Debug for dyn DataFrameUdfMut {
40
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41
write!(f, "dyn DataFrameUdfMut")
42
}
43
}
44
45
pub trait UdfSchema: Send + Sync {
46
fn get_schema(&self, input_schema: &Schema) -> PolarsResult<SchemaRef>;
47
}
48
49
impl<F> UdfSchema for F
50
where
51
F: Fn(&Schema) -> PolarsResult<SchemaRef> + Send + Sync,
52
{
53
fn get_schema(&self, input_schema: &Schema) -> PolarsResult<SchemaRef> {
54
self(input_schema)
55
}
56
}
57
58
impl Debug for dyn UdfSchema {
59
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
60
write!(f, "dyn UdfSchema")
61
}
62
}
63
64