Skip to content

Commit 61e807d

Browse files
committed
Make Topic a newtype
Turns out having anything with any sort of lifetime in stream/sink chains will trigger nasty issues such as rust-lang/rust#79648 which is pretty difficult to figure out how to work around. Making `Topic` a newtype erases the lifetime from the type, making it significantly easier to work with in thsoe contexts.
1 parent 8f9377e commit 61e807d

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

src/lib.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,17 @@
106106
#![cfg_attr(docsrs, feature(doc_cfg))]
107107

108108
use std::{collections::BTreeMap, time::SystemTime};
109-
109+
pub use topic::Topic;
110110
use uuid::Uuid;
111111

112112
#[cfg(feature = "publish")]
113113
#[cfg_attr(docsrs, doc(cfg(feature = "publish")))]
114114
pub mod publish;
115-
116115
#[cfg(test)]
117116
mod tests;
117+
mod topic;
118118
pub mod validators;
119119

120-
/// A message queue topic name to which messages can be published
121-
pub type Topic = &'static str;
122-
123120
/// All errors that may be returned when operating top level APIs.
124121
#[derive(Debug, thiserror::Error)]
125122
#[non_exhaustive]

src/publish/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ where
185185
None => None,
186186
Some(stream_item) => Some((
187187
stream_item,
188-
this.topic,
188+
*this.topic,
189189
this.messages
190190
.next()
191191
.expect("should be as many messages as publishes"),

src/publish/publishers/null.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Publisher for NullPublisher {
2020
type MessageError = std::convert::Infallible;
2121
type PublishStream = NullPublishStream;
2222

23-
fn publish<'a, I>(&self, _: &'static str, messages: I) -> Self::PublishStream
23+
fn publish<'a, I>(&self, _: crate::Topic, messages: I) -> Self::PublishStream
2424
where
2525
I: Iterator<Item = &'a ValidatedMessage> + ExactSizeIterator,
2626
{

src/topic.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// A message queue topic name to which messages can be published
2+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
3+
pub struct Topic(pub &'static str);
4+
5+
impl std::fmt::Display for Topic {
6+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
7+
std::fmt::Display::fmt(self.0, f)
8+
}
9+
}
10+
11+
impl From<&'static str> for Topic {
12+
fn from(s: &'static str) -> Topic {
13+
Topic(s)
14+
}
15+
}
16+
17+
impl From<Topic> for &'static str {
18+
fn from(s: Topic) -> &'static str {
19+
s.0
20+
}
21+
}

0 commit comments

Comments
 (0)