diff --git a/Cargo.lock b/Cargo.lock index 5b6f497..924059c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1094,7 +1094,6 @@ version = "0.1.0" dependencies = [ "chrono", "crossterm", - "once_cell", "prost", "tui", "unicode-width", diff --git a/examples/toy-chat/Cargo.toml b/examples/toy-chat/Cargo.toml index f168b27..0a76870 100644 --- a/examples/toy-chat/Cargo.toml +++ b/examples/toy-chat/Cargo.toml @@ -13,5 +13,4 @@ tui = "0.19" crossterm = "0.25" unicode-width = "0.1" prost = "0.11" -once_cell = "1.15" chrono = "0.4" \ No newline at end of file diff --git a/examples/toy-chat/src/protocol.rs b/examples/toy-chat/src/protocol.rs index 673132f..501ad85 100644 --- a/examples/toy-chat/src/protocol.rs +++ b/examples/toy-chat/src/protocol.rs @@ -1,14 +1,9 @@ use chrono::{DateTime, LocalResult, TimeZone, Utc}; -use once_cell::sync::Lazy; use prost::Message; use waku_bindings::{Encoding, WakuContentTopic}; -pub static TOY_CHAT_CONTENT_TOPIC: Lazy = Lazy::new(|| WakuContentTopic { - application_name: "toy-chat".into(), - version: 2, - content_topic_name: "huilong".into(), - encoding: Encoding::Proto, -}); +pub static TOY_CHAT_CONTENT_TOPIC: WakuContentTopic = + WakuContentTopic::new("toy-chat", 2, "huilong", Encoding::Proto); #[derive(Clone, Message)] pub struct Chat2Message { diff --git a/waku-bindings/src/general/mod.rs b/waku-bindings/src/general/mod.rs index 85ae5c9..4c54dda 100644 --- a/waku-bindings/src/general/mod.rs +++ b/waku-bindings/src/general/mod.rs @@ -1,5 +1,6 @@ //! Waku [general](https://rfc.vac.dev/spec/36/#general) types +use std::borrow::Cow; // std use std::fmt::{Display, Formatter}; use std::str::FromStr; @@ -285,7 +286,7 @@ pub struct MessageIndex { } /// WakuMessage encoding scheme -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum Encoding { Proto, Rlp, @@ -321,14 +322,30 @@ impl RegexRepresentation for Encoding { } /// A waku content topic `/{application_name}/{version}/{content_topic_name}/{encdoing}` -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Eq, PartialEq)] pub struct WakuContentTopic { - pub application_name: String, + pub application_name: Cow<'static, str>, pub version: usize, - pub content_topic_name: String, + pub content_topic_name: Cow<'static, str>, pub encoding: Encoding, } +impl WakuContentTopic { + pub const fn new( + application_name: &'static str, + version: usize, + content_topic_name: &'static str, + encoding: Encoding, + ) -> Self { + Self { + application_name: Cow::Borrowed(application_name), + version, + content_topic_name: Cow::Borrowed(content_topic_name), + encoding, + } + } +} + impl FromStr for WakuContentTopic { type Err = String; @@ -337,9 +354,9 @@ impl FromStr for WakuContentTopic { scanf!(s, "/{}/{}/{}/{:/.+?/}", String, usize, String, Encoding) { Ok(WakuContentTopic { - application_name, + application_name: Cow::Owned(application_name), version, - content_topic_name, + content_topic_name: Cow::Owned(content_topic_name), encoding, }) } else { @@ -385,16 +402,23 @@ impl<'de> Deserialize<'de> for WakuContentTopic { } /// A waku pubsub topic in the form of `/waku/v2/{topic_name}/{encoding}` -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Eq, PartialEq)] pub struct WakuPubSubTopic { - pub topic_name: String, + pub topic_name: Cow<'static, str>, pub encoding: Encoding, } impl WakuPubSubTopic { - pub fn new(topic_name: String, encoding: Encoding) -> Self { + pub const fn new(topic_name: &'static str, encoding: Encoding) -> Self { Self { - topic_name, + topic_name: Cow::Borrowed(topic_name), + encoding, + } + } + + pub fn with_topic_name(topic_name: String, encoding: Encoding) -> Self { + Self { + topic_name: Cow::Owned(topic_name), encoding, } } @@ -406,7 +430,7 @@ impl FromStr for WakuPubSubTopic { fn from_str(s: &str) -> std::result::Result { if let Ok((topic_name, encoding)) = scanf!(s, "/waku/2/{}/{:/.+?/}", String, Encoding) { Ok(WakuPubSubTopic { - topic_name, + topic_name: Cow::Owned(topic_name), encoding, }) } else { diff --git a/waku-bindings/tests/node.rs b/waku-bindings/tests/node.rs index 985481f..a015fd3 100644 --- a/waku-bindings/tests/node.rs +++ b/waku-bindings/tests/node.rs @@ -83,12 +83,7 @@ pub fn main() -> Result<(), String> { // subscribe to default channel node.relay_subscribe(None)?; - let content_topic = WakuContentTopic { - application_name: "toychat".to_string(), - version: 2, - content_topic_name: "huilong".to_string(), - encoding: Encoding::Proto, - }; + let content_topic = WakuContentTopic::new("toychat", 2, "huilong", Encoding::Proto); let message = WakuMessage::new( content,