Skip to content

Commit

Permalink
Option<NonZeroU64> for msg_deadline
Browse files Browse the repository at this point in the history
  • Loading branch information
lwshang committed Jan 13, 2025
1 parent 8af817e commit 985c361
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
10 changes: 7 additions & 3 deletions e2e-tests/src/bin/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn call_msg_caller() {

/// This entrypoint will call [`call_msg_deadline`] with both best-effort and guaranteed responses.
#[ic_cdk::update]
async fn call_msg_dealine_caller() {
async fn call_msg_deadline_caller() {
use ic_cdk::call::{Call, SendableCall};
// Call with best-effort responses.
let reply1 = Call::new(canister_self(), "call_msg_deadline")
Expand All @@ -35,13 +35,17 @@ async fn call_msg_dealine_caller() {
assert_eq!(reply1, vec![0]);
}

/// This entrypoint is to be called by [`call_msg_dealine_caller`].
/// This entrypoint is to be called by [`call_msg_deadline_caller`].
/// If the call was made with best-effort responses, `msg_deadline` should be `Some`, then return 1.
/// If the call was made with guaranteed responses, `msg_deadline` should be `None`, then return 0.
#[export_name = "canister_update call_msg_deadline"]
fn call_msg_deadline() {
let reply = match msg_deadline() {
Some(_) => 1,
Some(v) => {
// `NonZeroU64::get()` converts the value to `u64`.
assert!(v.get() > 1);
1
}
None => 0,
};
msg_reply(vec![reply]);
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn call_api() {
.unwrap();
assert_eq!(res, WasmResult::Reply(vec![]));
let res = pic
.update_call(canister_id, sender, "call_msg_dealine_caller", vec![])
.update_call(canister_id, sender, "call_msg_deadline_caller", vec![])
.unwrap();
// Unlike the other entry points, `call_msg_dealine_caller` was implemented with the `#[update]` macro.
// So it returns the bytes of the Candid value `()` which is not the vec![]`.
Expand Down
16 changes: 12 additions & 4 deletions ic-cdk/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! For example, [`msg_arg_data`] wraps both `ic0::msg_arg_data_size` and `ic0::msg_arg_data_copy`.
use candid::Principal;
use std::convert::TryFrom;
use std::{convert::TryFrom, num::NonZeroU64};

pub mod call;
pub mod management_canister;
Expand Down Expand Up @@ -88,18 +88,26 @@ pub fn msg_reject_msg() -> String {
/// For calls to update methods with best-effort responses and their callbacks,
/// the deadline is computed based on the time the call was made,
/// and the `timeout_seconds` parameter provided by the caller.
/// In such cases, the deadline value wrapped in `Some` is returned.
/// In such cases, the deadline value will be converted to `NonZeroU64` and wrapped in `Some`.
/// To get the deadline value as a `u64`, call `get()` on the `NonZeroU64` value.
///
/// ```rust,no_run
/// use ic_cdk::api::msg_deadline;
/// if let Some(deadline) = msg_deadline() {
/// let deadline_value : u64 = deadline.get();
/// }
/// ```
///
/// For other calls (ingress messages and all calls to query and composite query methods,
/// including calls in replicated mode), a `None` is returned.
/// Please note that the raw `msg_deadline` system API returns 0 in such cases.
/// This function is a wrapper around the raw system API that provides more semantic information through the return type.
pub fn msg_deadline() -> Option<u64> {
pub fn msg_deadline() -> Option<NonZeroU64> {
// SAFETY: ic0.msg_deadline is always safe to call.
let nano_seconds = unsafe { ic0::msg_deadline() };
match nano_seconds {
0 => None,
_ => Some(nano_seconds),
_ => Some(NonZeroU64::new(nano_seconds).unwrap()),
}
}

Expand Down

0 comments on commit 985c361

Please sign in to comment.