polars_core/frame/column/
series.rs

1use std::ops::{Deref, DerefMut};
2
3use super::Series;
4
5/// A very thin wrapper around [`Series`] that represents a [`Column`]ized version of [`Series`].
6///
7/// At the moment this just conditionally tracks where it was created so that materialization
8/// problems can be tracked down.
9#[derive(Debug, Clone)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
12pub struct SeriesColumn {
13    inner: Series,
14
15    #[cfg(debug_assertions)]
16    #[cfg_attr(feature = "serde", serde(skip, default))]
17    materialized_at: Option<std::sync::Arc<std::backtrace::Backtrace>>,
18}
19
20impl SeriesColumn {
21    #[track_caller]
22    pub fn new(series: Series) -> Self {
23        Self {
24            inner: series,
25
26            #[cfg(debug_assertions)]
27            materialized_at: if std::env::var("POLARS_TRACK_SERIES_MATERIALIZATION").as_deref()
28                == Ok("1")
29            {
30                Some(std::sync::Arc::new(
31                    std::backtrace::Backtrace::force_capture(),
32                ))
33            } else {
34                None
35            },
36        }
37    }
38
39    pub fn materialized_at(&self) -> Option<&std::backtrace::Backtrace> {
40        #[cfg(debug_assertions)]
41        {
42            self.materialized_at.as_ref().map(|v| v.as_ref())
43        }
44
45        #[cfg(not(debug_assertions))]
46        None
47    }
48
49    pub fn take(self) -> Series {
50        self.inner
51    }
52}
53
54impl From<Series> for SeriesColumn {
55    #[track_caller]
56    #[inline(always)]
57    fn from(value: Series) -> Self {
58        Self::new(value)
59    }
60}
61
62impl Deref for SeriesColumn {
63    type Target = Series;
64
65    fn deref(&self) -> &Self::Target {
66        &self.inner
67    }
68}
69
70impl DerefMut for SeriesColumn {
71    fn deref_mut(&mut self) -> &mut Self::Target {
72        &mut self.inner
73    }
74}