1use std::ops::Deref;
14use std::task::{Poll, ready};
15
16use crate::{Result, broadcast, frame, group, track};
17
18#[derive(Clone)]
23pub struct Consumer {
24 inner: track::Consumer,
25}
26
27impl Consumer {
28 pub(super) fn new(inner: track::Consumer) -> Self {
29 Self { inner }
30 }
31
32 pub fn name(&self) -> &str {
34 self.inner.name()
35 }
36
37 pub fn broadcast(&self) -> &broadcast::Info {
39 self.inner.broadcast()
40 }
41
42 pub fn subscribe(&self, subscription: impl Into<Option<track::Subscription>>) -> kio::Pending<Subscribing> {
45 kio::Pending::new(Subscribing {
46 inner: self.inner.subscribe(subscription).into_inner(),
47 })
48 }
49}
50
51pub struct Subscribing {
53 inner: track::Subscribing,
54}
55
56impl Subscribing {
57 pub fn poll_ok(&self, waiter: &kio::Waiter) -> Poll<Result<Subscriber>> {
59 let inner = ready!(self.inner.poll_ok(waiter))?;
60 Poll::Ready(Ok(Subscriber { inner }))
61 }
62
63 pub fn update(&mut self, subscription: track::Subscription) -> Result<()> {
66 self.inner.update(subscription)
67 }
68}
69
70impl kio::Task for Subscribing {
71 type Output = Result<Subscriber>;
72
73 fn poll(&mut self, waiter: &kio::Waiter) -> Poll<Self::Output> {
74 self.poll_ok(waiter)
75 }
76}
77
78pub struct Subscriber {
80 inner: track::Subscriber,
81}
82
83impl Subscriber {
84 pub fn info(&self) -> &track::Info {
86 self.inner.info()
87 }
88
89 pub fn name(&self) -> &str {
91 self.inner.name()
92 }
93
94 pub fn broadcast(&self) -> &broadcast::Info {
96 self.inner.broadcast()
97 }
98
99 pub fn control(&self) -> track::Control {
101 self.inner.control()
102 }
103
104 #[cfg_attr(feature = "async", kio::wrapper)]
106 pub fn poll_recv_group(&mut self, waiter: &kio::Waiter) -> Poll<Result<Option<Group>>> {
107 let group = ready!(self.inner.poll_recv_group(waiter))?;
108 Poll::Ready(Ok(group.map(Group::new)))
109 }
110
111 #[cfg_attr(feature = "async", kio::wrapper)]
113 pub fn poll_finished(&mut self, waiter: &kio::Waiter) -> Poll<Result<track::Position>> {
114 self.inner.poll_finished(waiter)
115 }
116
117 pub fn update(&mut self, subscription: track::Subscription) -> Result<()> {
119 self.inner.update(subscription)
120 }
121}
122
123pub struct Group {
127 inner: group::Consumer,
128}
129
130impl Deref for Group {
131 type Target = group::Info;
132
133 fn deref(&self) -> &Self::Target {
134 &self.inner
135 }
136}
137
138impl Group {
139 fn new(inner: group::Consumer) -> Self {
140 Self {
143 inner: inner.compressed(),
144 }
145 }
146
147 pub fn index(&self) -> u64 {
149 self.inner.index()
150 }
151
152 #[cfg_attr(feature = "async", kio::wrapper)]
156 pub fn poll_next_header(&mut self, waiter: &kio::Waiter) -> Poll<Result<Option<frame::Info>>> {
157 let Some(frame) = ready!(self.inner.poll_next_frame(waiter))? else {
158 return Poll::Ready(Ok(None));
159 };
160 self.inner.forget_payload();
162 Poll::Ready(Ok(Some(frame::Info {
163 size: frame.inflated.unwrap_or(frame.size),
164 timestamp: frame.timestamp,
165 })))
166 }
167}
168
169impl std::fmt::Debug for Group {
170 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
171 f.debug_struct("Group").field("start", &self.start).finish()
172 }
173}