From c4f5b0cea27f144edc2544bbfed1f35f505c3aa8 Mon Sep 17 00:00:00 2001 From: Ilia Kosenkov Date: Sun, 4 Aug 2024 19:49:03 +0300 Subject: [PATCH] Support `r#` escape syntax in Rust function names in `rust_source()` family of functions (#374) * Add test to reproduce the issue * Update regex * Cleanup --- NEWS.md | 1 + R/find_exports.R | 2 +- tests/testthat/test-source.R | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 47c6227d..fece6fd9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -13,6 +13,7 @@ * Support `RTOOLS44` (#347) * Removed `use_try_from` as an option in `rust_function`, and added `use_rng` (#354) * Added `use_crate()` function to make adding dependencies to Cargo.toml easier within R, similar to `usethis::use_package()` (#361) +* Fixed an issue in `rust_source()` family of functions that prevented usage of `r#` escape sequences in Rust function names (#374) # rextend 0.3.1 diff --git a/R/find_exports.R b/R/find_exports.R index 52a67684..71727e10 100644 --- a/R/find_exports.R +++ b/R/find_exports.R @@ -25,7 +25,7 @@ extract_meta <- function(lns) { # Matches fn|impl<'a> item_name result <- stringi::stri_match_first_regex( glue_collapse(lns, sep = "\n"), - "(?:(fn)|(impl)(?:\\s*<(.+?)>)?)\\s+(_\\w+|[A-z]\\w*)" + "(?:(?fn)|(?impl)(?:\\s*<(?.+?)>)?)\\s+(?(?:r#)?(?:_\\w+|[A-z]\\w*))" ) %>% tibble::as_tibble(.name_repair = "minimal") %>% rlang::set_names(c("match", "fn", "impl", "lifetime", "name")) %>% diff --git a/tests/testthat/test-source.R b/tests/testthat/test-source.R index 1eaaa975..3dde7da4 100644 --- a/tests/testthat/test-source.R +++ b/tests/testthat/test-source.R @@ -130,3 +130,20 @@ test_that("`rust_source()` should not raise internal error for code without exte expect_no_error(rust_source(code = "fn test() {}")) }) + +# https://github.com/extendr/rextendr/issues/356 +test_that("`rust_function()` supports `r#` prefix in rust function names", { + skip_if_cargo_unavailable() + + rust_fn_src <- " + fn r#true() -> &'static str { + \"Specially-named function has been called\" + } + " + + rust_function( + code = rust_fn_src + ) + + expect_equal(true(), "Specially-named function has been called") +})