Skip to main content

polars_config/
engine.rs

1use std::fmt;
2use std::str::FromStr;
3
4#[repr(u8)]
5#[derive(Clone, Debug, Copy, Eq, PartialEq, Hash)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub enum Engine {
8    Auto = 0,
9    Streaming = 1,
10    InMemory = 2,
11    Gpu = 3,
12}
13
14impl fmt::Display for Engine {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        write!(f, "{}", self.as_static_str())
17    }
18}
19
20impl FromStr for Engine {
21    type Err = String;
22
23    fn from_str(s: &str) -> Result<Self, Self::Err> {
24        match s {
25            "auto" => Ok(Engine::Auto),
26            // We keep "cpu" for backwards compatibility.
27            "cpu" | "in-memory" => Ok(Engine::InMemory),
28            "streaming" => Ok(Engine::Streaming),
29            "gpu" => Ok(Engine::Gpu),
30            "old-streaming" => Err("the 'old-streaming' engine has been removed".to_owned()),
31            v => Err(format!(
32                "`engine` must be one of {{'auto', 'in-memory', 'streaming', 'gpu'}}, got {v}",
33            )),
34        }
35    }
36}
37
38impl Engine {
39    pub(crate) fn from_discriminant(d: u8) -> Self {
40        match d {
41            0 => Self::Auto,
42            1 => Self::Streaming,
43            2 => Self::InMemory,
44            3 => Self::Gpu,
45            _ => unreachable!(),
46        }
47    }
48
49    pub fn as_static_str(&self) -> &'static str {
50        match self {
51            Self::Auto => "auto",
52            Self::Streaming => "streaming",
53            Self::InMemory => "in-memory",
54            Self::Gpu => "gpu",
55        }
56    }
57}