zbus/
utils.rs

1#[cfg(unix)]
2pub(crate) const FDS_MAX: usize = 1024; // this is hardcoded in sdbus - nothing in the spec
3
4pub(crate) fn padding_for_8_bytes(value: usize) -> usize {
5    padding_for_n_bytes(value, 8)
6}
7
8pub(crate) fn padding_for_n_bytes(value: usize, align: usize) -> usize {
9    let len_rounded_up = value.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
10
11    len_rounded_up.wrapping_sub(value)
12}
13
14/// Helper trait for macro-generated code.
15///
16/// This trait allows macros to refer to the `Ok` and `Err` types of a [Result] that is behind a
17/// type alias.  This is currently required because the macros for properties expect a Result
18/// return value, but the macro-generated `receive_` functions need to refer to the actual
19/// type without the associated error.
20#[doc(hidden)]
21pub trait ResultAdapter {
22    type Ok;
23    type Err;
24}
25
26impl<T, E> ResultAdapter for Result<T, E> {
27    type Ok = T;
28    type Err = E;
29}
30
31#[cfg(not(feature = "tokio"))]
32#[doc(hidden)]
33pub fn block_on<F: std::future::Future>(future: F) -> F::Output {
34    async_io::block_on(future)
35}
36
37#[cfg(feature = "tokio")]
38#[doc(hidden)]
39pub fn block_on<F: std::future::Future>(future: F) -> F::Output {
40    use std::sync::OnceLock;
41    static TOKIO_RT: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
42    let runtime = TOKIO_RT.get_or_init(|| {
43        tokio::runtime::Builder::new_current_thread()
44            .enable_io()
45            .enable_time()
46            .build()
47            .expect("launch of single-threaded tokio runtime")
48    });
49    runtime.block_on(future)
50}
51
52// If we're running inside a Flatpak sandbox.
53pub(crate) fn is_flatpak() -> bool {
54    std::env::var("FLATPAK_ID").is_ok()
55}