Skip to content

Commit

Permalink
Fix compiler error when using a macro with format! inside it.
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayJack committed Jun 2, 2021
1 parent 524730e commit c9ce49c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "janetrs"
version = "0.3.0"
version = "0.3.1"
authors = ["Eric Shimizu Karbstein <gr41.j4ck@gmail.com>"]
description = "High level binding for Janet programming language"
repository = "https://github.com/GrayJack/janetrs"
Expand Down
30 changes: 29 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
() => {
Expand All @@ -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()));
};
}

Expand Down
5 changes: 4 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c9ce49c

Please sign in to comment.