diff --git a/CHANGELOG.md b/CHANGELOG.md index a2ace6589a..15cd7d4446 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to the library should be put here ## Unreleased +## 0.3.1 + +### Fixes + +- Fix compiler complaining about alloc crate when `std` feature is active while using a macro + ## 0.3.0 ### Changes diff --git a/Cargo.toml b/Cargo.toml index 98c020ba89..8d1584292f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "janetrs" -version = "0.3.0" +version = "0.3.1" authors = ["Eric Shimizu Karbstein "] description = "High level binding for Janet programming language" repository = "https://github.com/GrayJack/janetrs" diff --git a/src/macros.rs b/src/macros.rs index 1705606320..6f79027223 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -265,6 +265,7 @@ macro_rules! janet_mod { /// jpanic!(4); // In simple cases you can use any type that Janet implements From trait /// jpanic!("this is a {} {message}", "fancy", message = "message"); /// ``` +#[cfg(not(feature = "std"))] #[macro_export] macro_rules! jpanic { () => { @@ -274,7 +275,34 @@ macro_rules! jpanic { $crate::util::_panic($crate::Janet::from($msg)); }; ($msg:expr, $($arg:tt)+) => { - $crate::util::_panic($crate::Janet::from(alloc::format!($msg, $($arg)+).as_str())); + $crate::util::_panic($crate::Janet::from(::alloc::format!($msg, $($arg)+).as_str())); + }; +} + +/// Causes a panic in the Janet side. Diferently of the Rust `panic!` macro, this macro +/// does **not** terminate the current thread. Instead, it sends a error signal with the +/// passed message where the Janet runtime takes care to properly exit. +/// +/// # Examples +/// ```ignore +/// use janetrs::jpanic; +/// # let _client = janetrs::client::JanetClient::init().unwrap(); +/// jpanic!(); +/// jpanic!("this is a terrible mistake!"); +/// jpanic!(4); // In simple cases you can use any type that Janet implements From trait +/// jpanic!("this is a {} {message}", "fancy", message = "message"); +/// ``` +#[cfg(feature = "std")] +#[macro_export] +macro_rules! jpanic { + () => { + $crate::util::_panic($crate::Janet::from("explicity panic")); + }; + ($msg:expr $(,)?) => { + $crate::util::_panic($crate::Janet::from($msg)); + }; + ($msg:expr, $($arg:tt)+) => { + $crate::util::_panic($crate::Janet::from(::std::format!($msg, $($arg)+).as_str())); }; } diff --git a/src/util.rs b/src/util.rs index 17c283e6fb..c27378fea1 100644 --- a/src/util.rs +++ b/src/util.rs @@ -66,7 +66,10 @@ pub fn c_func( doc: Option<&str>, ) { if let Some(prefix) = namespace { - let full_name = alloc::format!("{}/{}", prefix.trim(), fn_name.trim()); + #[cfg(feature = "std")] + let full_name = ::std::format!("{}/{}", prefix.trim(), fn_name.trim()); + #[cfg(not(feature = "std"))] + let full_name = ::alloc::format!("{}/{}", prefix.trim(), fn_name.trim()); let mut cfun = JanetTable::with_capacity(2); cfun.insert(JanetKeyword::new("value"), f);