1#![deny(missing_docs)]
9#![allow(async_fn_in_trait)]
10
11use std::fmt;
12
13#[doc(hidden)]
14pub use async_trait::async_trait;
15#[cfg(all(not(target_arch = "wasm32"), unix))]
16use tokio::signal::unix;
17#[cfg(not(target_arch = "wasm32"))]
18use {::tracing::debug, tokio_util::sync::CancellationToken};
19pub mod abi;
20#[cfg(not(target_arch = "wasm32"))]
21pub mod command;
22pub mod crypto;
23pub mod data_types;
24pub mod dyn_convert;
25mod graphql;
26pub mod hashed;
27pub mod http;
28pub mod identifiers;
29mod limited_writer;
30pub mod ownership;
31#[cfg(not(target_arch = "wasm32"))]
32pub mod port;
33#[cfg(with_metrics)]
34pub mod prometheus_util;
35#[cfg(not(chain))]
36pub mod task;
37pub mod vm;
38#[cfg(not(chain))]
39pub use task::Blocking;
40pub mod time;
41#[cfg_attr(web, path = "tracing_web.rs")]
42pub mod tracing;
43#[cfg(not(target_arch = "wasm32"))]
44pub mod tracing_opentelemetry;
45#[cfg(test)]
46mod unit_tests;
47
48pub use graphql::BcsHexParseError;
49#[doc(hidden)]
50pub use {async_graphql, bcs, hex};
51
52#[macro_export]
67macro_rules! ensure {
68 ($cond:expr, $e:expr) => {
69 if !($cond) {
70 return Err($e.into());
71 }
72 };
73}
74
75pub fn hex_debug<T: AsRef<[u8]>>(bytes: &T, f: &mut fmt::Formatter) -> fmt::Result {
109 const ELIDE_AFTER: usize = 16;
110 let bytes = bytes.as_ref();
111 if bytes.len() <= 2 * ELIDE_AFTER {
112 write!(f, "{}", hex::encode(bytes))?;
113 } else {
114 write!(
115 f,
116 "{}..{}",
117 hex::encode(&bytes[..ELIDE_AFTER]),
118 hex::encode(&bytes[(bytes.len() - ELIDE_AFTER)..])
119 )?;
120 }
121 Ok(())
122}
123
124#[expect(clippy::ptr_arg)] pub fn hex_vec_debug(list: &Vec<Vec<u8>>, f: &mut fmt::Formatter) -> fmt::Result {
149 write!(f, "[")?;
150 for (i, bytes) in list.iter().enumerate() {
151 if i != 0 {
152 write!(f, ", ")?;
153 }
154 hex_debug(bytes, f)?;
155 }
156 write!(f, "]")
157}
158
159pub fn visit_allocative_simple<T>(_: &T, visitor: &mut allocative::Visitor<'_>) {
161 visitor.visit_simple_sized::<T>();
162}
163
164#[cfg(not(target_arch = "wasm32"))]
167pub async fn listen_for_shutdown_signals(shutdown_sender: CancellationToken) {
168 let _shutdown_guard = shutdown_sender.drop_guard();
169
170 #[cfg(unix)]
171 {
172 let mut sigint =
173 unix::signal(unix::SignalKind::interrupt()).expect("Failed to set up SIGINT handler");
174 let mut sigterm =
175 unix::signal(unix::SignalKind::terminate()).expect("Failed to set up SIGTERM handler");
176 let mut sighup =
177 unix::signal(unix::SignalKind::hangup()).expect("Failed to set up SIGHUP handler");
178
179 tokio::select! {
180 _ = sigint.recv() => debug!("Received SIGINT"),
181 _ = sigterm.recv() => debug!("Received SIGTERM"),
182 _ = sighup.recv() => debug!("Received SIGHUP"),
183 }
184 }
185
186 #[cfg(windows)]
187 {
188 tokio::signal::ctrl_c()
189 .await
190 .expect("Failed to set up Ctrl+C handler");
191 debug!("Received Ctrl+C");
192 }
193}