Skip to content

Commit

Permalink
Support r# escape syntax in Rust function names in rust_source()
Browse files Browse the repository at this point in the history
…family of functions (#374)

* Add test to reproduce the issue

* Update regex

* Cleanup
  • Loading branch information
Ilia-Kosenkov authored Aug 4, 2024
1 parent c456fe1 commit c4f5b0c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion R/find_exports.R
Original file line number Diff line number Diff line change
Expand Up @@ -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>fn)|(?<impl>impl)(?:\\s*<(?<lifetime>.+?)>)?)\\s+(?<name>(?:r#)?(?:_\\w+|[A-z]\\w*))"
) %>%
tibble::as_tibble(.name_repair = "minimal") %>%
rlang::set_names(c("match", "fn", "impl", "lifetime", "name")) %>%
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-source.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

0 comments on commit c4f5b0c

Please sign in to comment.