Skip to content

Commit 7adf37c

Browse files
chore: Factor out v2 executor into separate crate (#603)
1 parent 929fd0b commit 7adf37c

File tree

8 files changed

+37
-8
lines changed

8 files changed

+37
-8
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ opt-level = 'z'
2222
[workspace.dependencies]
2323
ic0 = { path = "src/ic0", version = "0.23.0" }
2424
ic-cdk = { path = "src/ic-cdk", version = "0.17.1" }
25+
ic-cdk-executor = { path = "src/ic-cdk-executor", version = "0.1.0" }
2526
ic-cdk-timers = { path = "src/ic-cdk-timers", version = "0.11.0" }
2627

2728
candid = "0.10.4"

src/ic-cdk-executor/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "ic-cdk-executor"
3+
version = "0.1.0"
4+
authors.workspace = true
5+
edition.workspace = true
6+
repository.workspace = true
7+
rust-version.workspace = true
8+
license.workspace = true
9+
description = "Async executor for `ic-cdk`"
10+
categories = ["asynchronous", "rust-patterns"]
11+
keywords = ["internet-computer", "dfinity", "canister", "cdk"]
12+
# DO NOT change this field, not even to update the link, unless you are updating every existing version of the package.
13+
# Update the link by updating the links-pin tag.
14+
links = "ic-cdk async executor, see https://github.com/dfinity/cdk-rs/blob/links-pin/TROUBLESHOOTING.md"
15+
16+
[dependencies]

src/ic-cdk-executor/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
// dummy build script, required by `package.links`
3+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! An async executor for [`ic-cdk`](https://docs.rs/ic-cdk). Most users should not use this crate directly.
2+
13
use std::cell::{Cell, RefCell};
24
use std::future::Future;
35
use std::pin::Pin;
@@ -26,7 +28,9 @@ pub fn spawn<F: 'static + Future<Output = ()>>(future: F) {
2628
.poll(&mut Context::from_waker(&waker));
2729
}
2830

29-
pub(crate) static CLEANUP: AtomicBool = AtomicBool::new(false);
31+
/// In a cleanup callback, this is set to `true` before calling `wake`, and `false` afterwards.
32+
/// This ensures that `wake` will not actually run the future, but instead cancel it and run its destructor.
33+
pub static CLEANUP: AtomicBool = AtomicBool::new(false);
3034

3135
// This module contains the implementation of a waker we're using for waking
3236
// top-level futures (the ones returned by canister methods). Rc handles the
@@ -88,7 +92,7 @@ mod waker {
8892
if super::CLEANUP.load(Ordering::Relaxed) {
8993
state.previous_trap.set(true);
9094
} else if state.previous_trap.get() {
91-
crate::trap("Call already trapped");
95+
panic!("Call already trapped");
9296
} else {
9397
let waker = waker(Rc::clone(&state));
9498
let Ok(mut borrow) = state.future.try_borrow_mut() else {

src/ic-cdk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ include = ["src", "Cargo.toml", "LICENSE", "README.md"]
2222
[dependencies]
2323
candid.workspace = true
2424
ic0.workspace = true
25+
ic-cdk-executor.workspace = true
2526
# Pin ic-cdk-macros to a specific version.
2627
# This actually create a 1-to-1 mapping between ic-cdk and ic-cdk-macros.
2728
# Dependents won't accidentaly upgrading ic-cdk-macros only but not ic-cdk.

src/ic-cdk/src/api/call.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ unsafe extern "C" fn cleanup<T: AsRef<[u8]>>(state_ptr: *const RwLock<CallFuture
182182
if let Some(waker) = w {
183183
// Flag that we do not want to actually wake the task - we
184184
// want to drop it *without* executing it.
185-
crate::futures::CLEANUP.store(true, Ordering::Relaxed);
185+
ic_cdk_executor::CLEANUP.store(true, Ordering::Relaxed);
186186
waker.wake();
187-
crate::futures::CLEANUP.store(false, Ordering::Relaxed);
187+
ic_cdk_executor::CLEANUP.store(false, Ordering::Relaxed);
188188
}
189189
}
190190
}
@@ -873,5 +873,5 @@ where
873873
/// [std::thread::panicking] - it tells you whether the destructor is executing *because* of a trap,
874874
/// as opposed to just because the scope was exited, so you could e.g. implement mutex poisoning.
875875
pub fn is_recovering_from_trap() -> bool {
876-
crate::futures::CLEANUP.load(Ordering::Relaxed)
876+
ic_cdk_executor::CLEANUP.load(Ordering::Relaxed)
877877
}

src/ic-cdk/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
compile_error!("This version of the CDK does not support multithreading.");
1414

1515
pub mod api;
16-
mod futures;
1716
mod macros;
1817
mod printer;
1918
pub mod storage;
@@ -45,13 +44,13 @@ pub fn setup() {
4544
note = "Use the spawn() function instead, it does the same thing but is more appropriately named."
4645
)]
4746
pub fn block_on<F: 'static + std::future::Future<Output = ()>>(future: F) {
48-
futures::spawn(future);
47+
ic_cdk_executor::spawn(future);
4948
}
5049

5150
/// Spawn an asynchronous task that drives the provided future to
5251
/// completion.
5352
pub fn spawn<F: 'static + std::future::Future<Output = ()>>(future: F) {
54-
futures::spawn(future);
53+
ic_cdk_executor::spawn(future);
5554
}
5655

5756
/// Format and then print the formatted message

0 commit comments

Comments
 (0)