Skip to content

Commit

Permalink
Make Topic a newtype
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nagisa committed Mar 17, 2021
1 parent 8f9377e commit 61e807d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
7 changes: 2 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion src/publish/publishers/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = &'a ValidatedMessage> + ExactSizeIterator,
{
Expand Down
21 changes: 21 additions & 0 deletions src/topic.rs
Original file line number Diff line number Diff line change
@@ -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<Topic> for &'static str {
fn from(s: Topic) -> &'static str {
s.0
}
}

0 comments on commit 61e807d

Please sign in to comment.