v0.1.2-pre.002
This commit is contained in:
42
crates/ksp-logging-lib/src/span.rs
Normal file
42
crates/ksp-logging-lib/src/span.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
// file: crates/ksp-logging-lib/src/span.rs
|
||||
// version: 1
|
||||
|
||||
/// KSP-owned handle to a tracing span.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Span {
|
||||
inner: tracing::Span,
|
||||
}
|
||||
|
||||
impl Span {
|
||||
/// Runs synchronous work while this span is entered.
|
||||
pub fn in_scope<T>(&self, operation: impl std::ops::FnOnce() -> T) -> T {
|
||||
return self.inner.in_scope(operation);
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
/// Constructs the KSP span wrapper for macro expansion support.
|
||||
#[must_use]
|
||||
pub fn __from_tracing(inner: tracing::Span) -> Self {
|
||||
return Self { inner };
|
||||
}
|
||||
|
||||
/// Consumes this wrapper and returns the internal tracing span.
|
||||
pub(crate) fn into_tracing(self) -> tracing::Span {
|
||||
return self.inner;
|
||||
}
|
||||
}
|
||||
|
||||
/// Instruments an asynchronous future with a KSP span.
|
||||
///
|
||||
/// The span is entered whenever the future is polled and exited whenever it yields, so no enter guard is held across an `.await` point.
|
||||
#[must_use]
|
||||
pub fn instrument<F>(span: crate::Span, future: F) -> impl std::future::Future<Output = F::Output>
|
||||
where
|
||||
F: std::future::Future,
|
||||
{
|
||||
return tracing::Instrument::instrument(future, span.into_tracing());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../unit_tests/span.rs"]
|
||||
mod tests;
|
||||
Reference in New Issue
Block a user