|
19 | 19 | //! For example, [`msg_arg_data`] wraps both `ic0::msg_arg_data_size` and `ic0::msg_arg_data_copy`.
|
20 | 20 |
|
21 | 21 | use candid::Principal;
|
22 |
| -use std::convert::TryFrom; |
| 22 | +use std::{convert::TryFrom, num::NonZeroU64}; |
23 | 23 |
|
24 | 24 | pub mod call;
|
25 | 25 | pub mod management_canister;
|
@@ -83,6 +83,34 @@ pub fn msg_reject_msg() -> String {
|
83 | 83 | String::from_utf8_lossy(&bytes).into_owned()
|
84 | 84 | }
|
85 | 85 |
|
| 86 | +/// Gets the deadline, in nanoseconds since 1970-01-01, after which the caller might stop waiting for a response. |
| 87 | +/// |
| 88 | +/// For calls to update methods with best-effort responses and their callbacks, |
| 89 | +/// the deadline is computed based on the time the call was made, |
| 90 | +/// and the `timeout_seconds` parameter provided by the caller. |
| 91 | +/// In such cases, the deadline value will be converted to `NonZeroU64` and wrapped in `Some`. |
| 92 | +/// To get the deadline value as a `u64`, call `get()` on the `NonZeroU64` value. |
| 93 | +/// |
| 94 | +/// ```rust,no_run |
| 95 | +/// use ic_cdk::api::msg_deadline; |
| 96 | +/// if let Some(deadline) = msg_deadline() { |
| 97 | +/// let deadline_value : u64 = deadline.get(); |
| 98 | +/// } |
| 99 | +/// ``` |
| 100 | +/// |
| 101 | +/// For other calls (ingress messages and all calls to query and composite query methods, |
| 102 | +/// including calls in replicated mode), a `None` is returned. |
| 103 | +/// Please note that the raw `msg_deadline` system API returns 0 in such cases. |
| 104 | +/// This function is a wrapper around the raw system API that provides more semantic information through the return type. |
| 105 | +pub fn msg_deadline() -> Option<NonZeroU64> { |
| 106 | + // SAFETY: ic0.msg_deadline is always safe to call. |
| 107 | + let nano_seconds = unsafe { ic0::msg_deadline() }; |
| 108 | + match nano_seconds { |
| 109 | + 0 => None, |
| 110 | + _ => Some(NonZeroU64::new(nano_seconds).unwrap()), |
| 111 | + } |
| 112 | +} |
| 113 | + |
86 | 114 | /// Replies to the sender with the data.
|
87 | 115 | pub fn msg_reply<T: AsRef<[u8]>>(data: T) {
|
88 | 116 | let buf = data.as_ref();
|
|
0 commit comments