Skip to content

Commit

Permalink
refactor(rust): Remove unnecessary unsafe around warning function (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
orlp authored Jan 30, 2025
1 parent d68eb1a commit a823c23
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/polars-error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ description = "Error definitions for the Polars DataFrame library"
arrow-format = { workspace = true, optional = true }
avro-schema = { workspace = true, optional = true }
object_store = { workspace = true, optional = true }
parking_lot = { workspace = true }
regex = { workspace = true, optional = true }
simdutf8 = { workspace = true }

Expand Down
28 changes: 14 additions & 14 deletions crates/polars-error/src/warning.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
use parking_lot::RwLock;

type WarningFunction = fn(&str, PolarsWarning);
static mut WARNING_FUNCTION: Option<WarningFunction> = None;
static WARNING_FUNCTION: RwLock<WarningFunction> = RwLock::new(eprintln);

fn eprintln(fmt: &str, warning: PolarsWarning) {
eprintln!("{:?}: {}", warning, fmt);
}

/// Set the function that will be called by the `polars_warn!` macro.
/// You can use this to set logging in polars.
///
/// # Safety
/// The caller must ensure there is no other thread accessing this function
/// or calling `polars_warn!`.
pub unsafe fn set_warning_function(function: WarningFunction) {
WARNING_FUNCTION = Some(function)
pub fn set_warning_function(function: WarningFunction) {
*WARNING_FUNCTION.write() = function;
}

pub fn get_warning_function() -> WarningFunction {
*WARNING_FUNCTION.read()
}

#[derive(Debug)]
pub enum PolarsWarning {
Deprecation,
Expand All @@ -18,13 +25,6 @@ pub enum PolarsWarning {
MapWithoutReturnDtypeWarning,
}

fn eprintln(fmt: &str, warning: PolarsWarning) {
eprintln!("{:?}: {}", warning, fmt);
}

pub fn get_warning_function() -> WarningFunction {
unsafe { WARNING_FUNCTION.unwrap_or(eprintln) }
}
#[macro_export]
macro_rules! polars_warn {
($variant:ident, $fmt:literal $(, $arg:tt)*) => {
Expand Down

0 comments on commit a823c23

Please sign in to comment.