fuel_core_services/
lib.rs

1//! Common traits and logic for managing the lifecycle of services
2#![deny(clippy::arithmetic_side_effects)]
3#![deny(clippy::cast_possible_truncation)]
4#![deny(unused_crate_dependencies)]
5#![deny(missing_docs)]
6#![deny(warnings)]
7
8mod async_processor;
9pub mod seqlock;
10mod service;
11mod state;
12mod sync;
13#[cfg(feature = "sync-processor")]
14mod sync_processor;
15pub mod yield_stream;
16
17/// Re-exports for streaming utilities
18pub mod stream {
19    #[doc(no_inline)]
20    pub use futures::stream::{
21        pending,
22        unfold,
23        Stream,
24    };
25
26    /// A `Send` + `Sync` BoxStream with static lifetime.
27    pub type BoxStream<T> =
28        core::pin::Pin<Box<dyn Stream<Item = T> + Send + Sync + 'static>>;
29
30    /// A `Send` BoxStream with a lifetime.
31    pub type RefBoxStream<'a, T> = core::pin::Pin<Box<dyn Stream<Item = T> + Send + 'a>>;
32
33    /// A Send + Sync BoxFuture
34    pub type BoxFuture<'a, T> =
35        core::pin::Pin<Box<dyn futures::Future<Output = T> + Send + Sync + 'a>>;
36
37    /// Helper trait to create a BoxStream from a Stream
38    pub trait IntoBoxStream: Stream {
39        /// Convert this stream into a [`BoxStream`].
40        fn into_boxed(self) -> BoxStream<Self::Item>
41        where
42            Self: Sized + Send + Sync + 'static,
43        {
44            Box::pin(self)
45        }
46
47        /// Convert this stream into a [`RefBoxStream`].
48        fn into_boxed_ref<'a>(self) -> RefBoxStream<'a, Self::Item>
49        where
50            Self: Sized + Send + 'a,
51        {
52            Box::pin(self)
53        }
54    }
55
56    impl<S> IntoBoxStream for S where S: Stream + Send {}
57}
58
59/// Helper trait to trace errors
60pub trait TraceErr {
61    /// Trace an error with a message.
62    fn trace_err(self, msg: &str) -> Self;
63}
64
65impl<T, E> TraceErr for Result<T, E>
66where
67    E: Display,
68{
69    fn trace_err(self, msg: &str) -> Self {
70        if let Err(e) = &self {
71            tracing::error!("{} {}", msg, e);
72        }
73        self
74    }
75}
76
77pub use async_processor::AsyncProcessor;
78pub use seqlock::{
79    SeqLock,
80    SeqLockReader,
81    SeqLockWriter,
82};
83pub use service::{
84    EmptyShared,
85    RunnableService,
86    RunnableTask,
87    Service,
88    ServiceRunner,
89    TaskNextAction,
90};
91pub use state::{
92    State,
93    StateWatcher,
94};
95use std::fmt::Display;
96pub use sync::{
97    Shared,
98    SharedMutex,
99};
100#[cfg(feature = "sync-processor")]
101pub use sync_processor::SyncProcessor;
102
103// For tests
104use crate as fuel_core_services;
105#[allow(unused_imports)]
106use fuel_core_services as _;