Skip to content

Commit c9ce49c

Browse files
committed
Fix compiler error when using a macro with format! inside it.
1 parent 524730e commit c9ce49c

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to the library should be put here
44

55
## Unreleased
66

7+
## 0.3.1
8+
9+
### Fixes
10+
11+
- Fix compiler complaining about alloc crate when `std` feature is active while using a macro
12+
713
## 0.3.0
814

915
### Changes

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "janetrs"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
authors = ["Eric Shimizu Karbstein <gr41.j4ck@gmail.com>"]
55
description = "High level binding for Janet programming language"
66
repository = "https://github.com/GrayJack/janetrs"

src/macros.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ macro_rules! janet_mod {
265265
/// jpanic!(4); // In simple cases you can use any type that Janet implements From trait
266266
/// jpanic!("this is a {} {message}", "fancy", message = "message");
267267
/// ```
268+
#[cfg(not(feature = "std"))]
268269
#[macro_export]
269270
macro_rules! jpanic {
270271
() => {
@@ -274,7 +275,34 @@ macro_rules! jpanic {
274275
$crate::util::_panic($crate::Janet::from($msg));
275276
};
276277
($msg:expr, $($arg:tt)+) => {
277-
$crate::util::_panic($crate::Janet::from(alloc::format!($msg, $($arg)+).as_str()));
278+
$crate::util::_panic($crate::Janet::from(::alloc::format!($msg, $($arg)+).as_str()));
279+
};
280+
}
281+
282+
/// Causes a panic in the Janet side. Diferently of the Rust `panic!` macro, this macro
283+
/// does **not** terminate the current thread. Instead, it sends a error signal with the
284+
/// passed message where the Janet runtime takes care to properly exit.
285+
///
286+
/// # Examples
287+
/// ```ignore
288+
/// use janetrs::jpanic;
289+
/// # let _client = janetrs::client::JanetClient::init().unwrap();
290+
/// jpanic!();
291+
/// jpanic!("this is a terrible mistake!");
292+
/// jpanic!(4); // In simple cases you can use any type that Janet implements From trait
293+
/// jpanic!("this is a {} {message}", "fancy", message = "message");
294+
/// ```
295+
#[cfg(feature = "std")]
296+
#[macro_export]
297+
macro_rules! jpanic {
298+
() => {
299+
$crate::util::_panic($crate::Janet::from("explicity panic"));
300+
};
301+
($msg:expr $(,)?) => {
302+
$crate::util::_panic($crate::Janet::from($msg));
303+
};
304+
($msg:expr, $($arg:tt)+) => {
305+
$crate::util::_panic($crate::Janet::from(::std::format!($msg, $($arg)+).as_str()));
278306
};
279307
}
280308

src/util.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ pub fn c_func(
6666
doc: Option<&str>,
6767
) {
6868
if let Some(prefix) = namespace {
69-
let full_name = alloc::format!("{}/{}", prefix.trim(), fn_name.trim());
69+
#[cfg(feature = "std")]
70+
let full_name = ::std::format!("{}/{}", prefix.trim(), fn_name.trim());
71+
#[cfg(not(feature = "std"))]
72+
let full_name = ::alloc::format!("{}/{}", prefix.trim(), fn_name.trim());
7073
let mut cfun = JanetTable::with_capacity(2);
7174

7275
cfun.insert(JanetKeyword::new("value"), f);

0 commit comments

Comments
 (0)