nt_client/topic/
collection.rs1use std::{fmt::Debug, iter::FusedIterator};
4
5use crate::{ClientHandle, error::ConnectionClosedError, subscribe::{Subscriber, SubscriptionOptions}};
6
7use super::Topic;
8
9#[derive(Debug, Clone)]
37pub struct TopicCollection {
38 names: Vec<String>,
39 handle: ClientHandle,
40}
41
42impl IntoIterator for TopicCollection {
43 type Item = Topic;
44 type IntoIter = IntoIter;
45
46 fn into_iter(self) -> Self::IntoIter {
47 IntoIter::new(self)
48 }
49}
50
51impl PartialEq for TopicCollection {
52 fn eq(&self, other: &Self) -> bool {
53 self.names == other.names
54 }
55}
56
57impl Eq for TopicCollection { }
58
59impl TopicCollection {
60 pub(crate) fn new(
61 names: Vec<String>,
62 handle: ClientHandle,
63 ) -> Self {
64 Self { names, handle }
65 }
66
67 pub fn names(&self) -> &Vec<String> {
69 &self.names
70 }
71
72 pub fn names_mut(&mut self) -> &mut Vec<String> {
74 &mut self.names
75 }
76
77 pub async fn subscribe(&self, options: SubscriptionOptions) -> Result<Subscriber, ConnectionClosedError> {
83 Subscriber::new(self.names.clone(), options, self.handle.announced_topics.clone(), self.handle.server_send.clone(), self.handle.client_send.subscribe()).await
84 }
85}
86
87pub struct IntoIter {
91 name_iter: std::vec::IntoIter<String>,
92 handle: ClientHandle,
93}
94
95impl Iterator for IntoIter {
96 type Item = Topic;
97
98 fn next(&mut self) -> Option<Self::Item> {
99 self.name_iter.next()
100 .map(|name| Topic::new(name, self.handle.clone()))
101 }
102}
103
104impl DoubleEndedIterator for IntoIter {
105 fn next_back(&mut self) -> Option<Self::Item> {
106 self.name_iter.next_back()
107 .map(|name| Topic::new(name, self.handle.clone()))
108 }
109}
110
111impl ExactSizeIterator for IntoIter {
112 fn len(&self) -> usize {
113 self.name_iter.len()
114 }
115}
116
117impl FusedIterator for IntoIter { }
118
119impl IntoIter {
120 pub(self) fn new(collection: TopicCollection) -> Self {
121 IntoIter {
122 name_iter: collection.names.into_iter(),
123 handle: collection.handle,
124 }
125 }
126}
127