ITADN

Add wrapper for Rf_warning

#500Closedaquasync 创建于 2026-03-24
A
aquasynccommented
It'd be great to have a way to issue warnings, and it should also halt when warnings are escalated to errors (ie `options(warn=2)`). I initially tried: ``` call!("warning", format!("Warning test: {:?}", value)).unwrap(); ``` But that gives me two errors: ``` Error: (converted from warning) Warning test: String("test") Error in eval(expr, p) : User function panicked: testing ``` Then ended up trying to follow the wrapper for Rf_error: ``` extern "C" { pub fn Rf_warning(arg1: *const ::std::os::raw::c_char, ...); } static mut R_WARNING_BUF: Option<std::ffi::CString> = None; pub fn r_warning<S: AsRef<str>>(s: S) { let s = s.as_ref(); unsafe { R_MSG_BUF = Some(std::ffi::CString::new(s).unwrap()); let ptr = std::ptr::addr_of!(R_WARNING_BUF); Rf_warning((*ptr).as_ref().unwrap().as_ptr()); }; } ... r_warning(format!("Warning test: {:?}", value)); ``` And that seemed to work correctly. Not sure if this is the right approach, and whether there are any concerns about how this interacts with the rust side of things. I presume this goes through the existing unwind protect mechanism so should be fine? If so it'd be great for (something like) this to be in the ffi & api crates. Thanks!
关闭于 2026-03-24 1 条评论