From 61e807deae830ec0992d2dc66a9b556af4b769a0 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Wed, 17 Mar 2021 23:57:48 +0200 Subject: [PATCH] Make Topic a newtype Turns out having anything with any sort of lifetime in stream/sink chains will trigger nasty issues such as https://github.com/rust-lang/rust/issues/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. --- src/lib.rs | 7 ++----- src/publish/mod.rs | 2 +- src/publish/publishers/null.rs | 2 +- src/topic.rs | 21 +++++++++++++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 src/topic.rs diff --git a/src/lib.rs b/src/lib.rs index 945efd6..20507e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -106,20 +106,17 @@ #![cfg_attr(docsrs, feature(doc_cfg))] use std::{collections::BTreeMap, time::SystemTime}; - +pub use topic::Topic; use uuid::Uuid; #[cfg(feature = "publish")] #[cfg_attr(docsrs, doc(cfg(feature = "publish")))] pub mod publish; - #[cfg(test)] mod tests; +mod topic; pub mod validators; -/// A message queue topic name to which messages can be published -pub type Topic = &'static str; - /// All errors that may be returned when operating top level APIs. #[derive(Debug, thiserror::Error)] #[non_exhaustive] diff --git a/src/publish/mod.rs b/src/publish/mod.rs index f943731..8b68124 100644 --- a/src/publish/mod.rs +++ b/src/publish/mod.rs @@ -185,7 +185,7 @@ where None => None, Some(stream_item) => Some(( stream_item, - this.topic, + *this.topic, this.messages .next() .expect("should be as many messages as publishes"), diff --git a/src/publish/publishers/null.rs b/src/publish/publishers/null.rs index 6721c44..00fcf53 100644 --- a/src/publish/publishers/null.rs +++ b/src/publish/publishers/null.rs @@ -20,7 +20,7 @@ impl Publisher for NullPublisher { type MessageError = std::convert::Infallible; type PublishStream = NullPublishStream; - fn publish<'a, I>(&self, _: &'static str, messages: I) -> Self::PublishStream + fn publish<'a, I>(&self, _: crate::Topic, messages: I) -> Self::PublishStream where I: Iterator + ExactSizeIterator, { diff --git a/src/topic.rs b/src/topic.rs new file mode 100644 index 0000000..e05952a --- /dev/null +++ b/src/topic.rs @@ -0,0 +1,21 @@ +/// A message queue topic name to which messages can be published +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +pub struct Topic(pub &'static str); + +impl std::fmt::Display for Topic { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + std::fmt::Display::fmt(self.0, f) + } +} + +impl From<&'static str> for Topic { + fn from(s: &'static str) -> Topic { + Topic(s) + } +} + +impl From for &'static str { + fn from(s: Topic) -> &'static str { + s.0 + } +}