bach/queue/
span.rs

1use super::{CloseError, PopError, PushError, Pushable};
2use crate::tracing::{info_span, Span};
3use alloc::borrow::Cow;
4use core::{fmt, ops};
5use std::task::Context;
6
7pub struct Queue<Q> {
8    #[cfg_attr(not(feature = "tracing"), allow(dead_code))]
9    name: Cow<'static, str>,
10    inner: Q,
11}
12
13impl<Q: Default> Default for Queue<Q> {
14    fn default() -> Self {
15        let name = "";
16        let inner = Q::default();
17        Self {
18            name: Cow::Borrowed(name),
19            inner,
20        }
21    }
22}
23
24impl<Q: fmt::Debug> fmt::Debug for Queue<Q> {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        self.inner.fmt(f)
27    }
28}
29
30impl<Q> ops::Deref for Queue<Q> {
31    type Target = Q;
32
33    #[inline]
34    fn deref(&self) -> &Self::Target {
35        &self.inner
36    }
37}
38
39impl<Q> ops::DerefMut for Queue<Q> {
40    #[inline]
41    fn deref_mut(&mut self) -> &mut Self::Target {
42        &mut self.inner
43    }
44}
45
46impl<Q> Queue<Q> {
47    pub fn new<N: Into<Cow<'static, str>>>(inner: Q, name: N) -> Self {
48        Self {
49            name: name.into(),
50            inner,
51        }
52    }
53
54    fn span(&self) -> Span {
55        info_span!("queue", queue = %self.name)
56    }
57}
58
59impl<T, Q> super::Queue<T> for Queue<Q>
60where
61    Q: super::Queue<T>,
62{
63    fn push_lazy(&mut self, value: &mut dyn Pushable<T>) -> Result<Option<T>, PushError> {
64        self.span().in_scope(|| self.inner.push_lazy(value))
65    }
66
67    fn push_with_notify(
68        &mut self,
69        value: &mut dyn Pushable<T>,
70        cx: &mut Context,
71    ) -> Result<Option<T>, PushError> {
72        self.span()
73            .in_scope(|| self.inner.push_with_notify(value, cx))
74    }
75
76    fn pop(&mut self) -> Result<T, PopError> {
77        self.span().in_scope(|| self.inner.pop())
78    }
79
80    fn pop_with_notify(&mut self, cx: &mut Context) -> Result<T, PopError> {
81        self.span().in_scope(|| self.inner.pop_with_notify(cx))
82    }
83
84    fn close(&mut self) -> Result<(), CloseError> {
85        self.span().in_scope(|| self.inner.close())
86    }
87
88    fn is_closed(&self) -> bool {
89        self.span().in_scope(|| self.inner.is_closed())
90    }
91
92    fn is_empty(&self) -> bool {
93        self.span().in_scope(|| self.inner.is_empty())
94    }
95
96    fn is_full(&self) -> bool {
97        self.span().in_scope(|| self.inner.is_full())
98    }
99
100    fn len(&self) -> usize {
101        self.span().in_scope(|| self.inner.len())
102    }
103
104    fn capacity(&self) -> Option<usize> {
105        self.span().in_scope(|| self.inner.capacity())
106    }
107}