1on_load <- function(expr, env = parent.frame()) {
2  ns <- topenv(env)
3  expr <- substitute(expr)
4  callback <- function() eval_bare(expr, env)
5  ns$.__rlang_hook__. <- c(ns$.__rlang_hook__., list(callback))
6}
7
8run_on_load <- function(env = parent.frame()) {
9  ns <- topenv(env)
10
11  hook <- ns$.__rlang_hook__.
12  env_unbind(ns, ".__rlang_hook__.")
13
14  for (callback in hook) {
15    callback()
16  }
17
18  ns$.__rlang_hook__. <- NULL
19}
20
21replace_from <- function(what, pkg, to = topenv(caller_env())) {
22  if (what %in% getNamespaceExports(pkg)) {
23    env <- ns_env(pkg)
24  } else {
25    env <- to
26  }
27  env_get(env, what, inherit = TRUE)
28}
29
30# nocov start
31
32# Useful for micro-optimising default arguments requiring evaluation,
33# such as `param = c("foo", "bar")`. Buys about 0.6us on my desktop.
34fn_inline_formals <- function(fn, names) {
35  stopifnot(typeof(fn) == "closure")
36
37  fmls <- formals(fn)
38  fmls[names] <- lapply(fmls[names], eval)
39
40  formals(fn) <- fmls
41  fn
42}
43
44# nocov end
45