Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Rcpp dependency with cpp11 #109

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
^vignettes/future_promise/.*\.key$
^vignettes/future_promise/.*\.R$
^CRAN-RELEASE$
^\.vscode$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ inst/doc
doc
Meta
docs
.vscode
12 changes: 7 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
Type: Package
Package: promises
Title: Abstractions for Promise-Based Asynchronous Programming
Version: 1.2.1.9001
Version: 1.2.1.9002
Authors@R: c(
person("Joe", "Cheng", , "[email protected]", role = c("aut", "cre")),
person("Mauricio", "Vargas Sepulveda", role = "ctb",
comment = c(ORCID = "0000-0003-1017-7574")),
person("Posit Software, PBC", role = c("cph", "fnd"))
)
Description: Provides fundamental abstractions for doing asynchronous
Expand All @@ -17,11 +19,11 @@ URL: https://rstudio.github.io/promises/,
https://github.com/rstudio/promises
BugReports: https://github.com/rstudio/promises/issues
Imports:
cpp11,
fastmap (>= 1.1.0),
later,
magrittr (>= 1.5),
R6,
Rcpp,
rlang,
stats
Suggests:
Expand All @@ -33,12 +35,12 @@ Suggests:
testthat,
vembedr
LinkingTo:
later,
Rcpp
cpp11,
later
VignetteBuilder:
knitr
Config/Needs/website: rsconnect
Encoding: UTF-8
Language: en-US
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export(then)
export(with_promise_domain)
import(R6)
import(later)
importFrom(Rcpp,evalCpp)
importFrom(cpp11,cpp_eval)
importFrom(magrittr,"%>%")
importFrom(magrittr,"%T>%")
useDynLib(promises)
useDynLib(promises, .registration = TRUE)
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# promises (development version)

* `is.promising` is now an S3 method. (#104)

* Refactored to use the "cpp11" package for C++ code instead of "Rcpp". (@pachadotdev, #109)

# promises 1.2.1

Expand Down
7 changes: 0 additions & 7 deletions R/RcppExports.R

This file was deleted.

5 changes: 5 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated by cpp11: do not edit by hand

asyncFib <- function(resolve, reject, x) {
invisible(.Call(`_promises_asyncFib`, resolve, reject, x))
}
3 changes: 1 addition & 2 deletions R/promise.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#' @useDynLib promises
#' @importFrom Rcpp evalCpp
#' @importFrom cpp11 cpp_eval
#' @import later
NULL

Expand Down
6 changes: 1 addition & 5 deletions R/promises-package.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#' @keywords internal
#' @useDynLib promises, .registration = TRUE
"_PACKAGE"

## usethis namespace: start
## usethis namespace: end
NULL

ignore_unused_imports <- function() {
fastmap::fastqueue
}
Expand Down
4 changes: 4 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ Lopp
Multisession
NSE
OpenCPU
ORCID
PBC
RServe
RStudio
Shiny's
ShinyApps
Sepulveda
UI
anonymized
async
backoff
cloneable
conf
cpp
cranwhales
deserialization
deserialized
Expand Down Expand Up @@ -58,6 +61,7 @@ reactives
renderers
rstudio
runtime’s
Rcpp
scalability
scalable
shinydashboard
Expand Down
2 changes: 1 addition & 1 deletion man/promises-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.o
*.so
*.dll
34 changes: 0 additions & 34 deletions src/RcppExports.cpp

This file was deleted.

28 changes: 28 additions & 0 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Generated by cpp11: do not edit by hand
// clang-format off


#include "cpp11/declarations.hpp"
#include <R_ext/Visibility.h>

// promise_task.cpp
void asyncFib(cpp11::function resolve, cpp11::function reject, double x);
extern "C" SEXP _promises_asyncFib(SEXP resolve, SEXP reject, SEXP x) {
BEGIN_CPP11
asyncFib(cpp11::as_cpp<cpp11::decay_t<cpp11::function>>(resolve), cpp11::as_cpp<cpp11::decay_t<cpp11::function>>(reject), cpp11::as_cpp<cpp11::decay_t<double>>(x));
return R_NilValue;
END_CPP11
}

extern "C" {
static const R_CallMethodDef CallEntries[] = {
{"_promises_asyncFib", (DL_FUNC) &_promises_asyncFib, 3},
{NULL, NULL, 0}
};
}

extern "C" attribute_visible void R_init_promises(DllInfo* dll){
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
R_forceSymbols(dll, TRUE);
}
53 changes: 25 additions & 28 deletions src/promise_task.cpp
Original file line number Diff line number Diff line change
@@ -1,66 +1,63 @@
#include <Rcpp.h>
#include <later_api.h>

#include <cpp11.hpp>

class PromiseTask : public later::BackgroundTask {
public:
PromiseTask(Rcpp::Function resolve, Rcpp::Function reject) :
resolve(resolve), reject(reject) {
}
public:
PromiseTask(cpp11::function resolve, cpp11::function reject)
: resolve(resolve), reject(reject) {}

protected:
protected:
virtual void execute() = 0;
virtual Rcpp::RObject get_result() = 0;
virtual cpp11::sexp get_result() = 0;

void complete() {
Rcpp::RObject result = get_result();
cpp11::sexp result = get_result();
resolve(result);
}
private:
Rcpp::Function resolve;
Rcpp::Function reject;

private:
cpp11::function resolve;
cpp11::function reject;
};

long fib(long x) {
if (x <= 2) {
return 1;
}
return fib(x-1) + fib(x-2);
return fib(x - 1) + fib(x - 2);
}

class FibonacciTask : public PromiseTask {
public:
FibonacciTask(Rcpp::Function resolve, Rcpp::Function reject, double x) :
PromiseTask(resolve, reject), x(x) {
}
public:
FibonacciTask(cpp11::function resolve, cpp11::function reject, double x)
: PromiseTask(resolve, reject), x(x) {}

void execute() {
result = fib((long)x);
}
void execute() { result = fib((long)x); }

Rcpp::RObject get_result() {
Rcpp::NumericVector res(1);
cpp11::sexp get_result() {
cpp11::writable::doubles res(1);
res[0] = (double)result;
return res;
}

private:
private:
double x;
long result;
};

// [[Rcpp::export]]
void asyncFib(Rcpp::Function resolve, Rcpp::Function reject, double x) {
[[cpp11::register]] void asyncFib(cpp11::function resolve,
cpp11::function reject, double x) {
FibonacciTask* fib = new FibonacciTask(resolve, reject, x);
fib->begin();
}

/*** R
library(promises)
library(later)
library(Rcpp)
library(cpp11)

promise(function(resolve, reject) {
promise:::asyncFib(resolve, reject, 45)
cpp11::package("your_package_name")$asyncFib(resolve, reject, 45)
}) %...>% print()

*/
*/